Docker Cheat Sheet

Docker Cheat Sheet #

Cheatsheet for Docker CLI #

Run a new Container #

# Start a new Container from an Image
docker run IMAGE
docker run nginx

# ... and assign it a name
docker run --name CONTAINER IMAGE
docker run --name web nginx

# ... and map a port
docker run -p HOSTPORT:CONTAINERPORT IMAGE
docker run -p 8080:80 nginx

# ... and map all ports
docker run -P IMAGE
docker run -P nginx

# ... and start container in background
docker run -d IMAGE
docker run -d nginx

# ... and assign it a hostname
docker run --hostname HOSTNAME IMAGE
docker run --hostname srv nginx

# ... and add a dns entry
docker run --add-host HOSTNAME:IP IMAGE

# ... and map a local directory into the container
docker run -v HOSTDIR:TARGETDIR IMAGE
docker run -v ${PWD}:/usr/share/nginx/html nginx

# ... but change the entrypoint
docker run -it --entrypoint EXECUTABLE IMAGE
docker run -it --entrypoint bash nginx

Manage Containers #

# Show a list of running containers
docker ps

# Show a list of all containers
docker ps -a

# Delete a container
docker rm CONTAINER
docker rm web

# Delete a running container
docker rm -f CONTAINER
docker rm -f web

# Delete stopped container
docker container prune

# Stop a running container
docker stop CONTAINER
docker stop web

# Copy a file from container to the host
docker cp CONTAINER:SOURCE TARGET
docker cp web:/index.html index.html

# Start a shell inside a running container
docker exec -it CONTAINER EXECUTABLE
docker exec -it web bash

# Rename a container
docker rename OLD_NAME NEW_NAME
docker rename 096 web

# Create an image out of container
docker commit CONTAINER
docker commit web

Manage Images #

# Download an image
docker pull IMAGE[:TAG]
docker pull nginx

# Upload an image to a repository
docker push IMAGE
docker push myimage:1.0

# Delete an image
docker rmi IMAGE

# Show a list of all images
docker images

# Delete dangling images
docker images prune

# Delete all unused images
docker images prune -a

# Build an image from a Dockerfile
docker build DIRECTORY
docker build .

# Tag an image
docker tag IMAGE NEW_IMAGE
docker tag ubuntu ubuntu:20.04

# Build and tag an image from a Dockerfile
docker build -t IMAGE DIRECTORY
docker build -t myimage:1.0 .

# Save an image to .tar file
docker save IMAGE > FILE
docker save nginx > nginx.tar

# Load an image from a .tar file
docker load -i TARFILE
docker load -i nginx.tar

Infos & Stats #

# Show the logs of a container
docker logs CONTAINER
docker logs web

# Show stats of running containers
docker stats

# Show processes of container
docker top CONTAINER
docker top web

# Get detailed info about an object
docker inspect Name
docker inspect nginx

# Show all modified files in container
docker diff CONTAINER
docker diff web

# Show mapped ports of a container
docker port CONTAINER
docker port web

Tips #

Prune #

  • docker system prune
  • docker volume prune
  • docker network prune
  • docker container prune
  • docker image prune

Delete containers after stopping #

docker stop $(docker ps -aq) && docker rm -v $(docker ps -aq)

Containers #

Lifecycle #

Starting and Stopping #

Info #

  • docker ps shows running containers.
  • docker logs gets logs from container. (You can use a custom log driver, but logs is only available for json-file and journald in 1.10).
  • docker inspect looks at all the info on a container (including IP address).
  • docker events gets events from container.
  • docker port shows public facing port of container.
  • docker top shows running processes in container.
  • docker stats shows containers' resource usage statistics.
  • docker diff shows changed files in the container’s FS.

Images #

Lifecycle #

  • docker images shows all images.
  • docker import creates an image from a tarball.
  • docker build creates image from Dockerfile.
  • docker commit creates image from a container, pausing it temporarily if it is running.
  • docker rmi removes an image.
  • docker load loads an image from a tar archive as STDIN, including images and tags (as of 0.7).
  • docker save saves an image to a tar archive stream to STDOUT with all parent layers, tags & versions (as of 0.7).

Info #

Networks #

Lifecycle #

  • docker network create NAME Create a new network (default type: bridge).
  • docker network rm NAME Remove one or more networks by name or identifier. No containers can be connected to the network when deleting it.

Info #

Connection #

Registry & Repository #

Dockerfile #

Instructions #

  • .dockerignore
  • FROM Sets the Base Image for subsequent instructions.
  • MAINTAINER (deprecated - use LABEL instead) Set the Author field of the generated images.
  • RUN execute any commands in a new layer on top of the current image and commit the results.
  • CMD provide defaults for an executing container.
  • EXPOSE informs Docker that the container listens on the specified network ports at runtime. NOTE: does not actually make ports accessible.
  • ENV sets environment variable.
  • ADD copies new files, directories or remote file to container. Invalidates caches. Avoid ADD and use COPY instead.
  • COPY copies new files or directories to container. By default this copies as root regardless of the USER/WORKDIR settings. Use --chown=<user>:<group> to give ownership to another user/group. (Same for ADD.)
  • ENTRYPOINT configures a container that will run as an executable.
  • VOLUME creates a mount point for externally mounted volumes or other containers.
  • USER sets the user name for following RUN / CMD / ENTRYPOINT commands.
  • WORKDIR sets the working directory.
  • ARG defines a build-time variable.
  • ONBUILD adds a trigger instruction when the image is used as the base for another build.
  • STOPSIGNAL sets the system call signal that will be sent to the container to exit.
  • LABEL apply key/value metadata to your images, containers, or daemons.
  • SHELL override default shell is used by docker to run commands.
  • HEALTHCHECK tells docker how to test a container to check that it is still working.

Volumes #

Lifecycle #

Info #

Edit Edit this page