Dockerfile Creation
A Dockerfile is a text file that contains instructions for building a Docker image. It’s like a recipe: it specifies the base operating system, dependencies, commands to execute, and the application code to include. Docker uses the Dockerfile to automate the image creation process.
Dockerfile Instructions
Here are some of the most common Dockerfile instructions:
FROM
- Description: Specifies the base image for your new image. Every
Dockerfilemust start with aFROMinstruction. -
Example:
FROM ubuntu:22.04 # Use the official Ubuntu 22.04 image as the base FROM python:3.9-slim # Use a slim Python 3.9 image FROM scratch # Create an empty image
WORKDIR
- Description: Sets the working directory inside the image. Subsequent
RUN,COPY,ADD,CMD, andENTRYPOINTinstructions will be executed in this directory. It’s like usingcdin a shell script. If the directory doesn’t exist, it will be created. -
Example:
WORKDIR /app # Set the working directory to /app
COPY
- Description: Copies files and directories from the host machine (where you’re running
docker build) into the image. -
Example:
COPY requirements.txt /app/ # Copy requirements.txt to the /app directory in the image COPY src/ /app/src/ # Copy the entire 'src' directory to /app/src/ COPY . /app # Copy everything from the current directory to /app (be careful with this!)Note: It’s best practice to be specific about what you copy. Copying everything (
COPY . /app) can lead to larger image sizes and potential security issues if you accidentally include sensitive files. Use a.dockerignorefile (similar to.gitignore) to exclude files and directories from the build context.
ADD
- Description: Similar to
COPY, but with additional features:- Can copy files from URLs.
- Can automatically extract compressed archives (tar, gzip, bzip2, etc.).
- Example:
ADD [https://example.com/my_app.tar.gz](https://www.google.com/search?q=https://example.com/my_app.tar.gz) /tmp/