Home > Backend Development > Python Tutorial > Docker Hands-on: Learn Dockerfile, Container, Port Forwarding with Sample Flask Project

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

Susan Sarandon
Release: 2025-01-14 07:36:44
Original
383 people have browsed it

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:
<code class="language-python"># 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)</code>
Copy after login
  1. Also in "flask-app", create a Dockerfile (no extension) with the following content:
<code class="language-dockerfile">FROM python:3.13.1-alpine3.21
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "index.py"]</code>
Copy after login
  1. Finally, create requirements.txt in the "flask-app" directory:
<code>Flask==2.3.2</code>
Copy after login

Your directory structure should now look like this:

<code>flask-app/
├── Dockerfile
├── index.py
└── requirements.txt</code>
Copy after login

Building and Running the Docker Image:

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

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):
<code class="language-bash">docker tag flask-app omerbsezer/dev-to-flask-app:latest</code>
Copy after login
  1. Push the image:
<code class="language-bash">docker push omerbsezer/dev-to-flask-app:latest</code>
Copy after login

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:

The above is the detailed content of Docker Hands-on: Learn Dockerfile, Container, Port Forwarding with Sample Flask Project. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template