Docker Cheat Sheet
I first wrote about Docker in 2014. Since that time, Docker has become an integral part of my development workflow.
The goal of this article is to highlight common Docker commands (acting as a cheat sheet). I have not provided a detailed description of every command, therefore please refer to the official Docker documentation.
Docker Start:
sudo systemctl start docker
This command will launch docker on Linux.
Docker Information Commands:
docker version
docker ps ax
docker system info
docker df
docker system prune
With prolonged usage, it is likely that your Docker installation will become cluttered (old containers, etc.)
Two useful housekeeping commands include docker system info
, which provides a summary of your environment, as well as docker system prune
, which will reclaim space by removing all unused networks, containers, images, and volumes.
Docker Container Commands:
docker container ls
docker container ls -a
docker container run <img name>
docker container run -it <img name>
docker container rm <container id>
docker container start <container id>
docker container stop <container id>
docker container kill <container id>
docker container exec <container id>
docker container attach <container id>
docker container logs <container id>
docker container inspect <container id>
docker container inspect <container id> | grep <keyword>
docker container commit <container id> <img name>:<img tag>
docker container -v <local dir>:<container dir> <img name>
Unsurprisingly, docker container
commands are the most common. Do not underestimate the value of the docker container inspect
commands, especially when used with grep
and --format
flag.
The container commit
command can also be valuable, allowing you to save all changes from a containers read/write layer to the read-only image layer, therefore creating a new image.
Finally, the docker container -v
command creates a new container, which includes a mount point from your local machine. This is very useful for local development, allowing you to map a local directory to a directory within the container.
Docker Image Commands:
docker image ls
docker image build -t <img name>:<img tag> .
docker image rm <img id>
The docker image build -t
command will build a Docker image from a Dockerfile. Ensure you navigate to the correct directory before running the command.
Docker Volume Commands:
docker volume create <vol name>
docker volume inspect <vol name>
docker volume rm <vol name>
The docker volume inspect
command provides an easy way to locate the volume mount point.
Docker Networking Commands:
docker network ls
docker network inspect <network name>
docker container run -d -p <host port>:<container port> <img name>
docker container port <container id>
docker container run -d -P <img id>
The docker container port
command can be used to verify any port mappings.
The -P
flag can be used to map all ports mentioned in the EXPOSE directive of a Dockerfile.
Docker Logging Commands:
docker system events
docker container logs <container id>
docker container logs -f <container id>
The docker container logs -f
follows the logs, which can be useful when troubleshooting.
Docker Compose Commands:
docker-compose up
docker-compose down
docker-compose ps
Docker Compose facilitates multi-container design on a single node, using a docker-compose.yml
file.
Docker Swarm Commands:
docker swarm init
docker node ls
docker swarm join-token worker
docker swarm join
docker node promote <node name>
docker node demote <node name>
Docker Swarm facilitates multi-node design. The docker swarm join-token worker
command provides the full join command that can be used on other nodes.
Docker Hub Commands:
docker login
docker image push <docker id>/<img name>:<img tag>
docker image pull <docker id>/<img name>:<img tag>
docker image tag <img name>:<img tag> <usr>/<img name>:<img tag>
Pushing and pulling images from the Docker Hub is very similar to GitHub, using the docker image push
and docker image pull
commands.
If you need to push a root image to your Docker ID account, use the docker image tag
command.
Dockerfile Statements:
- FROM = Base image to start from (usually the OS).
- RUN = Run a command in the environment.
- CMD and ENTRYPOINT = Define default behaviour.
- COPY and ADD = Copy files into the container.
- ENV = Define environment variables inside container.
- ARG = Define build-time argument (set with –build-arg).
- VOLUME = Create a volume.
- EXPOSE = Ports to expose outside of the container.
It is possible to create multi-stage builds, which include multiple FROM
statements.
When creating a Dockerfile, I recommend using the Exec format (not Shell), as Shell will launch a new primary process. For example ENTRYPOINT ["sudo", "-u", java", ...]
.