What is a Dockerfile?
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Docker reads these instructions to automatically build a Docker image.
Dockerfiles are essentially scripts that automate the process of creating Docker images. Each instruction in a Dockerfile creates a read-only layer in the image, defining the environment, dependencies, and commands required to run an application. They ensure that an image is built consistently and reproducibly every time.
Key Instructions
A Dockerfile typically consists of several instructions, each performing a specific action during the image build process:
- FROM: Specifies the base image upon which subsequent instructions will be built. Every Dockerfile must start with a FROM instruction.
- RUN: Executes commands in a new layer on top of the current image, committing the results. This is used for installing packages, creating directories, etc.
- COPY / ADD: Copies files and directories from the host machine (build context) into the image's filesystem.
- WORKDIR: Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it.
- EXPOSE: Informs Docker that the container listens on the specified network ports at runtime. This is purely informational.
- ENV: Sets environment variables. These variables are available within the container.
- ARG: Defines build-time variables that users can pass to the builder with the
docker build --build-arg <name>=<value>command. - CMD: Provides default commands and arguments for an executing container. There can only be one CMD instruction in a Dockerfile; if you specify more than one, only the last CMD will take effect.
- ENTRYPOINT: Configures a container that will run as an executable. It defines the primary command that will be executed when the container starts.
Example Dockerfile
Here's a simple Dockerfile for a Node.js application:
# 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 any dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port your app runs on
EXPOSE 3000
# Define the command to run your app
CMD ["node", "server.js"]
Building an Image
Once you have created a Dockerfile, you can build a Docker image using the docker build command. The . at the end specifies the build context, which is the path to the directory containing your Dockerfile and any other files needed for the build.
docker build -t my-node-app:1.0 .
This command builds an image named my-node-app with the tag 1.0.
Benefits of Using Dockerfiles
- Automation: Automates the image creation process, reducing manual steps and potential human errors.
- Reproducibility: Ensures that the same image can be built consistently across different environments and by different users.
- Version Control: Dockerfiles are text files and can be easily stored in version control systems (like Git) alongside your application code.
- Transparency: Provides a clear and auditable record of how an image was constructed, making it easier to understand and maintain.
- Portability: Images built from Dockerfiles are self-contained and can be easily shared and run on any Docker-enabled host.