Docker - File
Docker - Dockerfile Basics
Section titled “Docker - Dockerfile Basics”In previous chapters, we used pre-built images like ubuntu, nginx, or jenkins/jenkins downloaded from Docker Hub. These images were created by others using a Dockerfile.
A Dockerfile is a simple text file that contains a series of instructions telling Docker how to build a custom image. It automates the process of setting up an environment, installing dependencies, copying application code, and configuring the runtime.
Why Use Dockerfiles?
Section titled “Why Use Dockerfiles?”- Automation: Automates the image creation process, making it repeatable and reliable.
- Version Control: Dockerfiles can be checked into version control systems (like Git) alongside your application code, tracking changes to your application’s environment.
- Reproducibility: Ensures that anyone can build the exact same image from the Dockerfile, leading to consistent environments.
- Documentation: The Dockerfile itself acts as documentation for how the image is constructed.
- Efficiency: Docker’s layer caching mechanism can speed up builds significantly if parts of the Dockerfile haven’t changed.
Structure of a Dockerfile
Section titled “Structure of a Dockerfile”A Dockerfile consists of a series of instructions, each on its own line. Instructions are typically written in uppercase, followed by arguments.
Step 1: Create the File
Create a file named exactly Dockerfile (with a capital ‘D’ and no extension) in your project’s root directory or a dedicated directory.
Step 2: Add Instructions
Here’s a breakdown of a simple example Dockerfile:
# Use comments to explain steps# This is a sample Dockerfile
# Instruction 1: Specify the base imageFROM ubuntu:22.04
# Instruction 2: Add metadata (e.g., maintainer info)LABEL maintainer="Your Name <you@example.com>"
# Instruction 3: Run commands during build (e.g., install software)RUN apt-get update && apt-get install -y --no-install-recommends nginx && apt-get clean && rm -rf /var/lib/apt/lists/*
# Instruction 4: Copy files from build context into the image# COPY ./my-app-code /app
# Instruction 5: Expose a port the container will listen onEXPOSE 80
# Instruction 6: Define the default command to run when container startsCMD ["nginx", "-g", "daemon off;"]Key points about the structure and common instructions:
#: Used for comments.FROM: Specifies the starting image. Every Dockerfile must begin withFROM.LABEL: Adds metadata (key-value pairs) to the image (replaces deprecatedMAINTAINER).RUN: Executes commands during the image build process (e.g., installing packages). EachRUNcreates a new image layer.COPY: Copies files or directories from your local machine (the build context) into the image.EXPOSE: Informs Docker that the container listens on the specified network ports at runtime (documentation only).CMD: Specifies the default command to execute when a container starts from the image. Only the lastCMDin a Dockerfile takes effect.
Step 3: Save the file.
Once saved, you can use the docker build command in the same directory as the Dockerfile to create your custom image. We will cover the build process in the next chapter.