Skip to content

Instantly share code, notes, and snippets.

@GLMeece
Last active February 8, 2021 17:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GLMeece/359261b4706946a73ffc9ebffa28c827 to your computer and use it in GitHub Desktop.
Save GLMeece/359261b4706946a73ffc9ebffa28c827 to your computer and use it in GitHub Desktop.
Docker Cheat Sheet

Useful Docker Cheat Sheet

Random Tips

  • FROM β€” specifies the base (parent) image; typically you specify both the image name as well as the label. 🍰
  • RUN β€” runs a command and creates an image layer. Used to install packages into containers. 🍰
  • COPY β€” copies files and directories to the container. 🍰
  • ADD β€” copies files and directories to the container. Can upack local .tar files. 🍰
  • LABEL β€” provides metadata. Good place to include maintainer info.
  • ENV β€” sets a persistent environment variable.
  • CMD β€” provides a command and arguments for an executing container. Parameters can be overridden. There can be only one CMD.
  • WORKDIR β€” sets the working directory for the instructions that follow.
  • ARG β€” defines a variable to pass to Docker at build-time.
  • ENTRYPOINT β€” provides command and arguments for an executing container. Arguments persist.
  • EXPOSE β€” exposes a port.
  • VOLUME β€” creates a directory mount point to access and store persistent data.

Only the instructions FROM, RUN, COPY, and ADD create 🍰 layers in the final image


Docker CLI management commands start with docker, then a space, then the management category, then a space, and then the command. For example, docker container stop stops a container.

Containers

Use docker container the_command

  • create β€” Create a container from an image (flags listed here). E.g., docker container create my_repo/my_image:my_tag
  • run β€” Create a new container and start it. E.g., docker run -i -t -w /base/dir --entrypoint=/bin/bash --name image_name image_path/image_name
  • start β€” Start an existing container which was previously running.
  • stop β€” Gracefully stop running container.
  • kill β€” Stop main process in container abruptly.
  • rmβ€” Delete a stopped container.
  • ls β€” List running containers.
  • inspect β€” See lots of info about a container.
  • logs β€” Print logs.

Images

Use docker image the_command

  • build β€” Build an image. E.g., docker build -t IMAGE_NAME:LABEL .; the -t lets you specify both image and label
  • push β€” Push an image to a remote registry.
  • ls β€” List images.
  • history β€” See intermediate image info.
  • inspect β€” See lots of info about an image, including the layers.
  • rm β€” Delete an image.

Misc

  • docker version β€” List info about your Docker Client and Server versions.
  • docker login β€” Log in to a Docker registry.
  • docker system prune β€” Delete all unused containers, unused networks, and dangling images.

Key Volume Commands

  • docker volume create
  • docker volume ls
  • docker volume inspect
  • docker volume rm
  • docker volume prune

Common options for the --mount flag in docker run --mount my_options my_image:

  • type=volume
  • source=volume_name
  • destination=/path/in/container
  • readonly

Size Inspection

  • docker container ls -s to view the approximate size of a running container.
  • docker image ls shows the sizes of your images.
  • docker image history my_image:my_tag to see the size of the intermediate images that make up your image.
  • docker image inspect my_image:tag will show the sizes of each layer. Layers are subtly different than the images that make up an entire image. But you can think of them as the same for most purposes. Check out this great article by Nigel Brown if you want to dig into layer and intermediate image intricacies.
  • Also recommended - Dive.

Eight Best Practices to Reduce Image Sizes & Build Times

  1. Use an official base image whenever possible. Official images are updated regularly and are more secure than un-official images.
  2. Use variations of Alpine images when possible to keep your images lightweight.
  3. If using apt, combine RUN apt-get update with apt-get install in the same instruction. Then chain multiple packages in that instruction. List the packages in alphabetical order over multiple lines with the \ character. For example:
RUN apt-get update && apt-get install -y \
    package-one \
    package-two 
 && rm -rf /var/lib/apt/lists/*

This method reduces the number of layers to be built and keeps things nice and tidy.

  1. Include && rm -rf /var/lib/apt/lists/* at the end of the RUN instruction to clean up the apt cache so it isn’t stored in the layer. See more in the Docker Docks. Thanks to Vijay Raghavan Aravamudhan for this suggestion. Updated Feb. 4, 2019.
  2. Use caching wisely by putting instructions likely to change lower in your Dockerfile.
  3. Use a .dockerignore file to keep unwanted and unnecessary files out of your image.
  4. Check out dive β€” a very cool tool for inspecting your Docker image layers and helping you trim the fat.
  5. Don’t install packages you don’t need. Duh! But common.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment