"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Docker practice: learn Dockerfile, container and port forwarding through the Flask project

Docker practice: learn Dockerfile, container and port forwarding through the Flask project

Posted on 2025-04-18
Browse:943

This tutorial demonstrates building and deploying a simple Flask application using Docker. We'll cover creating a Dockerfile, building the image, running a container, and even pushing the image to Docker Hub. For those unfamiliar with Docker fundamentals, check out this previous post:

Let's get started with a hands-on example:

Project Setup:

  1. Create a directory named "flask-app".
  2. Inside "flask-app", create index.py containing this simple Flask application:
# index.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello World!"
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=int("5000"), debug=True)
  1. Also in "flask-app", create a Dockerfile (no extension) with the following content:
FROM python:3.13.1-alpine3.21
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "index.py"]
  1. Finally, create requirements.txt in the "flask-app" directory:
Flask==2.3.2

Your directory structure should now look like this:

flask-app/
├── Dockerfile
├── index.py
└── requirements.txt

Building and Running the Docker Image:

  1. Navigate to the "flask-app" directory in your terminal.
  2. Build the Docker image using this command:
docker build -t flask-app .
  1. Verify the image was built successfully:
docker images
  1. Run the Docker container, mapping port 5000 on your host machine to port 5000 in the container:
docker run --name my-flask-app -d -p 5000:5000 flask-app
  1. Check the running container:
docker ps -a
  1. Test the application by accessing http://127.0.0.1:5000 in your browser or using curl:
curl http://127.0.0.1:5000
  1. To stop and remove the container:
docker container rm -f my-flask-app
  1. To remove the image:
docker image rm -f flask-app

Pushing to Docker Hub:

Before pushing to Docker Hub, create an account if you don't have one already. Then:

  1. Tag the image for Docker Hub (replace omerbsezer with your Docker Hub username):
docker tag flask-app omerbsezer/dev-to-flask-app:latest
  1. Push the image:
docker push omerbsezer/dev-to-flask-app:latest

You can then see your image on Docker Hub. A screenshot would be placed here.

Docker Hands-on: Learn Dockerfile, Container, Port Forwarding with Sample Flask Project

Conclusion:

This practical example demonstrates a complete workflow for containerizing a simple Python application with Docker. For more Docker tutorials, AWS, Kubernetes, Linux, DevOps, Ansible, Machine Learning, Generative AI, and SAAS content, follow these links:

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3