Schedule a FREE onboarding and start making pipelines fast.

Hello Whale: Getting Started with Docker & Flask

Docker Tutorial | June 12, 2017

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! 🐳

New to Codefresh? Schedule a FREE onboarding and start making pipelines fast. and start building, testing and deploying Docker images faster than ever.

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

Reader Interactions

Enjoy this article? Don't forget to share.

Comments

  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!

Comments

Your email address will not be published. Required fields are marked *

Follow me on Twitter