0
0
DockerConceptBeginner · 3 min read

What is Dockerfile: Definition, Example, and Usage

A Dockerfile is a simple text file that contains instructions to build a Docker image. It tells Docker how to set up the environment, install software, and prepare your app inside a container.
⚙️

How It Works

Think of a Dockerfile as a recipe for baking a cake. Instead of ingredients and baking steps, it lists commands to prepare a container image. Each instruction adds a layer, like adding flour or sugar, building up the final product.

When you run Docker to build an image from this file, it follows the instructions step-by-step. This creates a lightweight, portable package that contains everything your app needs to run, no matter where you deploy it.

💻

Example

This example shows a simple Dockerfile that creates an image running a basic web server using Nginx.

dockerfile
FROM nginx:1.23.4-alpine
COPY ./html /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Output
When built and run, this image starts an Nginx server serving files from the local 'html' folder on port 80.
🎯

When to Use

Use a Dockerfile when you want to package your application and its environment into a container image. This is useful for:

  • Sharing your app with others in a consistent way
  • Deploying apps easily on any system with Docker
  • Automating builds and testing in CI/CD pipelines
  • Running multiple isolated apps on the same machine

Key Points

  • A Dockerfile is a text file with build instructions for Docker images.
  • It defines the base image, files to add, commands to run, and ports to expose.
  • Building a Docker image from a Dockerfile creates a portable container package.
  • It helps ensure your app runs the same everywhere.

Key Takeaways

A Dockerfile is a recipe that tells Docker how to build a container image.
It contains simple instructions like which base image to use and what commands to run.
Dockerfiles help create portable, consistent environments for your apps.
Use Dockerfiles to automate app packaging and deployment.
Each instruction in a Dockerfile adds a layer to the final image.