DEV Community

Ömer Berat Sezer
Ömer Berat Sezer

Posted on • Edited on

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

In the previous post, we've mentioned about the Docker tutorial. Please have a look below link, if you haven't seen before:

This time, we're starting to run sample projects to make practice.

It shows:

  • how to create Dockerfile
  • how to copy app while building Dockerfile
  • how to build Docker image from Dockerfile
  • how to run Docker container with port forwarding
  • how to tag the Docker image
  • how to push Docker image to DockerHub

GitHub Code Repo: https://github.com/omerbsezer/Fast-Docker/tree/main/hands-on-sample-projects/flask-app

Steps:

  • Create a directory (“flask-app”).
  • Create a file (“index.py”) in the “flask-app” directory (copy from below). This is a simple Flask that returns “Hello World” on the browser:
# 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)
Enter fullscreen mode Exit fullscreen mode
  • Create “Dockerfile” (there is no extension) in the “flask-app” directory (copy from below). It copies to /app directory in the container, run requirements.txt, expose 5000 port and run python app:
FROM python:3.13.1-alpine3.21
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "index.py"]
Enter fullscreen mode Exit fullscreen mode
  • Create “requirements.txt” (copy it below):
Flask==2.3.2
Enter fullscreen mode Exit fullscreen mode
  • Now, we have 3 files:
user@docker:~$ ls
Dockerfile  index.py  requirements.txt
Enter fullscreen mode Exit fullscreen mode
  • Build image from Dockerfile:
user@docker:~$ docker build -t flask-app .
Step 1/6 : FROM python:3.13.1-alpine3.21
 ---> 0a5bfb768070
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> d4a4ac651986
Step 3/6 : COPY . /app
 ---> Using cache
 ---> 4c3f1a3fea8a
Step 4/6 : RUN pip install -r requirements.txt
 ---> Using cache
 ---> b990e299be46
Step 5/6 : EXPOSE 5000
 ---> Using cache
 ---> 6bc486b0977e
Step 6/6 : CMD ["python", "index.py"]
 ---> Using cache
 ---> a4b9fe3cb4b0
Successfully built a4b9fe3cb4b0
Successfully tagged flask-app:latest
Enter fullscreen mode Exit fullscreen mode
  • Check the built image:
user@docker:~$ docker images
REPOSITORY                                                       TAG                 IMAGE ID       CREATED          SIZE
flask-app                                                        latest              a4b9fe3cb4b0   23 minutes ago   58.5MB
Enter fullscreen mode Exit fullscreen mode
  • Run container from the image "flask app":
user@docker:~$ docker run --name my-flask-app -d -p 5000:5000 flask-app
8724ba077eafa3db313631d602398a92df7914557cd305fcc130c9a10fd561dc
Enter fullscreen mode Exit fullscreen mode
  • List running container:
user@docker:~$ docker ps -a
CONTAINER ID   IMAGE       COMMAND             CREATED         STATUS         PORTS                                       NAMES
8724ba077eaf   flask-app   "python index.py"   3 seconds ago   Up 3 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   my-flask-app
Enter fullscreen mode Exit fullscreen mode
  • Check with curl command to reach the app:
user@docker:~$ curl http://127.0.0.1:5000
Hello World!
Enter fullscreen mode Exit fullscreen mode
  • It will run until stop or remove. You can check the STATUS with docker ps -a command. If you want to remove the running container:
user@docker:~$ docker ps -a
CONTAINER ID   IMAGE       COMMAND             CREATED         STATUS         PORTS                                       NAMES
8724ba077eaf   flask-app   "python index.py"   5 minutes ago   Up 5 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   my-flask-app
user@docker:~$ docker container rm -f my-flask-app
my-flask-app
user@docker:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
Enter fullscreen mode Exit fullscreen mode
  • You can also remove the image from your local machine:
user@docker:~$ docker images
REPOSITORY                                                       TAG                 IMAGE ID       CREATED          SIZE
flask-app                                                        latest              a4b9fe3cb4b0   28 minutes ago   58.5MB
user@docker:~$ docker image rm -f flask-app
Untagged: flask-app:latest
Deleted: sha256:a4b9fe3cb4b0a99d0de4c93d7718bd124891306459a4b4c856d725ac1478dd07
Deleted: sha256:6bc486b0977ef198ba26225f1a359591abcf1fdddb4bd6ad3b1f02fd0a3fbdd5
Deleted: sha256:b990e299be46afdaafdf7e24dec7c1aef1f057bbaa1818e3498fd661a4d71a94
Deleted: sha256:c2a2ee2575cb91b1483fa714a952675055e2af887b5fc6e670cfb8380f44c730
Deleted: sha256:4c3f1a3fea8a182b85844a73c8b6578ce90e2e97f3357026fd47fe607291f13e
Deleted: sha256:84c21ccd8630737d4d7c544d4784b87733549f35951ab47ec57036234a804f76
user@docker:~$ docker images
REPOSITORY                                                       TAG                 IMAGE ID       CREATED          SIZE
Enter fullscreen mode Exit fullscreen mode
  • We can also save the image on Docker Hub (public Docker repository). Before pushing to DockerHub, it requires to create account on DockerHub.

  • We have to tag to push DockerHub => account/imageName:tag:

user@docker:~$ docker tag flask-app omerbsezer/dev-to-flask-app:latest
user@docker:~$ docker push omerbsezer/dev-to-flask-app:latest
The push refers to repository [docker.io/omerbsezer/dev-to-flask-app]
9d655bfa54cf: Pushed
4d23fdc66abc: Pushed
4521f5110372: Pushed
d4715e0e4764: Mounted from library/python
ec2af1fd5aad: Mounted from library/python
e19a9440d9c3: Mounted from library/python
a0904247e36a: Mounted from library/python
latest: digest: sha256:765ef797b35697edc4a5b7ad1e293e341e0318184716a568627793e6daea4fed size: 1782
Enter fullscreen mode Exit fullscreen mode
  • After pushing, it can be viewed from DockerHub:

Image on DockerHub

Conclusion

This post shows how to create Dockerfile, Docker image, docker container using sample Flask project. Please have a look below menu for other Docker content, if you haven't seen before.

If you found the tutorial interesting, I’d love to hear your thoughts in the blog post comments. Feel free to share your reactions or leave a comment. I truly value your input and engagement 😉

Follow for Tips, Tutorials, Hands-On Labs for AWS, Kubernetes, Docker, Linux, DevOps, Ansible, Machine Learning, Generative AI, SAAS.

Top comments (0)