Learn to Create a Dockerfile: Easy Step-by-Step Example

Learn to Create a Dockerfile: Easy Step-by-Step Example

We'll explore how to write a Dockerfile, the blueprint for creating Docker images, which package applications with all their dependencies in a single container, making Docker essential for modern software development and deployment.

What is a Dockerfile?

A Dockerfile is a script that contains a series of instructions on how to build a Docker image. Each instruction in a Dockerfile creates a layer in the image, making it possible to build and distribute the application consistently across different environments.

Step-by-Step Guide to Writing a Dockerfile

We'll go through each instruction with explanations and then provide a complete example at the end.

1. Specify the Base Image

The first instruction in a Dockerfile is typically the FROM instruction, which specifies the base image. This base image serves as the starting point for your Docker image.

FROM python:3.9-slim

In this example, we're using the official Python 3.9 slim image as our base, which is a lightweight version (hence the slim suffix).

2. Set the Working Directory

The WORKDIR instruction sets the working directory for the commands that follow in the Dockerfile.

WORKDIR /usr/src/app

This sets the working directory to /usr/src/app inside the container. This is where our application code will be copied to.

3. Copy Files to the Container

The COPY instruction copies files from your host machine to the container.

COPY requirements.txt ./

This copies the requirements.txt file from the current directory (i.e., the directory containing the Dockerfile) into the container at the current working directory (/usr/src/app).

4. Install Dependencies

The RUN instruction executes a command in the container. It's often used to install dependencies.

RUN pip install --no-cache-dir -r requirements.txt

This installs the dependencies listed in requirements.txt. The --no-cache-dir flag tells pip not to cache the installed packages.

5. Copy the Rest of the Application Code

After installing dependencies, you typically copy the rest of the application code to the container.

COPY . .

This copies all files from the current directory on your host machine to the working directory in the container.

6. Expose Ports

The EXPOSE instruction tells Docker which port the container listens on at runtime.

EXPOSE 5000

This exposes port 5000, which is the default port for Flask applications.

7. Define the Command to Run the Application

The CMD instruction specifies the command to run within the container when it starts.

CMD ["python", "app.py"]

This tells Docker to run python app.py when the container starts.

Complete Dockerfile for a Simple Flask Application

Let's combine all these instructions into a complete Dockerfile for a simple Flask application.

Simple Flask Application Code

Create a directory for your project and add the following files:

app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Docker!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

requirements.txt:

Flask==2.0.1

Project structure:

flask_app/
│
├── app.py
└── requirements.txt

Dockerfile

Create a file named Dockerfile in the same directory with the following content:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the requirements file into the container
COPY requirements.txt ./

# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt

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

# Make port 5000 available to the world outside this container
EXPOSE 5000

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

Building and Running the Docker Image

With your Dockerfile and application code in place, you can now build and run the Docker image.

1. Build the Docker Image

Navigate to the project directory and run the following command to build the Docker image:

docker build -t flask-app .

The -t flag tags the image with the name flask-app.

2. Run the Docker Container

Run the Docker container with the following command:

docker run -p 5000:5000 flask-app

The -p flag maps port 5000 on your host to port 5000 on the container.

Visit http://your_ip:5000 in your browser, and you should see "Hello, Docker!".

Conclusion

We've walked through the process of writing a Dockerfile step by step. We covered the essential instructions and provided a comprehensive example of containerizing a simple Flask application. By following these steps, you can create Docker images that ensure your application runs consistently across different environments, simplifying deployment and scaling.