Mastering Essential Dockerfile Instructions
Written on
Chapter 1: Introduction to Dockerfiles
This article delves into Dockerfiles, marking the third segment of a six-part series focused on Docker. If you haven't yet read Part 1, I encourage you to do so to gain a fresh perspective on Docker container concepts. π‘ Part 2 offers a concise overview of the Docker ecosystem. Future installments will cover topics such as optimizing Docker images, Docker CLI commands, and managing data with Docker.
Now, letβs explore the essential Dockerfile instructions!
Understanding Docker Images
To recap, a Docker container is essentially a running instance of a Docker imageβa self-sufficient, streamlined operating system that houses application code. The image is constructed during the build phase, while the container is instantiated during runtime.
The cornerstone of Docker is the Dockerfile, which instructs Docker on how to construct the image utilized to create containers. Each Docker image incorporates a file named Dockerfile, which lacks an extension. When executing the docker build command to create an image, the Dockerfile is typically located in the current working directory, though a different path can be specified using the file flag (-f).
A Docker container is layered, with each layer being read-only, except for the final layer, which is writable. The Dockerfile dictates the sequence and type of layers to be added. Each layer acts as a file that captures the modifications made since the preceding layer.
The initial layer(s) of a Docker image stem from a base image, also referred to as a parent image. When an image is retrieved from a remote repository, only the layers that are absent from the local system are downloaded, embodying Docker's efficiency in conserving space and time.
Chapter 2: Key Dockerfile Instructions
Each instruction in a Dockerfile begins with a capitalized keyword followed by its arguments. The instructions are processed sequentially during image construction. The following are the key Dockerfile instructions to familiarize yourself with:
- FROM β Specifies the base image.
- LABEL β Adds metadata, useful for maintainer information.
- ENV β Sets a persistent environment variable.
- RUN β Executes a command, creating a new image layer; often used for installing packages.
- COPY β Transfers files and directories into the container.
- ADD β Similar to COPY, but can also unpack local .tar files.
- CMD β Provides a command and its arguments for the container; can be overridden.
- WORKDIR β Sets the working directory for subsequent instructions.
- ARG β Defines a variable that can be passed to Docker at build time.
- ENTRYPOINT β Specifies a command for the executing container, with arguments that persist.
- EXPOSE β Indicates which ports should be exposed.
- VOLUME β Establishes a directory mount point for persistent data.
Let's delve deeper into these instructions and see some examples!
Section 2.1: Simple Dockerfile Example
A Dockerfile may begin with a straightforward line:
FROM ubuntu:18.04
This instruction indicates that Docker should utilize the specified base image from the repository. In this case, "ubuntu" serves as the repository name, with "18.04" being the tag that indicates the version of the image to pull. If no tag is specified, Docker defaults to the latest version.
When building an image locally for the first time, Docker retrieves the necessary layers from the specified Ubuntu image. These layers stack upon one another, with each layer representing a file containing changes from the previous one.
Docker employs a copy-on-write strategy for efficiency. If a layer exists in the image, Docker utilizes it for read access without requiring a download. Any modifications to a running image create a copy in the writable layer above.
Section 2.2: Comprehensive Dockerfile
To illustrate a more complex Dockerfile, consider the following:
FROM python:3.7.2-alpine3.8
LABEL maintainer="[email protected]"
ENV ADMIN="jeff"
RUN apk update && apk upgrade && apk add bash
COPY . ./app
RUN ["mkdir", "/a_directory"]
CMD ["python", "./my_script.py"]
In this example, the base image is an official Python image tagged "3.7.2-alpine3.8". Alpine images are favored for their compact size and speed, although they may lack certain operating system features, requiring manual installation of necessary packages.
The LABEL instruction introduces metadata to the image, such as the maintainer's contact information. ENV sets a persistent environment variable, which can be utilized when the container runs.
The RUN instruction updates packages and installs bash. The COPY command transfers local files into the working directory of the image. The ADD instruction can also fetch files from a remote URL, while the CMD instruction designates a script to execute when the container starts.
Section 2.3: Other Important Instructions
Additional instructions worth noting include WORKDIR, which sets the current working directory for subsequent commands, and ARG, which defines variables to be used during the build process. ENTRYPOINT specifies default commands and arguments, ensuring they aren't overridden when the container is executed.
Finally, EXPOSE indicates which ports the container will make accessible, while VOLUME creates a mount point for persistent data storage.
Conclusion
You've now become acquainted with a dozen essential Dockerfile instructions that will enhance your Docker expertise. For those curious, a cheat sheet summarizing all Dockerfile instructions, including USER, ONBUILD, STOPSIGNAL, SHELL, and HEALTHCHECK, is available for your reference.
Mastering Dockerfiles is crucial for effective Docker usage. I hope this guide has bolstered your confidence in using Dockerfiles. Stay tuned for the next article in this series, where we'll focus on optimizing image sizes.
If you found this article beneficial, please share it on your preferred social media platforms. π