Skip to main content

Dockerfile

 

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, and ENTRYPOINT 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:

CommandUsageExample
FROMInitializes a new build stage and sets the Base ImageFROM ubuntu:18.04
RUNExecutes commands in a new layerRUN apt-get update && apt-get install -y git
CMDProvides defaults for executing containerCMD ["python", "app.py"]
LABELAdds metadata to an imageLABEL maintainer="john.doe@example.com"
EXPOSEIndicates the ports on which a container listensEXPOSE 80
ENVSets environment variablesENV API_KEY="SECRET_GIVEN"
ADDCopies files, directories, or remote file URLsADD ./localdir /containerdir
COPYCopies new files or directoriesCOPY ./localdir /containerdir
ENTRYPOINTConfigures a container that will run as an executableENTRYPOINT ["executable", "param1", "param2"]
VOLUMECreates a mount pointVOLUME /data
USERSets the UID or usernameUSER 1001
WORKDIRSets the working directoryWORKDIR /path/to/workdir
ARGDefines a variable to pass to the builderARG version=1.0
ONBUILDAdds a trigger instructionONBUILD RUN /usr/local/bin/python-build --dir /app
STOPSIGNALSets the system call signal that will be sent to the

Comments