Skip to main content

Docker Cheat Sheet

Installation

curl -sSL https://get.docker.com | sh

Docker

Basic Commands

CommandDescription
docker --versionDisplay Docker version.
docker infoDisplay system-wide information.
docker helpDisplay help for a command.
docker statsShow stats of running containers

Container Operations

CommandDescription
docker run <options> <image>Start a container from an image.
docker run --name <name> <image>Specify a container name
docker run -d <image>Start a container in background
docker run -v <hostdir>:<targetdir> <image>Map a local directory into the container when running
docker run -p <hostport>:<targetport> <image>Publish container's port(s) to the host
docker exec -it <container> <command>Execute a command in a running container.
docker exec -it <container> <executable>Start a shell inside a running container
docker commit <container>Create an image out of container
docker cp <container>:<source> <target>Copy a file from a container to the host
docker cp <target> <container>:<source> Copy a file from the host to a container
docker port <container>Show mapped ports of a container
docker ps -aList all containers.
docker psList running containers.
docker rename <old_name> <new_name>Rename a container
docker rm <container>Remove a stopped container.
docker start <container>Start a stopped container.
docker stop <container>Stop a running container.

Image Operations

CommandDescription
docker imagesList images.
docker pull <image>Pull an image from a Docker registry.
docker rmi <image>Remove an image.
docker build -t <tag> .Build an image from a Dockerfile.
docker push <image>Upload an image to a repository
docker image pruneDelete dangling images
docker image prune -aDelete all unused images
docker build <directory>Build an image from a Dockerfile
docker tag <image> <new_image>Tag an image
docker save <image> > <file>Save an image to tar file
docker load -i <tarfile>Load an image from a tar file

Docker Registry

CommandDescription
docker loginLog into a Docker registry.
docker logoutLogout from a Registry
docker tag <source_image>:<tag> <repository>:<tag>Tag an image.
docker push <repository>:<tag>Push an image to a registry.
docker search textSearch for an Image on the Official Registry
docker save -o <repo_tag.tar> <repo>:<tag>Export an Image/Repository as a Tarball

Docker Networks

CommandDescription
docker network lsList networks.
docker network create <name>Create a new network.
docker network connect <network> <container>Connect a container to a network.
docker network disconnect <network> <container>Disconnect a container from a network.
docker network rm <network>Remove a network.

Docker Volumes

CommandDescription
docker volume create <name>Create a volume.
docker volume lsList volumes.
docker volume rm <volume>Remove a volume.

Cleaning Up

CommandDescription
docker system pruneRemove unused data (containers, volumes, networks, and images).
docker container pruneRemove all stopped containers.
docker image pruneRemove unused images.

Dockerfile

CommandDescription
FROM <image>Base image for the build.
MAINTAINER <email>Set the name of the maintainer (metadata).
COPY <source> <destination>Copy files or directories from the context into the container at the specified destination.
ADD <source> <destination>Similar to COPY but can untar archives and accepts HTTP URLs as well.
RUN <args>Execute an arbitrary command inside the container during the build process.
USER <name>Set the default username or user context for subsequent commands.
WORKDIR <path>Set the default working directory for the container.
CMD <args>Set the default command to be executed when the container starts. This can be overridden at runtime.
ENV <name> <value>Set an environment variable with the specified name and value inside the container.

Docker Compose

CommandDescription
docker-compose upStart services defined in a docker-compose.yml file.
docker-compose downStop and remove all services defined in the file.
docker-compose logsView logs from services.

Docker Swarm

CommandDescription
docker service lsList all services in the Swarm
docker stack services <stack_name>See all running services
docker service logs <stack_name> <service_name>See all services logs
docker service scale stack_name_service_name=replicasScale services across nodes
docker image prunePrune unused (dangling) images
docker image prune -aRemove all images (including unused containers)
docker system prunePrune the entire system (including volumes and database info)
docker swarm leaveLeave Swarm
docker stack rm <stack_name>Remove Swarm (deletes all volume data and database info)
docker kill $(docker ps -q)Kill all running containers

Subscribe for product updates

By subscribing, you agree with our Terms and Privacy Policy.