Beginner’s Guide: How to Create a Dockerfile and Build a Docker Image Step-by-Step

by Admin
~1 minute
Beginner’s Guide: How to Create a Dockerfile and Build a Docker Image Step-by-Step

What is a Dockerfile?

Dockerfile is a text document that contains instructions to build a Docker image. It defines the environment and steps needed to package an application into a container.

Step 1: Clone the GitHub Repository

I’ve already written a simple Python app using Flask. You can start by cloning the repo:

First, fork the repository to your GitHub account by clicking the “Fork” button in the top right of the repo page.

Then clone your forked repository to your local.

below is the example command to clone repository to the local

git clone https://github.com/vishamyatech/python-web-application.git

after cloning : cd python-web-application

Step 2: Create a Dockerfile

If you were starting from scratch, you’d use the command below to create a new Dockerfile:

touch Dockerfile

This command creates an empty file named Dockerfile in your current directory.

However, you don’t need to do this manually — the Dockerfile is already present in the GitHub repository you cloned or forked.

You can open it and review or edit the content if needed.

Here’s the complete Dockerfile content for reference (you can copy and reuse it if you’re creating your own from scratch):

# Use a base image

FROM python:3.9-slim

# Set the working directory inside the container

WORKDIR /app

# Copy the local files into the container

COPY . .

# Define the command to run the application

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

Step 5: Build the Docker Image

docker build -t my-python-app .

The above step will create docker image named my-python-app

Step 6: Run the Docker Container

docker run my-python-app

Now start the container using:

docker run -d -p 5000:5000 my-python-app

click on the below link , you will see "Hello, World! Welcome to Docker + Python Web App."

http://localhost:5000

Want to Learn More About Docker?  checkout  below blog