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:

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 where I added the following code:

from flask import Flask, render_template
app = Flask(__name__)
def hello_whale():
    return render_template("whale_hello.html")
if __name__ == '__main__':, host='')

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__)
def hello_whale():
    return ‘Whale, Hello there!’
if __name__ == '__main__':, host='')

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:

FROM ubuntu:latest
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD [""]

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:


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

  build: ./web
   - "5000:5000"
   - .:/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!


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

  1. Disharathod says:

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


  3. Other than the Missing the hello_whale.html file very good tutorial

    here is hello_whale.html



  4. M. Laymon says:

    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.

  5. What is this part of the docker-compose for? It’s not mentioned anywhere in the text…

    – .:/code

    1. Kostis Kapelonis says:

      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

      1. Jacob Rael says:

        From Kostis Kapelonis post, I was under the impression I could edit the 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.

        1. Kostis Kapelonis says:

          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.

  6. Mike Finch says:

    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

  7. Jacob Rael says:

    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.

  8. Srinivas Machiraju says:

    Hello, I followed all the steps mentioned above, I was unable to get by index.htm page, I tried using localhost:5000 and Can you help me?

  9. James Dekker says:

    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!

    The solution was to change

    FROM ubuntu:latest


    FROM ubuntu:18.04

    You might want to make the change to your Github repo.

    Thanks for the cool article!

    Happy programming!

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

  11. 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)

  12. this just isn’t even fun

  13. Can someone explain what the commands are actually doing?

Leave a Reply

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