Dockerizing a Flask App

Anjelica A
5 min readMay 13, 2020

Dockerizing a project is fairly easy, as there are many tutorials that docker provides that can make the learning process a lot easier. Dockerizing is the process of converting an application or project to run within a docker container. This is very useful for testing virtual environments, and it allows developers to quickly run their applications anywhere. Recently, I finished dockerizing a flask site I made.

Getting Started

First, make sure you have docker installed. Instructions to install docker are linked here. You’ll know docker is successfully installed when there is a whale icon at the top of your screen.

This is how it appears on my screen, make sure you have the newest version of docker!

Creating a Dockerfile for Flask

Pull up an existing flask project or make a quick new one, then create a file called “Dockerfile” (no extension, capital D) in the same folder as the flask project. Make sure you have a requirements.txt in this project with all of its dependencies. An easy way to do this is:

pip3 freeze > requirements.txt

Here is what my Dockerfile looks like for my project:

My Dockerfile

Breaking Down the Dockerfile

As you can see, the Dockerfile looks like a set of steps or instructions. I’ll go over what every line means.

FROM python:alpine3.7 pulls python’s image from the docker hub. This is pretty much your way of saying that this project is a python project, pull up the python image from docker.

COPY . /app is the command that copies the entire flask app into the container.

WORKDIR /app is the command that sets the working directory for the project.

RUN pip3 install -r requirements.txt is the command that goes into the requirements.txt we made, and installs all the dependencies into the container. I had an issue with my requirements.txt, because my version of pip was out of date. If you have a similar issue, it can be fixed with:

pip3 install  -- upgrade pip

Make sure all your dependencies are up to date!

EXPOSE 5000 refers to what port flask is running on. I typically use localhost:5000 for my flask apps.

ENTRYPOINT [“python”] & CMD [ “app.py” ] is the equivalent to “python app.py” which runs the program.

Building a Container

Now that you have your Dockerfile, you can build the container for your flask app. Go into your terminal and type this command:

docker build -t (yourprojectnamehere):latest .
Building a Container

As you can see, Docker takes all the instructions you laid out in the dockerfile, copying over the app, installing the dependencies, etc. Once your build is successful, you can try out your container by running either or these two commands:

docker run -d -p (port):(port) (yourprojectname) ordocker run (yourprojectname)

As you can see, when you run the container, it behaves almost identically as it would if you ran it locally.

Running my app locally
Running my app in a container

Deployment

I deployed my app using Caprover, and had a lot of issues during the process. Make sure you have a Digital Ocean account, and a Caprover droplet and a domain, you’ll need it for Caprover deployment.

Setting up your CapRover Server

First, you need to get the IP address from your droplet. It’s usually underneath the name of the droplet on the menu. Then, create an A record on your domain’s DNS records, using the droplet’s IP. For host name, I used something fairly simple like *.dev. Keep in mind the host name will appear in the URL of your project. Finally, go to your terminal and type this command:

caprover serversetup

When prompted, say yes or y. For the IP address for your server, use the droplet’s IP, and for the CapRover server root domain, use (hostname).(yourdomain). For example, if your host name is *.dev and your domain is busybee.com, it would be formatted as dev.busybee.com.

Set up your password, and hold onto it, you’ll need it to deploy your project. Enter your email, and you should get a message saying everything was a success.

Go to the URL of your server root domain and enter your password, and on that page, go into “apps” and create a new app. Make sure your port is correct, by default it’s set to 80.

Finally, go into your project and type this command:

caprover deploy

Congratulations! You have successfully deployed your dockerized Flask app!

Issues along the Way…

Picture by Tim Gouw on Unsplash

While deploying, I had a lot of issues with the Caprover server setup. I had issues with the SSL, and would get tons of connection time outs. How I resolved it was by destroying the droplet, and rebuilding a new one. I waited 20 mins after updating the DNS before trying again, and was able to successfully build my server.

Conclusion

Docker is a very handy tool for developers, I find it very convenient to test my apps in containers, and building containers is quick and generally easy. Deploying on CapRover allows me to use my own domain for deploying projects, and I can easily make edits to my site.

--

--