When it comes to learning, I tend to retain info best by doing it myself (and failing many times in the process), and then writing a blog about it. So, surprise: I decided to create a blog explaining how you can get a Flask app up and running with Docker! Doing this on my own helped connect the dots when it came to Docker, so I hope it helps you as well.
You can follow along with my repo here:
https://github.com/ChloeCodesThings/chloe_flask_docker_demo
First, I created a simple Flask application. I started by making a parent directory and naming it chloes_flask_demo.
$ mkdir chloes_flask_demo $ cd chloes_flask_demo
Then, I made a folder called web. The files for my Flask app are kept in here.
$ mkdir web $ cd web
Then, I created a file called app.py where I added the following code:
from flask import Flask, render_template app = Flask(__name__) @app.route('/') def hello_whale(): return render_template("whale_hello.html") if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
NOTE: As you can see from my repo and code sample, I added a fun giphy and colorful text to mine. To run a simpler version as you follow along you can add:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_whale(): return ‘Whale, Hello there!’ if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
And I’ll save this inside of my web folder.
I need Flask to run this application, so I’ll also need a requirements file. This will hold the software we need to be installed inside the container. I’ll create the following requirements.txt folder inside of web folder as well:
Flask==0.12
Now, I’ll make a Dockerfile that will create our image and then deploy it. Name this Dockerfile and write the following:
FROM ubuntu:latest RUN apt-get update -y RUN apt-get install -y python-pip python-dev build-essential COPY . /app WORKDIR /app RUN pip install -r requirements.txt ENTRYPOINT ["python"] CMD ["app.py"]
To build this image, I’ll run:
$ docker build -t flask-sample:latest .
And to run the container, I’ll run:
$ docker run -d -p 5000:5000 flask-sample
Now, if I do a docker ps -a…
Cool! My container is up and running on port 5000! Now, if I go to localhost:5000…
Whale, look at that! There’s my Flask app!
Cool- so, now I Dockerized my Flask app. Let’s run it using Docker Compose.
First we’ll need to remove our container running at port 5000. You can do that by typing docker ps -a, into your terminal, and copying the container ID to write:
docker stop [YOUR CONTAINER NUMBER HERE]
Now, in our flask_demo directory, I need to make a docker-compose.yml with the following in it:
web: build: ./web ports: - "5000:5000" volumes: - .:/code
That’s it! From our chloe-flask-demo directory, run…
docker-compose up
Now, go to port 5000, and…
Hey, there it is again!
You can run docker ps -a to see the details…
Woo! We did it! We Dockerized our Flask App and ran it inside a Docker container, and we ran our app inside a Docker container using Docker compose.
If you have any questions, feel free to comment below. I hope this makes getting started with Docker and Flask a little easier for you. If you’re getting started with Docker, check out my other Hello, Whale blogs! 🐳
Ready to try Codefresh, the CI/CD platform for Docker/Kubernetes/Helm? Create Your Free Account Today!
This information is impressive; I am inspired by your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
Hey, thanks for your effort on this post!!
I’ve been searching for a simple approach like this for the study im doing. Although I know I should understand better how Docker works (I started to do an docker video course last month), I just wanted to test a simple task on a Python course, and it asked to install Flask, but I was wondering if I could dockerize it instead of installing on the main system (Ubuntu 16.04).
Regards!
Other than the Missing the hello_whale.html file very good tutorial
here is hello_whale.html
Whale
whale
Hello. The hello_whale.html is already in the repository mentioned in the article https://github.com/ChloeCodesThings/chloe_flask_docker_demo/blob/master/web/whale_hello.html
Hi. Useful article. Thanks for putting it together. A couple of suggestions:
1) When I initially tried to run the “docker build …” command, I was getting an error “E: Unable to locate package python-pip”. I realized this was because I am running behind a firewall. It might be helpful to note that users probably need to add ENV commands for their proxy settings to the Dockerfile when running behind a firewall.
2) The “docker build” command seemed to be downloading a lot of stuff. I changed the FROM command to “FROM python:3.7” and removed the two apt-get commands. The docker build was much quicker and the example still works fine.
What is this part of the docker-compose for? It’s not mentioned anywhere in the text…
volumes:
– .:/code
It mounts the source code inside the docker container. This way you can edit your code with your favourite editor and see your changes in the docker container (very useful during development). More info here https://docs.docker.com/storage/volumes/
From Kostis Kapelonis post, I was under the impression I could edit the app.py file and refresh my browser to see the changes. However, the page did not change. I guess I did not understand the “edit your code…and see your changes”. I also tried Ctrl-C the composer process and then run it again but it still see the old file.
The volume mounting only saves you from rebuilding your docker container everytime you change code. But to get live code reload your framework needs to support it as well. I think that in Flask you need an environment variable to enable this.
The first 3 lines of the Docker file can be replaced with
FROM python
or if you want to control the version and OS you can use a tag.
FROM python:3.5-alpine
In the text version (i.e. not the whale gif), I believe this line:
return ‘Whale, Hello there!’
should be:
return(‘Whale, Hello there!’)
My container would crash without any info.
Hello, I followed all the steps mentioned above, I was unable to get by index.htm page, I tried using localhost:5000 and 127.0.0.1:5000. Can you help me?
Hello. Flask has changed a lot since this article. Feel free to see our quick start guide that has the latest version. https://codefresh.io/docs/docs/getting-started/create-a-basic-pipeline/
Your Dockerfile is broken because ubuntu:latest is Ubuntu 20.04 and it doesn’t have access to python-pip.
Investigated this for you on StackOverflow and was provided the solution within minutes!
https://stackoverflow.com/questions/61564756/install-python-pip-using-apt-get-via-ubuntus-apt-get-in-dockerfile/
The solution was to change
FROM ubuntu:latest
to
FROM ubuntu:18.04
You might want to make the change to your Github repo.
Thanks for the cool article!
Happy programming!
I really really really can’t thank you enough for putting up this tutorial!!!!!
I had been stuck on setting up the docker image properly as localhost wasn’t accessible from outside the container. After following your tutorial especially your dockerfile I was finally able to do it.
I get the below error when i try to install flask. This is on Windows 10 and i am using Powershell on administrator mode.
The command ‘/bin/sh -c pip install -r requirements.txt’ returned a non-zero code: 4294967295: failed to shutdown container: container 8d2fa7345f32d82d99936674718d67696eed28be14028193e6f66aeddc573243 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110): subsequent terminate failed container 8d2fa7345f32d82d99936674718d67696eed28be14028193e6f66aeddc573243 encountered an error during hcsshim::System::waitBackground: failure in a Windows system call: The virtual machine or container with the specified identifier is not running. (0xc0370110)
this just isn’t even fun
Can someone explain what the commands are actually doing?