Docker Cheat Sheet
An image is a read-only template with instructions for creating a Docker container.
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
A container is a runnable instance of an image. You can create, start, stop, move, or delete a container. A container is a process which runs on a host. ...the container process that runs is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.
Listing
- List images:
docker image ls
— OR —
docker images - List containers:
docker container ls -a
— OR —
docker ps -a - List volumes:
docker volume ls - List networks:
docker network ls - List all containers (Compose):
docker-compose ps
Removing
- Remove a container:
docker rm my_container - Remove all stopped containers:
docker container prune - Remove an image:
docker image rm my-image
— OR —
docker rmi my-image - Remove dangling images:
docker image prune - Remove all images:
docker image prune -a - Remove a volume:
docker volume rm my_volume - Remove all volumes:
docker volume prune - Remove all build cache objects:
docker builder prune -a - Remove all unused containers, networks, and images (both dangling and unreferenced):
docker system prune - Remove everything:
Docker icon -> Troubleshoot -> Reset to factory defaults - Stop and remove containers and networks in
docker-compose.yml. (This does not remove volumes.)
docker-compose down
Pulling images
- Pull and image from the Docker registry (hub.docker.com):
docker pull my-image
Publishing images
See also: Get started with Docker - Share you image
- Login to the Docker registry (hub.docker.com):
docker login - Tag an image:
docker tag my-image my-username/my-repo:my-tag - Push an image to the Docker registry:
docker push my-username/my-repo:my-tag
Building images from Dockerfiles
- Build an image from a Dockerfile in the current directory:
docker build -t my-image . - Build an image using a differently-named Dockerfile:
docker build -f Dockerfile-other -t my-image . - Rebuild all images (Compose):
docker-compose build - Rebuild a specific image (Compose):
docker-compose build my_service- where
my_serviceis one of the services listed in thedocker-compose.ymlfile
- where
Creating containers
- Create a new container from an image:
docker create my-image - Build new images and create all containers (Compose). (This will not rebuild images if a Dockerfile changes.)
docker-compose up --no-start
Starting / stopping containers
- Start a container:
docker start my_container - Stop a container:
docker stop my_container - Start all containers (Compose):
docker-compose start - Stop all containers (Compose):
docker-compose stop
Running containers
docker run is a combination of (optionally) docker pull, docker create, and docker start. See also Docker run reference.
- Create a container from
my-image, and run the default command:
docker run my-image - Run the
echocommand in the container instead of the default command:
docker run my-image echo "hello" - Run container in the background:
docker run -d my-image - Run and remove the container after it exits:
docker run --rm my-image - Create a container from
my-image, namedmy_container, and start it:
docker run --name my_container my-image - Run a container, setting an environment variable in the container:
docker run --env MY_ENVIRONMENT_VAR=myvalue my-image - Build new images, create all containers, and start all containers (Compose). (This will not rebuild images if a Dockerfile changes.)
docker-compose up - Build, create, and start all in the background (Compose):
docker-compose up -d - Rebuild all images, create all containers, and start all containers (Compose):
docker-compose up --build - Create a new container for
my_serviceindocker-compose.ymland run theechocommand instead of the specified command:
docker-compose run my_service echo "hello"
volumes
- Run a container with a volume named
my_volumemounted at/my/pathin the Docker container. (The volume will be created if it doesn't already exist.) See the Volumes documentation for more information.
docker run --mount source=my_volume,target=/my/path my-image
— OR —
docker run -v my_volume:/my/path my-image - Copy all data from a volume named
my_volume_1to a volume namedmy_volume_2by running thecpcommand in a temporary container created from adebianimage1:
docker run --rm -it -v my_volume_1:/from -v my_volume_2:/to debian bash -c "cp -av /from/. /to"
— OR —
usingrsync(after installing it first):
docker run --rm -it -v my_volume_1:/from -v my_volume_2:/to debian bash -c "apt update && apt install -y rsync && rsync -avz /from/ /to/"
ports & networking
- Run and bind port 80 inside Docker to port 9090 on the host (outside Docker):
docker run -p 9090:80 my-image - Run and access
localhoston the host (outside Docker) using the special DNS namehost.docker.internal. Requires Docker 18.03 or greater. (Before 18.03,docker.for.mac.localhostwas available on Mac only.) See https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds
docker run my-image ping host.docker.internal - Create a network:
docker network create my_network
Run a container using a network:
docker run --network=my_network my-image
Interacting with containers
- Interact with a running container using bash:
docker exec -it my_container bash - Show logs for a container:
docker logs -f my_container - Copy
my-file.txtfrom the host current directory to the/tmpdirectory inmy_container:
docker cp ./my-file.txt my_container:/tmp/my-file.txt - Interact with a running container using bash (Compose):
docker-compose exec my_service bash - Show all logs (Compose):
docker-compose logs -f
Getting information
- Show the Docker version:
docker version - Show the commands used to build an image:
docker history my-image - Show I/O, CPU, and Memory usage for a container:
docker stats my_container - Show files that have changed in a container:
docker diff my_container - Show running processes in a container:
docker top my_container - Show a JSON blob of information about a image, container, volume, etc:
docker inspect my-image
docker inspect my_container
etc. - Show all running processes (Compose):
docker-compose top
* docker-compose commands are shaded in gray. They assume a docker-compose.yml file in the current directory.
See also
- Reference: Cloning Docker data volumes. See also: Docker volumes guide: Backup, restore, or migrate data volumes. [back]