Skip to content

Chapter 4 of 7

Running and Managing Containers

The docker run command is the primary way to create and start a new container from an image. For example, docker run -it ubuntu bash launches an Ubuntu container with an interactive terminal. By default, docker run attaches to the container's foreground, but adding the -d flag runs the container in detached mode in the background and prints the container ID instead. Many other flags control networking, environment variables, mounted volumes, and resource limits, making docker run both the simplest and the richest command in the toolkit.

To see what is currently running, docker ps lists active containers, while docker ps -a also includes containers that have stopped. The output includes useful information such as container IDs, the image used, status, port mappings, and names. To manage lifecycle, docker stop container_id sends a graceful SIGTERM signal, whereas docker kill forces immediate termination with SIGKILL. Once a container is stopped, it can be deleted with docker rm container_id, or you can clean up many at once with docker container prune.

Often you need to interact with a container that is already running. The docker exec command runs an additional process inside an existing container; the common pattern docker exec -it container_id /bin/bash opens an interactive shell so you can investigate or fix problems without restarting the service. Combined with docker ps, docker stop, docker rm, and docker exec, these commands form the day-to-day toolkit for working with containers by hand.

All chapters
  1. 1Foundations of Containers and Docker
  2. 2Docker Architecture, Installation, and Core CLI
  3. 3Building Images with Dockerfile
  4. 4Running and Managing Containers
  5. 5Data Persistence and Networking
  6. 6Docker Compose and Orchestration with Swarm
  7. 7Security and Troubleshooting

Drill it

Reading is not remembering. These come from the Docker And Containers deck:

Q

What is a container in computing?

A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, such as code, runtime, libraries, and d...

Q

What is containerization?

Containerization is a virtualization method that packages an application and its dependencies into a container, enabling consistent deployment across different...

Q

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications inside containers using containerization technology.

Q

What is the difference between a container and a virtual machine?

Containers share the host OS kernel and are lightweight, starting in seconds, while virtual machines include a full guest OS, making them heavier and slower to...