- If needed, create a
.dockerignorefile 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.tarfiles. π°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_tagrunβ 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_namestartβ 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-tlets 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 createdocker volume lsdocker volume inspectdocker volume rmdocker volume prune
Common options for the --mount flag in docker run --mount my_options my_image:
type=volumesource=volume_namedestination=/path/in/containerreadonly
docker container ls -sto view the approximate size of a running container.docker image lsshows the sizes of your images.docker image history my_image:my_tagto see the size of the intermediate images that make up your image.docker image inspect my_image:tagwill 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 updatewithapt-get installin 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
.dockerignorefile 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.