12 Essential Docker Commands
Docker, the open source container platform, has become a must-know technology for many admins and developers. Even if deploying and managing containers is not a major part of your job — or if you use a container orchestrator like KubernetesĀ — a basic familiarity with Docker is important for developing and managing a variety of applications today.
To help developers and admins get started with Docker, this article provides an overview of ten essential Docker commands that you can run using the Docker CLI tool, docker.
#1. Download a container image
One of the first steps in deploying a containerized application is to download it. You can do so with the docker pull command. For example, to download the latest container image for the NGINX Web server, run:
docker pull nginx
Note that by default, the docker pull command downloads images from Docker Hub, Dockerās public container registry platform. The image must first be tagged (using the docker tag command, which we discuss below) with the alternative registryās name. Then, you can pull the image with a command like:
docker pull myregistry.com/myrepo/nginx
Note also that docker pull by default will pull the image tagged ālatest.ā To specify a version, append the version number to the command, as follows:
docker pull nginx:1
#2. Push a container image
If you create a container image and want to upload it to a registry, you use the docker push command:
docker push image-name
Like docker pull, docker push pushes to Docker Hub by default. To specify a different registry, first tag that image, then use a command like:
docker push myregistry.com/myrepo/nginx
#3. List local images
The command docker images lists images that you have downloaded to your local system. With no arguments, the command will list all images, but you can filter the output in various ways. For example, to display all images from the repository ānginx,ā run:
docker images nginx
#4. Run a new container based on an image
To create and run a brand-new container based on an image you have downloaded (e.g. nginx), use the docker run command:
docker run nginx
Donāt use docker start, which does something slightly different, as explained immediately below.
Note that on its own, docker run creates a new container with a random name, and youāll have to identify it using docker ps. To specify a name, use the –name flag:
docker run –name mynginxcontainer nginx
#5. Stop a container
Use docker stop to stop a running container:
docker stop mynginxcontainer
#6. Restart a stopped container
The docker start command starts a container that you previously created and ran using docker run. For instance:
docker start mynginxcontainer
Note that this is different from the docker run command because docker start starts a container based on an image that you ran previously using docker run. In other words, use docker run the first time you want to start a container based on an image you have downloaded, and use docker start to create an additional container (or restart a failed one) based on that image.
#7. Pause and unpause a container
Docker allows you to pause containers with docker pause. The command suspends all processes running within the specified container:
docker pause mynginxcontainer
To unpause, use docker unpause:
docker unpause mynginxcontainer
#8. List running containers
The docker ps command lists containers currently running on your system, along with a little information about them (such as the IDs that Docker has assigned to them):
docker ps
You can use the -f flag to filter the list of containers based on various criteria. For example, if you want to list containers that are in the paused stage, run:
docker ps -f status=paused
Also particularly useful is the -a flag, which allows you to list all containers (stopped, running or other):
docker ps -a
#9. Tag an image
The docker tag command applies a tag name of your choosing to an image:
docker tag nginx mynginximage
The first argument can be either the tag name or the image ID of an existing image on your system. The second argument is the tag to apply to the image.
Image tags contain a number of components in the following format, many of them optional:
REGISTRY_HOST_NAME:PORT/PATH/TO/IMAGE_NAME:TAG
If these components are not specified, they are supplied default values. For example, an image tagged ānginxā uses the public Docker Hub registry at āregistry-1.docker.ioā by default and the ālatestā version tag. To tag the nginx image from Docker Hub with a private Docker registry and a specific version, first ensure you have pulled the version required, and then use a command like the following:
docker tag nginx:1 myregistry.com/myrepo/nginx:1
Note also that to save time, you can specify the tag name of a custom image when running the docker build command:
docker build –tag myimageĀ /path/to/Dockerfile
#10. Build a container image
Use docker build to create a container image based on an existing Docker file:
docker build /path/to/Dockerfile
You can also use the command to build an image based on a Dockerfile that is pulled automatically from a URL. For example:
docker build github.com/myproject/myimage
#11. View processes running in a container
You may sometimes want to know which specific processes are running inside a container. While it may be possible to get this information in various ways from the host OS, you can also do with a simple docker top command:
docker top mynginxcontainer
#12. Track container resource usage
The docker stats command displays basic information about resource consumption by containers. By default, it displays data for all running containers, but you can use the -a flag to tell it to display information for both running and non-running containers:
docker stats -a
This command is not a replacement for a full-fledged Docker monitoring solution, but itās handy if you need to collect basic resource consumption data from containers in real time.
Learn additional Docker tips
For additional tips on using Docker, check out JFrogās Docker cheat sheet, which provides a concise overview of how Docker works, when to use it and how to get started with Docker commands.