
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Build a Flask App Using Docker Compose
Docker Compose allows you to build multi-container Docker applications. If you are working on a microservice project where you have different nodes working on different parts of the project, Docker Compose is exactly what you need. Using Docker Compose, you can work on different components of the project on different Docker Containers and combine them to create a single application.
In this article, we are going to discuss how to build a Flask application which uses Python modules and we will try to run it inside a Docker Container using Docker Compose.
First, you need to install Docker Compose in your local machine. To install Docker Compose, you can use either of the following methods −
Using package manager
sudo apt−get install docker−compose
From the official repository
You need curl to download files from the official repository.
sudo apt−get install curl sudo curl −L "https://github.com/docker/compose/releases/download/1.24.0/docker−compose−$(uname −s)−$(uname −m)" −o /usr/local/bin/docker−compose sudo chmod +x /usr/local/bin/docker−compose
The above sequence of commands downloads and installs curl from apt package manager. After that using curl, it downloads the latest version of Docker Compose from it’s official github repository and then using the chmod command, it gives the user the executable permission.
After you have downloaded Docker Compose in your local machine, create a directory structure as follows.
Create a folder named “application” and inside that create another folder named “web” which will contain all our flask files.
Move to the “web”directory and create a python file called “app.py”. Create a simple flask application inside the file or paste the code below.
#Import the flask module from flask import import Flask #Create a Flask constructor. It takes name of the current module as the argument app = Flask(__name__) #Create a route decorator to tell the application, which URL should be called for the #described function and define the function @app.route('/') def tutorialspoint(): return "Welcome to TutorialsPoint" #Create the main driver function if __name__ == '__main__': #call the run method app.run(debug=True,host='0.0.0.0')
The above flask code creates a simple flask application and prints “Welcome to TutorialsPoint”. After you have created the python file, create the dockerfile with the following code.
FROM python:3 RUN apt−get −y update RUN apt−get install −y pip3 build−essential COPY . . RUN pip3 install −r requirements.txt ENTRYPOINT ["python3"] CMD ["app.py"]
The above dockerfile creates a python 3 image, runs updates and installs essential python packages. It then uses the requirements.txt file mentioned below to install the flask library and defines the entrypoint as the flask app which we just created in the app.py file.
Create a file called requirements.txt inside the web folder and paste the following.
Flask
Now, go to the parent directory called application and create a docker-compose.yml file with the following contents.
web: build: ./web ports: − "5000:5000" volumes: − .:/code
The above file builds the image from the dockerfile in the “web” directory and then exposes 5000 port to the container’s 5000 port. It then mounts the current directory to /code on docker container.
You can now build the docker image “application” from the directory called “web” using the following command.
sudo docker−compose up
To conclude, in this article we saw how to install docker compose, build a simple flask web application, create a dockerfile which builds the image and then deploy then deploy the application as a service using Docker Compose. The above mentioned steps is the most simplistic approach to create a web application using Docker Compose.