Skip to content

Docker - Containers

Containers are the running instances of Docker images. They encapsulate an application and its dependencies. The primary purpose of Docker is to build, ship, and run containers. This chapter focuses on managing the lifecycle of containers.

The fundamental command to create and start a container is docker run. We’ve seen basic examples, let’s revisit running interactively:

# Run a container from the 'ubuntu' image, interactively (-it), and start a bash shell
docker run -it ubuntu bash

This starts the container, attaches your terminal, and runs the bash command inside it. You can detach using Ctrl+P, Ctrl+Q to leave the container running, or type exit to stop it.

To run a container in the background (detached mode), use the -d flag:

# Run an nginx container in the background
docker run -d --name my-web-server nginx

This command starts the container and prints the container ID, then returns you to the host shell prompt.

To see the containers that are currently running on your system, use docker ps (or docker container ls).

docker ps

Output columns typically include:

  • CONTAINER ID: Unique ID (short version shown).
  • IMAGE: Image the container is based on.
  • COMMAND: Command being executed inside the container.
  • CREATED: Time since container creation.
  • STATUS: Current state (e.g., ‘Up 2 minutes’, ‘Exited (0) 5 seconds ago’).
  • PORTS: Port mappings (e.g., 0.0.0.0:8080->80/tcp).
  • NAMES: Name assigned to the container (auto-generated if not specified with --name).

To see all containers, including those that are stopped or exited, add the -a flag:

docker ps -a

This is useful for seeing containers that ran briefly and exited, or containers you have manually stopped.

Viewing Container History (Image Layers - docker history)

Section titled “Viewing Container History (Image Layers - docker history)”

The docker history command doesn’t show commands run inside a container instance, but rather shows the history of how the image itself was built, layer by layer. It displays the commands from the Dockerfile that created each layer.

# Show the build history of the 'nginx:latest' image
docker history nginx:latest

This helps understand the structure of an image.

To see the output (stdout/stderr) generated by the main process running inside a container, use docker logs.

# Show logs for the container named 'my-web-server'
docker logs my-web-server
# Follow logs in real-time (like tail -f)
docker logs -f my-web-server
# Show the last 10 lines of logs
docker logs --tail 10 my-web-server

This is essential for debugging applications running in containers.

You can stop and restart existing containers:

# Stop the container named 'my-web-server' gracefully (sends SIGTERM, then SIGKILL)
docker stop my-web-server
# Start a stopped container
docker start my-web-server
# Restart a running or stopped container
docker restart my-web-server
# Forcefully stop a container immediately (sends SIGKILL)
docker kill my-web-server

Once you no longer need a container, you can remove it using docker rm. Containers must be stopped before they can be removed.

# Stop the container first (if running)
docker stop my-web-server
# Remove the stopped container
docker rm my-web-server
# Force remove a running container (stops and removes)
docker rm -f my-web-server

To easily remove all stopped containers:

docker container prune