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!