Docker - Containers
Docker - Containers
Section titled “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.
Running a Container (docker run)
Section titled “Running a Container (docker run)”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 shelldocker run -it ubuntu bashThis 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 backgrounddocker run -d --name my-web-server nginxThis command starts the container and prints the container ID, then returns you to the host shell prompt.
Listing Containers (docker ps)
Section titled “Listing Containers (docker ps)”To see the containers that are currently running on your system, use docker ps (or docker container ls).
docker psOutput 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).
Listing All Containers (docker ps -a)
Section titled “Listing All Containers (docker ps -a)”To see all containers, including those that are stopped or exited, add the -a flag:
docker ps -aThis 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' imagedocker history nginx:latestThis helps understand the structure of an image.
Viewing Container Logs (docker logs)
Section titled “Viewing Container Logs (docker logs)”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 logsdocker logs --tail 10 my-web-serverThis is essential for debugging applications running in containers.
Stopping and Starting Containers
Section titled “Stopping and Starting 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 containerdocker start my-web-server
# Restart a running or stopped containerdocker restart my-web-server
# Forcefully stop a container immediately (sends SIGKILL)docker kill my-web-serverRemoving Containers (docker rm)
Section titled “Removing Containers (docker rm)”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 containerdocker rm my-web-server
# Force remove a running container (stops and removes)docker rm -f my-web-serverPruning Containers
Section titled “Pruning Containers”To easily remove all stopped containers:
docker container prune