Repeatable Builds
A Dockerfile provides a repeatable set of instructions for creating the same application image.
Learn how to package an application into a Docker image by creating your first Dockerfile. Understand the essential instructions, build your image, and run your application inside a container.
Build Workflow
Introduction
A Dockerfile is a text-based file containing instructions that Docker uses to build an image. These instructions can define the base image, working directory, files to copy, commands to run, and the default command for the container.
Once your Dockerfile is ready, you can use the docker build command to create an image. That image can then be used to create and run one or more containers.
Learning to write Dockerfiles is an important step toward containerizing applications and working with modern DevOps and cloud-native environments.
Your First Dockerfile
This example demonstrates a basic Dockerfile structure for a Node.js application.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Why Learn Dockerfiles
Dockerfiles provide a repeatable way to define how your application environment is created.
A Dockerfile provides a repeatable set of instructions for creating the same application image.
Package your application and its required environment so it can run consistently across different systems.
Dockerfiles make application image creation easier to automate in development and CI/CD workflows.
Dockerfile skills are important for containers, CI/CD, Kubernetes, cloud-native applications, and DevOps.
Core Concepts
These concepts will help you understand how Docker builds an image from a Dockerfile and runs it as a container.
Learn how a Dockerfile defines the instructions Docker uses to automatically build a container image.
Use the FROM instruction to select the base image your application will build upon.
Use WORKDIR to define where future commands and application files will be handled inside the image.
Use COPY to add your application files and dependencies to the container image.
Use RUN to execute build-time commands such as installing application dependencies.
Use CMD to define the default command that runs when a container starts.
Use docker build to create an image from the instructions in your Dockerfile.
Use docker run to create and start a container from the image you built.
Learn why the directory used with docker build determines which files are available to COPY instructions.
Learn the basics of image layers, build caching, .dockerignore, smaller images, and better Dockerfile organization.
Practice Commands
Use these Docker commands after creating your Dockerfile.
docker build -t my-app .Build a Docker image named my-app using the Dockerfile and current directory as the build context.
docker imagesList the Docker images available on your local system.
docker run my-appCreate and start a container from the my-app image.
docker psDisplay currently running containers.
docker ps -aDisplay all containers, including containers that have stopped.
docker run -p 3000:3000 my-appRun the application and publish container port 3000 to port 3000 on the host.
Skills You Will Learn
These instructions and commands form the foundation for building Docker images.
Learning Roadmap
Follow this simple process to package and run your first containerized application.
Start with a simple application that you want to package into a Docker container.
Create a file named Dockerfile in your project directory without a file extension.
Use the FROM instruction to select an appropriate base image for your application.
Copy application files into the image and run the commands required to install dependencies.
Use CMD to specify the default command that should run when the container starts.
Build your Docker image and run it as a container to test your application.
What's Next?
After creating your first Dockerfile, continue learning about images, containers, storage, networking, and multi-container applications.
Start Building
Create a simple Dockerfile, build your first image, and run your application inside a container.