Understanding Dockerfile: A Complete Guide with Example and Cheat Sheet
Introduction to Dockerfile
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build
users can create an automated build that executes several command-line instructions in succession.
Why Use a Dockerfile?
Creating a Dockerfile allows you to automate the installation of software in a new Docker image. This ensures that the image gets built the same way every time you run the command, making it repeatable and more reliable than manual builds.
Components of a Dockerfile
Here are some of the key instructions you can use in a Dockerfile:
- FROM: Sets the Base Image for subsequent instructions.
- RUN: Executes commands in a new layer on top of the current image.
- CMD: Provides defaults for an executing container.
- LABEL: Adds metadata to an image.
- EXPOSE: Informs Docker that the container listens on the specified network ports.
- ENV: Sets the environment variable.
- ADD: Copies new files, directories, or remote file URLs and adds them to the filesystem of the image.
- COPY: Copies new files or directories and adds them to the filesystem of the container.
- ENTRYPOINT: Allows you to configure a container that will run as an executable.
- VOLUME: Creates a mount point with the specified name and marks it as holding externally mounted volumes from the native host or other containers.
- USER: Sets the user name or UID to use when running the image and for any
RUN
,CMD
, andENTRYPOINT
instructions that follow it. - WORKDIR: Sets the working directory.
- ARG: Defines a variable that users can pass at build-time to the builder with the
docker build
command. - 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.
- HEALTHCHECK: Tells Docker how to test a container to check that it is still working.
- SHELL: Allows the shell to be overridden.
Example Dockerfile
Let's consider a Dockerfile for a simple Python Flask application:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
This Dockerfile creates a Docker image that includes a Python environment with Flask installed, sets up a working directory, copies in the application code, installs dependencies, and specifies how to run the application.
Dockerfile Commands Cheat Sheet
Here's a cheat sheet with common Dockerfile commands and their usage:
Command | Usage | Example |
---|---|---|
FROM | Initializes a new build stage and sets the Base Image | FROM ubuntu:18.04 |
RUN | Executes commands in a new layer | RUN apt-get update && apt-get install -y git |
CMD | Provides defaults for executing container | CMD ["python", "app.py"] |
LABEL | Adds metadata to an image | LABEL maintainer="john.doe@example.com" |
EXPOSE | Indicates the ports on which a container listens | EXPOSE 80 |
ENV | Sets environment variables | ENV API_KEY="SECRET_GIVEN" |
ADD | Copies files, directories, or remote file URLs | ADD ./localdir /containerdir |
COPY | Copies new files or directories | COPY ./localdir /containerdir |
ENTRYPOINT | Configures a container that will run as an executable | ENTRYPOINT ["executable", "param1", "param2"] |
VOLUME | Creates a mount point | VOLUME /data |
USER | Sets the UID or username | USER 1001 |
WORKDIR | Sets the working directory | WORKDIR /path/to/workdir |
ARG | Defines a variable to pass to the builder | ARG version=1.0 |
ONBUILD | Adds a trigger instruction | ONBUILD RUN /usr/local/bin/python-build --dir /app |
STOPSIGNAL | Sets the system call signal that will be sent to the |
Comments
Post a Comment