Back to Blog
Lesson 4 of the Docker: Containers & Your First Image course
DevOpsJuly 15, 20264 min read

Managing Container States: A Beginner's Guide to Docker Lifecycle

Master container management by learning how to start, stop, and list containers. Get hands-on experience controlling the lifecycle of your Docker instances.

DockerDevOpsContainer ManagementLifecycle
Shipping containers and cranes at Hamburg port showcasing global trade.

Previously in this course, we covered the basics of executing your first container in Running Hello World: Your First Docker Container Lifecycle. Now that you know how to launch a container, this lesson adds the ability to control its state—starting, stopping, and tracking containers as they move through their lifecycle.

The Container Lifecycle: Beyond the First Run

When you execute docker run, you are creating a new container instance from an image. However, a container isn't a "set it and forget it" process. In a real-world project, you will often need to pause, stop, or restart services as you develop your multi-service application. Understanding how to manage these states is the first step toward effective container management.

Conceptually, a container exists in one of three primary states:

  • Running: The process is active and executing.
  • Stopped: The process has exited, but the container file system and configuration persist on disk.
  • Removed: The container is deleted entirely (we will cover this in a future lesson).

Practical Container Management Commands

To manage these states, we use the Docker CLI. Let’s look at the three most critical commands for daily operations.

1. Listing Active Containers

To see what is currently running on your host, use: docker ps

This command provides a snapshot of your environment, showing the container ID, the image used, the status, and the ports mapped.

2. Stopping a Container

If you have a container running in the background (or even in the foreground), you can stop it gracefully. The Docker daemon sends a SIGTERM signal to the main process, allowing it to save state or close connections before shutting down. docker stop <container_id>

3. Starting a Stopped Container

Once a container is stopped, it doesn't disappear; it simply enters an "Exited" state. To bring it back to life without creating a new instance, use: docker start <container_id>

Worked Example: The Lifecycle Loop

Let's put this into practice. We will launch a lightweight web server container, observe it, stop it, and restart it.

  1. Start a container in detached mode:

    Bash
    docker run -d --name my-web-server nginx

    The -d flag runs the container in the background.

  2. Verify it is active:

    Bash
    docker ps

    You should see my-web-server listed in the output.

  3. Stop the container:

    Bash
    docker stop my-web-server
  4. Confirm it is gone from the active list:

    Bash
    docker ps

    The list will now be empty.

  5. Restart the existing container:

    Bash
    docker start my-web-server

    The container resumes its previous state.

Hands-on Exercise

To advance our running project, follow these steps:

  1. Run a new container using the alpine image: docker run -d --name my-test-service alpine sleep 3600.
  2. List the active container using docker ps.
  3. Stop the container.
  4. Verify the container has stopped by running docker ps again.
  5. Start it back up and verify its status.

Common Pitfalls

  • Confusing run with start: Many beginners try to use docker run to restart a stopped container. Remember: run creates a new instance. Use start to resume an existing one.
  • Ignoring the Status: If you think a container is running but docker ps shows nothing, it likely exited due to an error.
  • Stopping too aggressively: If a container doesn't stop within 10 seconds of a docker stop command, Docker will issue a SIGKILL (force kill). Always design your applications to handle SIGTERM signals gracefully to avoid data corruption.

FAQ

Q: Does docker stop delete my data? A: No. Data stored inside the container's writable layer remains intact until you explicitly remove the container.

Q: How do I see containers that aren't running? A: docker ps only shows running containers. Use docker ps -a to list all containers, including those that have stopped.

Q: Why does my container exit immediately after starting? A: A container only stays "running" as long as its primary process (the entrypoint) is active. If the process finishes, the container stops.

Recap

Container management is about controlling the lifecycle of your instances. By mastering docker ps to view status, and docker start/docker stop to transition between states, you keep your environment organized and efficient. These commands are the foundation of managing the multi-service application we are building throughout this course.

Up next: We will dive deeper into container visibility by learning how to use docker ps -a to inspect stopped containers and view logs to debug why a container might have failed.

Similar Posts