Dockerize your Flask application
In this article, we are gonna see how to dockerize your existing Flask application. We are going to create an image as well as create a docker-compose file so that our services run in an isolated environment.
One of the disadvantages of using only DockerFile is that we would have to build the whole image whenever we make any changes, whereas in the docker-compose we only detect the changes and build it on top of the already existing image.
Step 1: Create a simple Flask app
As our main focus is on Docker, here we are going to use a simple Flask app with only the root route that returns Hello.
We must also create requirements.txt file which is a configuration file to install specified packages required to run our application. In our case we only need Flask, so specify flask inside our requirements.txt file.
flask
Step 2 : Creating DockerFile
Now that we have a simple Flask app, let us create a DockerFile.
Let us now understand the above code
# Syntax - from [base_image_name] : [tag_name]from python:3.7-alpine
We would have first chosen a base image. There are many official docker images available to us. In our case, we are using python
image as we need to run a Flask app. The from
command is used to fetch our base image.
Docker tags are used to inform us about specific image version/variant.
Here we have 3.7 indicating that we are using Python image with version 3.7
WORKDIR /app
With this line, we are specifying that our current working directory is app
using the WORDIR
command.
COPY . /app
We now copy all the code into our present working directory which in our case is app
as we specified earlier.
RUN pip --no-cache-dir install -r requirements.txt
If we want any command to run while our image is getting built, we use RUN
command. In this case, we want to run requirements.txt which automatically installs Flask.
ENTRYPOINT ["python"]
CMD ["app.py"]# same as python app.py
ENTRYPOINT
is used to specify the commands that we want to execute when our container is created from the image. It is used to make our container executable. In our case, the file that we want to run is app.py
Step 3: Docker compose file
We are now going to create a docker-compose.yml file.
version: '3'
We have to first specify the version of Docker. For this, we are using 3rd version.
services:
flask_app:
Under the services, we can give the name of our application. Since this is a Flask app, we named it flask_app
ports:
- "5000:5000"
# <host_port> : <container_port>
Here we are mapping the port 5000
of the host to port 5000
of our container.
volumes:
- .:/app
volumes
is used to mount our source code from our computer to the target path in the container. In our case, we are mounting all our source directory to app
which is our current working directory.
Now, all that is left is to do is check if our application works.
sudo docker-compose up
Now go to localhost:5000
and we will see Hello
displayed there.
GitHub Link : https://github.com/Hilda24/Docker-Flask-Template
Conclusion :
We have learned how to dockerize a Flask application. Hope you found this useful.
Keep Exploring!!!