Docker commands - Quick reference guide
Docker is fantastic tool for building out your infrastructure, however it does have a fairly steep learning curve. That’s why I created this Docker Cheat Sheet. I was constantly looking up what docker commands I needed to run to build an image from a dockerfile, run a container, mount a volume, etc.
Table of Contents
- What is docker?
- Why docker?
- Registries and Repositories
- Create,Run,Update and Delete containers
- Start and stop containers
- Cleanup commands
- Docker Hub
- Other Commands
- Docker Compose
- Docker Swarm
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.
Docker is useful to automate the deployment of applications inside a software containers, which makes the applications easy to ship and run virtually anywhere (i.e, platform independent). The Docker container processes run on the host kernel, unlike VM which runs processes in guest kernel.
The docker desktop downloads are available for windows, mac and linux distributions.
It supports for Windows 10 64-bit: Pro, Enterprise, or Education (Build 15063 or later) editions. You need to follow the below steps for installation.
- Download docker desktop for windows from https://docs.docker.com/docker-for-windows/install/
Docker Desktop Installer.exeto run the installer.
- Make sure
Enable Hyper-V Windows Featuresoption is selected
- Download docker desktop for mac from https://docs.docker.com/docker-for-mac/install/
Docker.dmgto open the installer and drag it to the Applications folder.
Docker.appin the Applications folder to start Docker.
You can install from a package easily
Go to https://download.docker.com/linux/ubuntu/dists/, choose your Ubuntu version and then go to pool/stable/ to get .deb file
Install Docker Engine by referring the downloaded location of the Docker package.
$ sudo dpkg -i /path/to/package.deb
Verify the Docker Engine by running the
hello-worldimage to check correct installation.
$ sudo docker run hello-world
Docker Registry is a service that stores your docker images. It could be hosted by a third party, as public or private registry. Some of the examples are,
- Docker Hub,
- Google Container Registry,
- AWS Container Registry
A Docker Repository is a collection of related images with same name which have different tags. These tags are an alphanumeric identifiers(like 1.0 or latest) attached to images within a repository.
For example, if you want to pull golang image using
docker pull golang:latest command, it will download the image tagged latest within the
golang repository from the Docker Hub registry. The tags appeared on dockerhub as below,
Login to a registry
> docker login [OPTIONS] [SERVER]
Logout from a registry
> docker logout [SERVER]
Search for an image in registry
docker search [OPTIONS] TERM
This command pulls an image or a repository from a registry to local machine
docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
This command pushes an image to the registry from local machine.
docker image push [OPTIONS] NAME[:TAG]
Create a new container
docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]
Rename a container
docker container rename CONTAINER NEW_NAME
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
You can also run a command inside container
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Update configuration of one or more containers
docker container update [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
docker container rm [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
docker container start [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers
docker container stop [OPTIONS] CONTAINER [CONTAINER...]
Restart one or more containers and processes running inside the container/containers.
docker container restart [OPTIONS] CONTAINER [CONTAINER...]
Pause all processes within one or more containers
docker container pause CONTAINER [CONTAINER...]
Unpause all processes within one or more containers
docker container unpause CONTAINER [CONTAINER...]
Kill one or more running containers
docker container kill [OPTIONS] CONTAINER [CONTAINER...]
Block until one or more containers stop and print their exit codes after that
docker container wait CONTAINER [CONTAINER...]
Docker provides network commands connect containers to each other and to other non-Docker workloads. The usage of network commands would be
docker network COMMAND
List down available networks
docker network ls
You can connect a container by name or by ID to any network. Once it connected, the container can communicate with other containers in the same network.
docker network connect [OPTIONS] NETWORK CONTAINER
You can disconnect a container by name or by ID from any network.
docker network disconnect [OPTIONS] NETWORK CONTAINER
Removes one or more networks by name or identifier. Remember, you must first disconnect any containers connected to it before removing it.
docker network rm NETWORK [NETWORK...]
It is possible to create a network in Docker before launching containers
docker network create [OPTIONS] NETWORK
The above command will output the long ID for the new network.
You can see more details on the network associated with Docker using network inspect command.
docker network inspect networkname
You may need to cleanup resources (containers, volumes, images, networks) regularly.
docker system prune
$ docker images
$ docker ps
$ docker volume rm $(docker volume ls -qf dangling=true)
$ docker network ls
Docker Hub is a cloud-based repository provided by Docker to test, store and distribute container images which can be accessed either privately or publicly.
Dockerfile is a text document that contains set of commands and instructions which will be executed in a sequence in the docker environment for building a new docker image.
It initializes a new image and sets the Base Image for subsequent instructions. It must be a first non-comment instruction in the Dockerfile.
digest are optional. If you omit either of them, the builder assumes a latest by default.
RUN instruction allows you to install your application and packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. It is quite common to have multiple RUN instructions in a Dockerfile.
It has two forms
Shell Form: RUN
RUN npm start
Exec form RUN [“
“, “ “, “ “]
RUN [ "npm", "start" ]
An ENTRYPOINT allows you to configure a container that will run as an executable. It is used to run when container starts.
If an image has an ENTRYPOINT and pass an argument to it while running the container, it wont override the existing entrypoint but it just appends what you passed with the entrypoint. To override the existing ENTRYPOINT. you should user
–entrypoint flag for the running container.
Let’s see the behavior with the above dockerfile,
CMD instruction is used to set a default command, which will be executed only when you run a container without specifying a command. But if the docker container runs with a command, the default command will be ignored.
The CMD instruction has three forms,
1. Exec form:
The main purpose of the CMD command is to launch the required software in a container. For example, running an executable .exe file or a Bash terminal as soon as the container starts.
Remember, if docker runs with executable and parameters then CMD instruction will be overridden(Unlike ENTRYPOINT).
docker run executable parameters
Note: There should only be one CMD command in your Dockerfile. Otherwise only the last instance of CMD will be executed.
The COPY instruction copies new files or directories from source and adds them to the destination filesystem of the container.
The ADD instruction copies new files, directories or remote file URLs from source and adds them to the filesystem of the image at the destination path. The functionality is similar to COPY command and supports two forms of usage,
ADD commands provides additional features such as downloading remote resources, extracting TAR files etc.
1. Download an external file and copy to the destination
The ENV instruction sets the environment variable
- The first form,
ENV, will set a single variable to a value.
- The second form,
ENV, allows for multiple variables to be set at one time.
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. i.e, It helps in inter-container communication. You can specify whether the port listens on TCP or UDP, and the default is TCP.
But if you want to bind the port of the container with the host machine on which the container is running, use -p option of
docker run command.
docker run -p
The WORKDIR command is used to define the working directory of a Docker container at any given time for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
The LABEL instruction adds metadata as key-value pairs to an image. Labels included in base or parent images (images in the FROM line) are inherited by your image.
You can view an image’s labels using the
docker image inspect --format='' myimage command. The output would be as below,
The MAINTAINER instruction sets the Author field of the generated images.
This command is deprecated status now and the recommended usage is with LABEL command
The VOLUME instruction creates a mount point with the specified name and mounted volumes from native host or other containers.
Docker compose(or compose) is a tool for defining and running multi-container Docker applications.
Docker Swarm(or swarm) is an open-source tool used to cluster and orchestrate Docker containers.