Docker Containers - The Commands
A quick overview of the commands we’ve seen in this chapter for working with Docker containers.
We'll cover the following
The summary of Docker container commands
-
docker run
is the command to start new containers. You give it the name of an image and it starts a container from it. This example starts an interactive container from the Ubuntu image and tells it to run the Bash shell:docker run -it ubuntu bash
. -
Ctrl-PQ
is how you detach from a container without killing the process you’re attached to. You’ll use it frequently to detach from running containers without killing them. -
docker ps
lists all running containers, and you can add the-a
flag to also see containers in the stopped(Exited)
state. -
docker exec
allows you to run commands inside containers. The following command will start a new Bash shell inside a running container and connect your terminal to it:docker exec -it <container-name> bash
. For this to work, the container must include the Bash shell. This command runs aps
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 theExited (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. You can add the-f
flag to delete the container without having to stop it first. -
docker inspect
shows you detailed configuration and run-time information about a container. -
docker debug
attaches a debug shell to a container or image and lets you run commands that aren’t available inside the container or image. It requires a Pro, Team, or Business Docker subscription.
Get hands-on with 1400+ tech skills courses.