Docker is a popular virtualization tool that replicates a specific operating environment on top of a host OS. Each environment is called a container. Managing containers is essential for working in Docker.
A container uses an image of a preconfigured operating system optimized for a specific task. When a Docker image is launched, it exists in a container. For example, multiple containers may run the same image at the same time on a single host operating system.
This guide shows you how to list, stop, and start Docker containers.
- A Linux-based operating system
- Access to a user account with root or sudo privileges
- A preconfigured Docker installation with images
List Docker Containers
The basic format for using docker is:
docker command [options]
To list all running Docker containers, enter the following into a terminal window:
As you can see, the image above indicates there are no running containers.
To list all containers, both running and stopped, add
docker ps –a
To list containers by their ID use
docker ps –aq
To list the total file size of each container, use
docker ps –s
To list the latest created containers, use
docker ps –l
ps command provides several columns of information:
Container ID– a unique alphanumeric number for each container
Image– The base operating system image the container is based on
Command– The command that launched the container
Created– How long ago the container was created
Status– Uptime or downtime
Ports– Specifies any ports forwarded to the container for networking
Name– A memorable name assigned by the Docker software
Note: This guide assumes you already have an existing image. If you don’t, use the following:
docker pull name:tag
For example, enter
docker pull ubuntu:14.04 to grab a copy of the Ubuntu 14.04 image.
Start Docker Container
The main command to launch or start a single or multiple stopped Docker containers is
docker start [options] container_id
You can specify the container by either using its name or ID (long or short).
To create a new container from an image and start it, use
docker run [options] image [command] [argument]
If you do not define a name for your newly created container, the deamon will generate a random string name. To define container name, use the
docker run ––name=Ubuntu_Test ubuntu:14.04
The above mentioned command will create the Ubuntu_test container based on the ubuntu:14.04 image and start it.
A container may be running, but you may not be able to interact with it. To start the container in interactive mode, use the
docker run –it ––name=Ubuntu_Test ubuntu:14.04
In the above mentioned example, the system creates the Test_2 container from the ubuntu image, and connects to it enabling you to run commands directly on the container.
Instead of using
-t options, use the
attach command to connect to a running container:
docker attach container_id
Stop Docker Container
docker stop command to stop a container:
docker stop [option] container_id
Replace container_id with the container’s name or ID.
By default, you get a 10 second grace period. The
stop command instructs the container to stop services after that period. Use the
--time option to define a different grace period expressed in seconds:
docker stop --time=20 container_id
To immediately kill a docker container without waiting for the grace period to end use:
docker kill [option] container_id
To stop all running containers, enter the following:
docker stop $(docker ps –a –q)
The same command could be used with
kill. This would stop all containers without giving them a chance to exit.
This tutorial provided options to list, start, and stop, Docker containers.
Docker is used by development teams to ensure consistency across different machines.