- If needed, create a
.dockerignore
file to exclude files/paths you don't want. Matching rules are from Golang's filepath rules, as well as some unique exceptions.
A Dozen Dockerfile Instructions to Know
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.
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.
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 labelpush
β 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.
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.
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
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.
- Use an official base image whenever possible. Official images are updated regularly and are more secure than un-official images.
- Use variations of Alpine images when possible to keep your images lightweight.
- If using
apt
, combineRUN apt-get update
withapt-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.
- 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. - Use caching wisely by putting instructions likely to change lower in your Dockerfile.
- Use a
.dockerignore
file to keep unwanted and unnecessary files out of your image. - Check out dive β a very cool tool for inspecting your Docker image layers and helping you trim the fat.
- Donβt install packages you donβt need. Duh! But common.