Docker Cheat Sheet
Docker Cheat Sheet
View the help index, a reference of all the supported commands. B uild the Dockerfile at the docker/Dockerfile App path. _
image:latest.
docker run -it example-image:latest
docker inspect <container>
Attaches your terminal’s input stream and a TTY to the container. Use this
command to run interactive commands inside the container. O btain all the information Docker holds about a container, in J SON format.
docker run --name my-container example-image:latest docker kill <container>
N ames the new container my-container. S end a SI KILL signal to the foreground process running in a container, to
G
force it to stop.
docker run --hostname my-container example-image:latest
docker rename <container> my-container
Set the container’s hostname to a specific value (it defaults to the container’s
name). R ename a specified container to my-container.
docker run --env foo=bar example-image:latest docker pause <container> and docker unpause <container>
S et the value of the foo environment variable inside the container to bar. P ause and unpause the processes running within a specific container.
docker run --env-file config.env example-image:latest docker stop <container>
Populate environment variables inside the container from the file config.env. S top a running container.
The file should contain key-value pairs in the format foo=bar.
docker start <container>
docker run -p 8080:80 example-image:latest
S tart a previously stopped container.
B ind port 8080 on your Docker host to port 80 inside the container. It allows
you to visit localhost:8080 to access the network service listening on port 80 docker rm <container>
inside the container.
Delete a container by its ID or name. Use the -f (force) flag to delete a
docker run -v /host-directory: /container-directory example-image:latest container that’s currently running.
Bind mount /host-directory on your host to /container-directory inside the
container. The directory’s contents will be visible on both sides of the mount.
docker run -v data:/data example-image:latest
Execute Commands in Containers
M ount the named Docker volume called data to /data inside the container. docker exec my-container demo-command
docker run --network my-network example-image:latest R un demo-command inside my-container; the process’ output will be shown
in your terminal
Connect the new container to the Docker network called my-network.
docker exec -it my-container demo-command
docker run --restart unless-stopped example-image:latest Run a command interactively by attaching your terminal’s input stream and a
Set the container to start automatically when the Docker daemon starts, pseudo-TTY.
unless the container has been manually stopped. Other restart policies are
also supported.
docker run --privileged example-image:latest iew Container Resource Utilization
V
Run the container with privileged access to the host system. This should
usually be disabled to maintain security. docker stats <container>
Stream a container’s resource utilization information into your terminal. The
output includes CPU, memory, and I/O usage, as well as the number of
processes running within the container.
Co py to and rom Containers
F
M ana e olumes
g V
Use Con fig u t ra ion Contexts
docker volume create my-volume
docker context create my-context
--host=tcp: //host: ca= /ca-file cert= /cert-file key= /key-file
docker login
T he --file argument supplies the path to the Dockerfile that was used to
build the image. hen the Dockerfile is available, more detailed vulnerability
W
Login to your account. You’ll be prompted to supply credentials interactively. information is produced.
You must login before you can push images. Logging in also helps you avoid
hitting public pull rate limits. docker scan example-image:latest --severity high
docker logout
O nly report vulnerabilities that are high severity or higher. The --severity flag
also supports low and medium values.
Logs you out of your account
docker search nginx
Clean U Unused Resources
p
S earches Docker Hub for images matching the supplied search term (nginx,
in this example). docker system prune
R emoves unused data, including dangling image layers (images with no
tags).
Docker Compose
docker system prune -a
docker compose ps Extends the prune process by deleting all unused images, instead of only
Displays a list of containers created by Docker Compose. dangling ones.
docker compose stop docker system prune --volumes
Stops all running containers defined in your docker-compose.yml. You can Includes volume data in the prune process. This will delete any volumes that
restart them later using docker compose start. aren’t used by a container.
docker compose restart docker image prune
Restarts all the containers in your stack. R emoves dangling images, without affecting any other types of data.
docker compose down docker image prune -a
Stops and removes containers, networks, and any other resources created by R emoves all unused images.
docker compose up. Volumes are not deleted unless you set the -v or --
volumes flag. docker network prune