We’re going to create a Python Flask application in docker. You learn how to use Docker on your local computer and how to run and create docker images.

What is docker?

Docker is a software container system that allows build containers from the logic of your application to automate their deployment.

Containers help developers break the program into tiny pieces including collections, dependencies, and so on and build a package from it

Related course: Python Flask: Create Web Apps with Flask

Why docker?

  • Docker provides security for apps running in a shared environment.
  • Rapid development of the process.
  • Simple to scale and also to monitor.
  • Ensure a consistent environment for new constructions.
  • Open the source, and it’s free to be used.

Install

You need to install python3, python3-pip and flask

$ sudo apt-get install python3
$ sudo apt-get install python3-pip
$ sudo pip3 install flask

Example

Create a new directory “myapp” for your project. To create an image we need a program, as I described earlier, you can use a python flask app.

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
return 'Hello World'

if __name__ == '__main__':
app.run()

Start it with python app.py, and open the url localhost:5000. You should see the output in your web browser.

python flask app

Docker file

Docker gives us the ability to create custom images with the assistance of Dockerfile. Dockerfile is a plain file containing steps on how to create the image.

python docker

Create a Dockerfile and insert the code into it:

FROM ubuntu 

RUN apt-get update
RUN apt-get install python3-pip
RUN apt-get install flask

ADD app.py /
WORKDIR /

EXPOSE 5000

CMD [“python3”,”app.py”]

So what does this mean?

FROM ubuntu

FROM informs docker from which base image you would like to base the image. In this instance, we are going to create an image that keeps Ubuntu as the base image.

RUN apt-get update 
RUN apt-get install python3-pip
RUN apt-get install flask

The RUN command executes commands inside the image. In this easy example, you’re installing python3-pip and flask packages.

ADD app.py /

The command ADD attaches code to the image.
It supports 2 arguments:

  • a source file
  • a target inside the image to which the data has to be copied.

In this case, we transfer app.py to the/(root) directory of the file.

WORKDIR /

WORKDIR configures the current working directory of the program to execute each of the commands in.In this case, it is configured as / (root).

EXPOSE 5000

EXPOSE: Exposes the port in which to run our app. In this configuration, the Flask app runs on port 5000 by default.

CMD [“python”,”app.py”]

The most important step, the command to start your application.

Build docker image

Open a terminal and type

$ docker build -t hello-world .

The command docker build tells the image from the docker file.

To see all images type the command:

$ docker images

Create and run Docker container

To run your Flask app from the image, you can use the command docker run.

$ docker run --name mycontainer -p 5000:5000 -d <imagename>

If everything went right, you’ll see the same output on localhost:5000

You’ve made your first docker container with Python Flask!

python flask docker

Docker commands

Docker has some commands you may find useful:

# Pause
docker pause <container-id/name>

# Unpause
docker unpause <container-id/name>

# Start
docker start <container-id/name>

# Stop
docker stop <container-id/name>

# Remove
docker rm <container-id/name>

Btw, you can easily Deploy your Flask app

Related course: Python Flask: Create Web Apps with Flask