Docker Containers: Key Commands

Get a brief summary of the commands for managing Docker containers covered in this chapter.

Summary of the Docker container commands

  • docker run is the command to start new containers. We give it the name of an image, and it starts a container from it. For example, the docker run -it ubuntu bash command starts an interactive container from the Ubuntu image and runs the Bash shell.

  • Ctrl-PQ is how we detach from a container stopping the process we’re attached to. We’ll use it frequently to detach from running containers without terminating them.

  • docker ps lists all running containers, and we can add the -a flag to also see containers in the stopped (Exited) state.

  • docker exec allows us to run commands inside containers. For example, the docker exec -it <container-name> bash command will start a new Bash shell inside a running container and connect your terminal to it. For this to work, the container must include the Bash shell. This command runs a ps command inside a running container without opening an interactive shell session: docker exec <container-name> ps.

  • docker stop stops a running container and puts it in the Exited (137) state. It issues a SIGTERM to the container’s PID 1 process and allows the container 10 seconds to gracefully quit. If the process hasn’t cleaned up and stopped within 10 seconds, it sends a SIGKILL to force the container to immediately terminate.

  • docker restart restarts a stopped container.

  • docker rm deletes a stopped container. We can add the -f flag to delete the container without having to stop it first.

  • docker inspect shows us the detailed configuration and runtime information about a container.

  • docker debug attaches a debug shell to a container or image and lets us run commands that aren’t available inside the container or image. It requires a Pro, Team, or Business Docker subscription.

Get hands-on with 1300+ tech skills courses.