AWS Builder Center

Writing Your First Dockerfile: A Beginner’s Blueprint

A step-by-step guide to turning your application code into a secure, portable, and production-ready container image.

Moving from running pre-made images (like official Nginx or Redis containers) to building your own custom applications is a major milestone in your DevOps journey. At the heart of this process is the Dockerfile - a simple, plain-text script containing all the commands required to assemble a complete container image.
Let’s break down how to write your very first Dockerfile from scratch using a practical, clean example.

The Anatomy of a Dockerfile

A Dockerfile reads like a recipe, executing instructions sequentially from top to bottom. Every line builds upon the previous layer. Here are the core instructions you need to know:
  • FROM: Establishes the base image (e.g., an official lightweight Python or Node.js runtime). Every Dockerfile must start with this.
  • WORKDIR: Sets the working directory for any subsequent COPY, RUN, or CMD instructions inside the container.
  • COPY: Transfers files and directories from your local host machine into the container's file system.
  • RUN: Executes build-time commands, such as installing software packages, libraries, or dependencies.
  • EXPOSE: Documents which network ports the container listens on at runtime.
  • CMD: Specifies the default command that executes when the container finally starts up.

Building a Simple Python Dockerfile

Imagine you have a simple Python application. To containerize it, create a file named literally Dockerfile (no file extension) in your project root directory and add the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1. Start with a lightweight official Python base image
FROM python:3.11-slim

# 2. Set the working directory inside the container
WORKDIR /app

# 3. Copy dependency definitions first (optimizes layer caching)
COPY requirements.txt .

# 4. Install project dependencies
RUN pip install --no-cache-dir -r requirements.txt

# 5. Copy the rest of your application code into the container
COPY . .

# 6. Expose the port the app runs on
EXPOSE 5000

# 7. Define the default command to run your application
CMD ["python", "app.py"]

Building and Running Your Image

Once your Dockerfile and code are ready, open your terminal in the same directory and execute two simple commands:
1. Build the Image
1
docker build -t my-python-app .
  • -t my-python-app: Tags your image with a friendly name.
  • .: Tells Docker to look for the Dockerfile in the current directory.
2. Run the Container
1
docker run -d -p 5000:5000 --name running-app my-python-app
Your application is now successfully containerized, isolated, and running locally!

Pro-Tips for Clean Dockerfiles

  • Use .dockerignore: Just like .gitignore, create a .dockerignore file to exclude heavy folders like node_modules, .git, or virtual environments (venv) from being copied into your image.
  • Leverage Layer Caching: Always copy your dependency files (requirements.txt or package.json) before copying your source code. This ensures Docker only re-installs dependencies when they change, making subsequent builds lightning-fast.
  • Choose Slim Base Images: Prefer -slim or Alpine variants to keep your image size small and reduce your overall security attack surface.

Conclusion

Writing a Dockerfile transforms your code from "it works on my machine" into a portable artifact that can run identically anywhere-from your laptop to an enterprise Kubernetes cluster. By mastering these foundational commands, you have complete control over how your application is packaged and deployed.
If you enjoyed this article, don't forget to press the like button and drop a comment with your views.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article