Introduction
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.
High-Performance Virtualization Servers Starting at Only $378.00/mo
Prerequisites
- 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:
docker ps
As you can see, the image above indicates there are no running containers.
To list all containers, both running and stopped, add –a
:
docker ps –a
To list containers by their ID use –aq
(quiet):
docker ps –aq
To list the total file size of each container, use –s
(size):
docker ps –s
To list the latest created containers, use –l
(latest):
docker ps –l
The ps
command provides several columns of information:
Container ID
– a unique alphanumeric number for each containerImage
– The base operating system image the container is based onCommand
– The command that launched the containerCreated
– How long ago the container was createdStatus
– Uptime or downtimePorts
– Specifies any ports forwarded to the container for networkingName
– 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
:
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
:
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 ––name
option:
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 –i
and –t
options:
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 -i
or -t
options, use the attach
command to connect to a running container:
docker attach container_id
Stop Docker Container
Use the 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.
Conclusion
This tutorial provided options to list, start, and stop, Docker containers.
Docker is used by development teams to ensure consistency across different machines.
Next you should also read
DevOps and Development,Virtualization
Docker ADD vs COPY: What are the Differences?
December 16, 2019
If you are creating a Dockerfile, you may be puzzled by how to copy files and directories into it. Docker…
SysAdmin,DevOps and Development,Virtualization
How to Set Up and Use Private Docker Registry
December 5, 2019
By setting up a private Docker registry, you can save valuable resources and speed up development processes.…
DevOps and Development,Virtualization
List of Docker Commands: Cheat Sheet
December 2, 2019
Docker has earned a reputation as one of the most popular open-source platforms for application development.…
SysAdmin,DevOps and Development,Virtualization
How to Install Docker Compose on CentOS 7
November 19, 2019
If you are a Docker user, you are most likely running and managing multiple containers at the same time.…
DevOps and Development,Virtualization
How to Commit Changes to a Docker Image with Examples
November 14, 2019
Docker allows users to run a container based on an existing image. This feature is both time efficient and…
How To Remove Docker Images, Containers, Networks & Volumes
February 7, 2019
Docker allows users to create a container in which an application or process can run. In this guide, you will…
Author
Dejan Tucakov
Dejan is the Technical Writing Team Lead at phoenixNAP with over 6 years of experience in Web publishing. Prior to joining phoenixNAP, he was Chief Editor of several websites striving to advocate for emerging technologies. He is dedicated to simplifying complex notions and providing meaningful insight into data center and cloud technology.