Docker CLI commands and what you can do with them
twain taylor Jan 10, 2018
Docker has changed the way applications are developed. In a short span of time since its launch in 2014 Docker has found a place in almost every development organization that cares about the quality and efficiency of its software development. This growth has been fueled primarily by developers. One reason why developers are such rabid Docker fans is its simple yet powerful list of commands. What used to take many clicks and long commands is now brought down to simple commands that are easy to remember, and build on.
New to Docker? Let’s delve deeper to know what’s great about the Docker CLI, and some of the most commonly used commands.
Docker supports hierarchical command structures. The following list of Docker commands consists of child commands to the base command: docker, described above.
This article covers the most basic commands one needs in order to get started with Docker. See this Docker CLI wiki page for a bunch of useful resources to get you started.
This is a useful command when getting started with Docker. Running the docker help command shows you the syntax for Docker commands, and a list of commands with explanations for what they do.
To get detailed information on any particular command just append the name of the command as follows:
docker help run
This command will show you details of what the run command does.
The run command creates a writable container layer over the specified image. Here’s the syntax for this command:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
The docker run command has to specify a container image to derive the container from. You can add to or override the image defaults with options for the run command. You can do things like run the container as a foreground or background process, set restart policies, configure network settings, and enforce limits on how much resources the container can consume.
The docker pull command downloads an image from a repository like Docker Hub. You may opt for a private registry like Quay.io, but the pull command stays the same.
docker pull [OPTIONS] image_name:tag
By default, all registries scan any downloaded image to check for commonly known vulnerabilities. Using docker pull you can skip this security scan, although, you may never need to as it’s good practice to scan all downloaded images.
The pull command pulls a single image, but you can set it to pull multiple images using the -a tag. Additionally, while the pull command is running, you can still cancel it using a ctrl+C.
For developers, especially those that are part of a large team that practices DevOps, the process of ‘committing’ small changes in features is essential to building great software. Docker enables this model of frequent, small updates and has even provided a docker commit command just for this purpose.
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Once you’ve made changes to a container image and you’d like to publish your changes, you can use the docker commit command.
Once downloaded, if you want to remove a certain image you can use the rmi command. You can remove one or more images using the following syntax:
docker rmi [OPTIONS] IMAGE [IMAGE...]
By default rmi deletes the image and all parent images as well, if you want to keep untagged parent images you can append the --no-prune option.
The rm command removes a container, and uses the following format:
docker rm [OPTIONS] CONTAINER [CONTAINER...]
If the container has already stopped, you can run this command as is. But if the container is still running as a daemon, you need to append the -f option to force remove the container.
The inspect command helps you inspect an image or container and view metadata about them.
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
The inspect command helps you monitor container health, environment variables and more. It retrieves vital information like the Tag, OS Version, IP address, MAC address, and more. To pull specific information about containers you could use the -f argument. The results are displayed in JSON format.
docker stop & docker kill
Working with containers means you’ll have to constantly end processes and terminate containers. The docker stop command stops processes running in a container and gives the container a grace period before terminating it.
docker stop [OPTIONS] CONTAINER [CONTAINER...]
The docker kill command terminates the container immediately.
docker kill [OPTIONS] CONTAINER [CONTAINER...]
When you manually stop or kill containers, or if they are compromised, or if they fail while running, you’d want to restart them to keep your applications resilient. In this case, the docker restart command does the job.
docker restart [OPTIONS] CONTAINER [CONTAINER...]
There are other ways to restart containers like systemd, but this is the method Docker recommends. Another effective way to restart containers is using a Kubernetes restart policy.
When you want to view all your containers in a single view, the docker ps command is the one to use. It presents a systematic record of all containers, both running and exited on the host.
docker restart [OPTIONS] CONTAINER [CONTAINER...]
You can use options to show a list of containers created recently, or view their file sizes. This is a great way to “take stock of your inventory” with containers and see what you have running at any given moment.
These are the most commonly used Docker commands, and they help you perform the most essential functions on a day-to-day basis. To make the most of these commands you should be familiar with each command’s options. However, they’re not the entire list. You can view the full list on the Docker documentation.
As you work with Docker containers, they make development much easier, and more powerful. There are Docker commands for everything from pulling an image, to inspecting it, and committing it. The key to this is mastering the Docker CLI and the various commands you can use. This includes the options which are unique for each command. One of Docker’s strength is its simple yet powerful CLI, and we’ve seen proof of that in this post.