Docker is a powerful (very powerful) containerization platform that allows you to package, distribute, and run applications in isolated environments. Here's the only Docker commands cheat sheet you'll ever need to navigate and manage Docker containers and images efficiently.
Remember that Docker offers a wide range of commands and options, and this Docker commands cheat sheet provides a starting point for your Docker journey.
As you delve deeper into Docker, exploring the official Docker documentation will provide more detailed information and examples.
Some of the things you can do using these docker commands:
- create, start, stop, and remove containers,
- pull and push images to registries,
- interact with running containers,
- inspect container information,
- manage networks and volumes, and
- utilize Docker Compose for multi-container applications.
Tip: Bookmark this blog to keep coming back to these commands as and when you need them.
Let start this cheatsheet of Docker CLI commands with Docker Hub a.k.a registry.
- Docker Commands Cheat Sheet for Registry & Repository
- Docker Commands List for Images
- Docker Container Commands Cheat Sheet
- Docker Container Commands List
- Docker Build Commands List
- Docker Commands Cheat Sheet for Cleanup
- Docker Networking Commands Cheat Sheet
- Docker Volumes Commands List
- Docker Commands Cheat Sheet - Orchestrate
- Docker Commands Cheat Sheet for Services
- Docker Compose Cheat Sheet
- Docker Plugin Management Commands
Docker Commands Cheat Sheet for Registry & Repository
The hosted collection of images used to build the file system for containers is referred to as a repository. The host that houses the repositories and offers an HTTP API to control them is referred to as the registry.
Numerous thousands of repositories are housed in Docker's central registry. To prevent security risks, you should check the pictures from this registry before using them.
docker login: Logs in to a Docker Hub
docker login <registry>
docker logout: Logs out from Docker Hub
docker logout <registry>
docker search: Searches Docker Hub for images.
docker search <search_term>
docker pull: Pulls an image from Docker Hub to the local machine.
docker pull <image_name>
docker push: Pushes an image to Docker Hub from the local machine.
docker push <image_name>
Docker Commands List for Images
Images can be thought of as models for the Docker containers. To interact with the images, issue the following commands:
docker images: Lists available images on the local machine.
docker import: Imports the contents of a tar archive as a new Docker image.
docker import <file.tar> <new_image_name>
docker build: Builds docker image from Dockerfile.
docker build -t <image_name> <directory>
docker commit: Creates a new image from changes made to a container.
docker commit <container_id> <new_image_name>
docker rmi: Removes one or more images.
docker rmi <image_id>
docker load: Loads an image from a tar archive (read from STDIN by default).
docker load < <file.tar>
docker history: Displays the history of an image.
docker history <image_name>
docker tag: Tags the image to a name.
docker load < my_image.tar.gz: Loads an image from the mentioned file along with its history.
docker save: Saves one or more images to a tar archive (streaming to STDOUT by default).
docker save <image_name> > <file.tar>
cat my_container.tar.gz | docker import - my_image:my_tag: Imports the container as an image from the mentioned file without its history; thus, the file size is small.
docker export: Exports the contents of a container's filesystem as a tar archive.
docker export <container_id> > <file.tar>
Also Read: Docker Image vs Docker Container
Docker Container Commands Cheat Sheet
Containers are Docker processes that are isolated and contain the code to be executed.
docker create: Creates a container without starting it.
docker create [options] IMAGE
-a, --attach # attach stdout/err
-i, --interactive # attach stdin (interactive)
-t, --tty # pseudo-tty
--name NAME # name your image
-p, --publish 5100:5100 # port map (host:container)
--expose 5412 # expose a port to containers
-P, --publish-all # publish all ports
--link container:alias # linking
-v, --volume `pwd`:/app # mount (absolute path is needed here)
-e, --env NAME=help # env vars
docker rename: Use this command to rename a container.
docker rename <container_id> <new_name>
docker run: Creates and starts a new container from an image.
docker run <image_name>
--detach , -d # Runs a container in the background and prints the container ID
--env , -e # Sets environment variables
--hostname , -h # Sets a hostname to a container
--label , -l # Creates a meta data label for a container
--name # Assigns a name to the container
--network # Connects a container to a network
--rm # Removes container when it stops
--read-only # Sets the container filesystem as read-only
--workdir , -w # Sets a working directory in a container
docker rm: Removes one or more containers.
docker rm <container_id>
docker update: Updates the configuration of one or more containers.
docker update --cpus 2 --memory 1g <container_id>
docker run -td container_id: Keeps the container running, -t allocates a pseudo-TTY session, and -d detaches the container automatically.
docker run --rm: Removes the container once it stops.
docker run -v $HOSTDIR:$DOCKERDIR: Maps a directory on the host to a docker container.
docker rm -v: Removes the volumes associated with the container.
docker run --log-driver=syslog: Docker 1.10 comes with the logging driver for each container, and it will run docker with a custom log driver.
docker start: Starts one or more stopped containers.
docker start <container_id>
docker stop: Stops one or more running containers.
docker stop <container_id>
docker restart: Stops and starts a container.
docker restart <container_id>
docker pause: Pauses all processes within a container.
docker pause <container_id>
docker unpause: Unpauses a paused container.
docker unpause <container_id>
docker wait: Blocks until the running container stops.
docker kill: Sends a signal to terminate a running container abruptly.
docker kill <container_id>
docker attach: Attaches to a running container's input/output (stdin/stdout).
docker attach <container_id>
docker run- it -c 512 <container>: It lets you limit the CPU, either using a percentage of all CPUs or by using specific cores. 1024 means 100% of the CPU, so if you want the container to take 50% of all CPU cores, you should specify 512.
docker run -it --cpuset-cpus=0,4,6 <container>: CPU cores using cpuset-cpus.
docker run -it -m 300M ubuntu:14.04 /bin/bash: For setting memory constraints on Docker.
docker run --rm -it --cap-add SYS_ADMIN --device /dev/fuse sshfs: Setting Linux capabilities using cap-add. It lets you mount a FUSE-based filesystem, and you need to combine both --cap-add and --device
docker run -it --device=/dev/ttyUSB0 debian bash: Providing access to a single device.
docker run -it --privileged -v /dev/bus/usb:/dev/bus/usb debian bash: For providing access to all devices.
docker ps: Displays all the running containers.
docker ps -a: Displays the running and stopped containers.
docker logs: Fetches and displays the logs of a container. (A custom log driver can be used, but logs are only available for json-file and journald in 1.10).
docker logs <container_id>
docker inspect: Displays all the details about a container or image.
docker inspect <container_id>
docker events <container>: Displays real-time events from the Docker daemon.
docker port: Displays the public-facing port of the container.
docker top: Displays the running processes of a container whose id is provided.
docker top <container_id>
docker stats: Displays a live stream of resource usage statistics for containers.
docker diff: Displays the changed files in the container's FS.
docker stats --all: Displays a list of all containers and --default shows just running.
docker cp: Copies files or directories between a container and the local filesystem.
docker cp <container_id>:<source_path><destination_path>
docker exec: Runs a command in a running container.
docker exec <container_id> <command>
-d, --detach # run in background
-i, --interactive # stdin
-t, --tty #interactive
docker exec -it foo /bin/bash: Enters into a running container, and attaches a new shell process to a running container called foo.
docker commit <container> <image>: Commits a new docker image.
docker version: Displays the Docker version information.
docker info: Displays system-wide information about Docker.
docker buildx: Builds and pushes multi-platform Docker images.
docker buildx build --platform linux/amd64,linux/arm64 -t <image_name>
docker system logs: Displays system-wide logs.
Docker Container Commands List
The following commands help you interact with the container.
docker exe -ti <container_name> <command.sh>: Runs a command in the container.
docker logs -ft <container name>: Follows the container log.
docker commit -m “commit message” -a “author” <container_name> <username/image_name>: tag: Saves the running container as an image.
Docker Build Commands List
The following commands build the images from a Dockerfile.
docker build -t myapp :1.0: Builds an image from the Dockerfile and tags it.
docker images: Lists all the images that are locally stored.
docker rmi alpine: 3.4: Deletes an image from the Docker Store.
Docker Commands Cheat Sheet for Cleanup
To maintain performance, you must clean up the resources on a regular basis to optimize their use.
To clean up Docker resources, use the following commands.
docker image prune: Cleans an unused/dangling image
docker image prune -a: Removes an image not used in a container.
docker system prune: Removes unused resources, such as stopped containers and dangling images, to free up disk space.
docker system df: Shows disk usage of Docker
docker swarm leave: Leaves a swarm.
docker stack rm <stack_name>: Removes a swarm.
docker rm $(docker ps -a -q): Deletes all stopped containers
docker rmi $(docker images -q): Deletes all images.
Docker Networking Commands Cheat Sheet
Docker includes a network that allows containers to communicate. Docker supports three network interface types: bridge, host, and none.
By default, the new container is launched into the bridge network. A new network is required for launching containers in order to establish communication among them.
It allows containers to communicate while keeping them isolated from other containers that are not connected to the network.
docker network create: Creates a network for containers to communicate with each other.
docker network create <network_name>
docker network rm: Deletes one or more named networks and ensure that no containers are connected to the deleted network.
docker network rm <network_name>
docker network ls: Lists available networks.
docker network inspect: Displays detailed information about a network.
docker network inspect <network_name>
docker network connect: Connects a container to a network.
docker network connect <network_name> <container_id>
docker network disconnect: Disconnects a container from a network.
docker network disconnect <network_name> <container_id>
Docker Volumes Commands List
Docker has free-floating filesystem volumes. As a result, there is no need to be linked to a specific container. For portability, you can use volumes mounted from data-only containers. Docker 1.9.0 includes named volumes, which replace data-only containers.
docker volume create: Creates a named volume for persistent storage.
docker volume create <volume_name>
docker volume ls: Lists available volumes.
docker volume inspect: Displays detailed information about a volume.
docker volume inspect <volume_name>
docker volume rm: Removes one or more volumes by specifying volume name(s).
docker volume rm <volume_name>
Docker Commands Cheat Sheet - Orchestrate
Orchestration manages the life cycle of containers, which is especially important in dynamic environments. It can be used to control and automate a variety of container tasks.
Docker Swarm, Kubernetes, and Mesos are the most commonly used orchestration tools among a long list of Docker orchestration tools. Docker Swarm commands are used in this Docker commands cheat sheet.
docker swarm init --advertise-addr 10.1.0.3: Initialize the swarm mode and listen to a specific interface.
docker swarm join --token<manager-token> 10.1.0.2:2377: Joins an existing swarm as a manager node.
docker swarm join --token<worker-token> 10.1.0.2:2377: Joins a swarm as a worker node.
docker node ls: Lists all the nodes in the swarm.
docker service create --replicas 4 -p 81:81 name -webio: Creates a service from an image on the existing port and deploys three instances.
docker service ls: Lists services running in a swarm.
docker service scale web=3: Scales the service.
docker service ps web: Lists the tasks of service.
Docker Commands Cheat Sheet for Services
Let's look at the commands for viewing the running services, running the services, viewing all service logs, and scaling the services.
docker service ls: Lists all services running in a swarm.
docker stack services <stack_name>: Displays all running services.
docker service logs <stack_name> <service_names>: Displays all service logs.
docker service scale <stack_name> <service_name>= replicas: Scales a service across qualified nodes.
Also Read: What is Helm?
Docker Compose Cheat Sheet
Docker Compose is a tool for creating and running multi-container Docker applications. It allows you to configure your application services using a YAML file.
You can easily create and start all of the services from your configuration using the following commands.
docker-compose start: Starts the container.
docker-compose stop: Stops the container.
docker-compose unpause: Unpauses the container.
docker-compose ps: Lists all the containers.
docker-compose build: Builds or rebuilds services defined in a Compose file.
docker-compose up: Builds, (re)creates, and starts containers defined in a Compose file.
docker-compose down: Stops and removes containers, networks, and volumes defined in a Compose file.
docker-compose -f <docker-compose-file> up: Starts up your application
docker-compose logs: Displays logs from containers defined in a Compose file.
docker-compose exec: Runs a command in a service container defined in a Compose file.
docker-compose exec <service_name> <command>
Docker Plugin Management Commands
Docker plugins extend the functionality of Docker and allow users to connect to other popular services. The commands listed below enable you to add, manage, and remove plugins from your system.
docker plugin enable <plugin>: Enables a Docker plugin
docker plugin disable <plugin>: Disables a Docker plugin
docker plugin create <plugin> <path-to-data>: Creates a plugin from config.json and rootfs
docker plugin inspect <plugin>: View details about a plugin
docker plugin rm <plugin>: Removes a plugin.
Docker Commands List - Final Thoughts
Docker commands are essential for managing containers and images within the Docker ecosystem.
This Docker commands cheat sheet provides a handy reference for some of the most commonly used ones.
With this cheat sheet at your disposal, you'll be well-equipped to navigate and manage Docker containers and images efficiently, enabling you to leverage the power of containerization for your applications.
Happy cheat coding!