Create your FREE Codefresh account and start making pipelines fast. Create Account

Hello Whale: Getting Started with Docker & Flask

3 min read

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.

Then, I made a folder called web. The files for my Flask app are kept in here.

Then, I created a file called app.py where I added the following code:

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:

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:

Now, I’ll make a Dockerfile that will create our image and then deploy it. Name this Dockerfile and write the following:

To build this image, I’ll run:

And to run the container, I’ll run:

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:

Now, in our flask_demo directory, I need to make a docker-compose.yml with the following in it:

That’s it! From our chloe-flask-demo directory, run…

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!

Avatar

About Chloe Condon

Developer Evangelist at Codefresh. Hackbright Alumni/Ambassador/Mentor/frequent conference speaker. Former actress, and one of the only humans who can code while singing the entire score of "Pirates of Penzance".

Enjoy this article? Don't forget to share.

5 responses to “Hello Whale: Getting Started with Docker & Flask

  1. 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.

  2. 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!

  3. 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.

Comments

* All fields are required. Your email address will not be published.

Related Posts

Get a head start on building better pipelines!

Schedule a demo with a Codefresh expert today.

Request Demo