Docker cheat sheet

GitBook docs section concerning useful cheat sheet for Docker.

Docker is a containerization tool that enables you to deploy and configure services in isolated environments. To manage various aspects of this platform and the services running on top of it, you mainly use various Docker commands.

There are different Docker commands for specific tasks. Based on their purpose, they fall into these categories:

  • Build commands. Create an image from a Dockerfile.

  • Cleanup commands. Remove unused images and volumes to free up space.

  • Container interaction commands. Manage and communicate with containers.

  • Container inspection commands. Analyze and check the details of containers.

  • Image management commands. Administer images.

  • Run commands. Build a container from an image and change its settings.

  • Registry commands. Interact with a remote Docker image registry, like Docker Hub.

  • Service commands. Manage all aspects of Docker services.

  • Network commands. Configure, manage, and interact with the Docker network.

Build commands

The build command in Docker is used to build images from a Dockerfile. There are some variations of this command used for different tasks, with the most common ones including:

Command
Explanation

docker builddocker build

Builds an image from a Dockerfile in the current directory

docker build https://github.com/docker/rootfs.git#container:docker

Builds an image from a remote Git repository

docker build -t imagename/tag

Builds and tags an image for easier tracking

docker build https://yourserver/file.tar.gz

Builds an image from a remote tar archive

docker build -t image:1.0 -<<EOF FROM busybox RUN echo "hello world"EOF

Builds an image via a Dockerfile that is passed through STDIN or standard input

Cleanup commands

The cleanup commands, as the name suggests, remove unused images, containers, and volumes to keep your system clean, as well as freeing up storage space. Here are operations you can do with it:

Command
Explanation

docker image prune

Deletes dangling or untagged images

docker image prune -a

Clears all images that aren’t being used by containers

docker system prune

Removes all stopped containers, all networks not used by containers, all dangling images, and all build cache

docker image rm image-name

Removes a specific image by name

docker rm container

Removes a running container

docker swarm leave

Leaves a Swarm

docker stack rm stackname

Deletes a Swarm

docker volume rm $(docker volume ls -f dangling=true -q)

Removes all dangling volumes

docker rm $(docker ps -a -q)

Clears all stopped containers

docker kill $(docker ps -q)

Stops all running containers

Container interaction commands

The container interaction commands are used to build and manage applications running on isolated environments. Here are some of the most common ones:

Command
Explanation

docker start container

Starts a new container

docker stop container

Stops a container

docker pause container

Pauses a container

docker unpause container

Unpauses a container

docker restart container

Restarts a container

docker export container

Exports container contents to a tar archive

docker attach container

Attaches to a running container

docker wait container

Waits until the container is terminated and shows the exit code

docker commit -m “commit message” -a "author" container username/image_name: tag

Saves a running container as an image

docker logs -ft container

Follows container logs

docker exec -ti container script.sh

Runs a command in a container

docker commit container image

Creates a new image from a container

docker create image

Creates a new container from an image

Container inspection commands

The container inspection commands are helpful for checking details of the isolated environments, which is commonly done for quality assurance or troubleshooting purposes. Here are some commands that help you get an overview of what different containers are doing:

Command
Explanation

docker ps

Lists all running containers

docker ps -a

Lists all containers, regardless of their statuses

docker diff container

Inspects changes to directories and files in the container filesystem

docker top container

Shows all running processes in an existing container

docker inspect container

Displays low-level information about a container

docker logs container

Gathers the logs for a container

docker stats container

Shows container resource usage statistics

Image management commands

The image management commands enable you to modify and inspect images that will be used to build containers. The most common utilities for this purpose include:

Command
Explanation

docker image ls

Lists images

docker image rm mysql

Removes an image

docker tag

Tags an image

docker history image

Displays the image history

docker inspect image

Displays low-level information about an image

Run command

The run command in Docker is used to create containers from provided images. The default syntax for this utility looks like:

You can add the following flags to modify the command behavior and build containers using specific settings:

Flag
Explanation

--detach , -d

Runs the container in the background and prints the container ID

--env , -e

Sets environment variables for the container

--hostname , -h

Gives the container a specific hostname

--label , -l

Creates a metadata label for the container

--name

Assigns a name to a container

--network

Connects the container to a Docker network

--rm

Removes container when it stops

--read-only

Sets the container filesystem as read-only

--workdir , -w

Sets a working directory in the container

Registry commands

Docker registry commands enable you to interact with remote image repositories, such as the official Docker Hub or a self-hosted registry running on a private server:

Command
Explanation

docker login

Logs in to a registry

docker logout

Logs out from a registry

docker pull mysql

Pulls an image from a registry

docker push repo/rhel-httpd:latest

Pushes an image to a registry

docker search term

Searches Docker Hub for images with the specified term

Service commands

The service commands are used to manage a Docker Swarm service — an image for a microservice within the context of a larger application. These include:

Command
Explanation

docker service ls

Lists all services running in a swarm

docker stack services stackname

Lists all running services

docker service ps servicename

Lists the tasks of a service

docker service update servicename

Updates a service

docker service create image

Creates a new service

docker service scale servicename=10

Scales one or more replicated services

docker service logs servicename

Lists all service logs

Network commands

The network commands are used to manage your Docker application’s network, which defines how containers communicate with each other and external systems. Here are some of the most common ones:

Command
Explanation

docker network create network-name

Creates a new network

docker network rm network-name

Removes a specified network

docker network ls

Lists all networks

docker network connect network-name container

Connects a container to a network

docker network disconnect network-name container

Disconnects a container from a network

docker network inspect network-name

Displays detailed information about a network

Last updated