Introduction
While pulling existing images from Docker Hub is convenient, real-world applications often require custom environments or specific configurations. This is where Dockerfiles come into play. A Dockerfile is a text file that contains a series of instructions that Docker uses to build an image. This lesson will introduce you to the basic structure of a Dockerfile and its most commonly used instructions.
Key Concepts
What is a Dockerfile?
A Dockerfile is a script that defines how a Docker image should be built. Each instruction in a Dockerfile creates a new layer in the Docker image. These layers are cached, which speeds up subsequent builds.
-
Syntax: Dockerfiles use a simple, command-line-like syntax, with each instruction on a new line.
-
Layering: Each instruction (e.g.,
RUN,COPY) in a Dockerfile results in a new read-only layer being added to the image. This layering is fundamental to Docker's efficiency and versioning.
Core Dockerfile Instructions
-
FROM: Specifies the base image for your build. This is always the first instruction in a Dockerfile. It sets the foundation, like an operating system or a language runtime.- Example: `FROM ubuntu:
-
04
orFROM node:18-alpine`. -
WORKDIR: Sets the working directory for anyRUN,CMD,ENTRYPOINT,COPY, andADDinstructions that follow it in the Dockerfile.- Example:
WORKDIR /app.
- Example:
-
COPY: Copies new files or directories from a source path (typically your build context) into the filesystem of the container at the destination path.- Example:
COPY . .(copies everything from the current directory to the working directory in the container).
- Example:
-
RUN: Executes any commands in a new layer on top of the current image and commits the results. This is often used for installing software, updating packages, or building applications.- Example:
RUN apt-get update && apt-get install -y git.
- Example:
-
EXPOSE: Informs Docker that the container listens on the specified network ports at runtime. This doesn't actually publish the port; it merely documents which ports are intended to be exposed.- Example:
EXPOSE 80.
- Example:
-
CMD: Provides defaults for an executing container. There can only be oneCMDinstruction in a Dockerfile. If you specify aCMDwhen running a container, it overrides the Dockerfile'sCMD.- Example:
CMD ["node", "server.js"].
- Example:
Example/Code
Here's a simple Dockerfile for a Node.js application:
dockerfile# Use an official Node.js runtime as a parent image FROM node:18-alpine # Set the working directory in the container WORKDIR /app # Copy package.json and package-lock.json to the working directory COPY package*.json ./ # Install application dependencies RUN npm install # Copy the rest of the application code to the working directory COPY . . # Inform Docker that the container listens on port 3000 at runtime EXPOSE 3000 # Define the command to run your app CMD [ "npm", "start" ]
Summary/Key Takeaways
-
A Dockerfile is a script with instructions for building a Docker image.
-
Each instruction creates a new image layer.
-
Key instructions include
FROM(base image),WORKDIR(set directory),COPY(add files),RUN(execute commands),EXPOSE(document ports), andCMD(default command).