DockerFile Python Flask

Dockerlize a Python Flask Application

We will show, how to do write a docker file for a flask application and run a container for Flask.

Github link — https://github.com/bikramatmedium/docker/tree/main/Dockerfile/python

  1. Create a server.py flask file
$ vi server.py

2. Create requirements.txt file

$ vi requirements.txt

3. Create a Dockerfile that contains instructions to build the flask application.

$ vi DockerFile

Now we have three files dockerfile, app.py, and requirement.txt files.

Create Docker Image now

$ docker build -t flask-app .

4. Create a Container with the docker image created.

$ docker run -d -p 5000:5000 --name flask-app flask-app:latest

An explanation of this command is as below

  • docker run is used for creating a docker container.
  • -d option is used to run a container in the background and print container ID.
  • -p option is used for port mapping between container and Host machine.
  • — name option is used to assign a name to the container.
  • In the end, we provide the name of the docker image from which we wish to run our container.

This will start the Python flask app on port 5000. If you visit http://ip:5000 in your browser.