- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 and deploy a flask application inside docker
Docker allows you to build, manage and deploy applications inside containers. It provides a packed environment and allows developers to make portable applications by containerizing them. You can easily build a flask application, manage it and make it portable all using a single technology, docker. You can use similar techniques to build and deploy other python frameworks as well.
In this article, we will be discussing how to build a simple application using flask and convert that into a docker image by containerizing it. You can follow the steps mentioned below to do the same.
Create a new project folder. Let’s name it flask_project.
Move to that directory and create a python file. Let’s call it app.py.
Write a simple flask code inside the file. Check out 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()
You can copy the above code inside the app.py file which displays a simple welcome statement.
Create another file called requirements.txt. This file would contain a list of packages to be installed. For this project, we only need Flask library to be installed. Hence, inside the file, only include “flask” (Without inverted commas).
Now, inside the root directory, create another file called dockerfile. This file would contain all the commands that would help you build the docker image.
Inside the dockerfile, copy the following code.
#Create a ubuntu base image with python 3 installed. FROM python:3 #Set the working directory WORKDIR /usr/src/app #copy all the files COPY . . #Install the dependencies RUN apt-get -y update RUN pip3 install -r requirements.txt #Expose the required port EXPOSE 5001 #Run the command CMD [“python3”, “./app.py”]
The dockerfile created above creates an ubuntu base image with python installed in it. It sets a working directory and then copies all the files from the host machine to the docker server. It then runs an update command and installs flask which is mentioned in the requirements file. It then exposes the port 5001 which would run our flask application and using CMD command, it runs the app.py file.
Now, build the image using the following command. Make sure you are in the root directory.
sudo docker build -t flaskproject . (don’t forget the dot)
After building the image, run the bash inside the docker container using an interactive shell through the following command.
sudo docker run -ti -p 5001:5001 flaskproject bash
The above command runs the image by connecting the ports and opens the bash.
Inside the bash, run the command -
You will see something like this appearing on the screen.
Serving Flask app "app" (lazy loading)
Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
Debug mode: on
Running on http://0.0.0.0:5001/ (Press CTRL+C to quit)
Restarting with stat
Debugger is active!
This means that your flask application is successfully running on the container can be accessed through port 5001 on your local machine.
Open a browser and go to the url - http://0.0.0.0:5001/
You will see the following message - “Welcome to TutorialsPoint”
To conclude, using these steps you can easily build and deploy a flask application inside your docker container and can access the application by connecting the ports to your local machine.
- Build a flask App using Docker Compose
- Mounting a volume inside docker container
- Installing Linux Packages Inside a Docker Container
- How to force a clean build of a Docker Image?
- How to run a command inside Docker Container?
- Working with Java inside Docker Container
- How to build a java web application using SAP platform and HANA database?
- Page build in HTML and wanted to load into JS view in SAPUI5 application.
- How to show Matplotlib in Flask?
- Maintain and Manipulate Docker Containers
- How to backup and restore a Docker Container?
- From inside of a Docker container, how do I connect to the localhost of the machine
- Difference between Docker Swarm and Kubernetes
- Creating a MySQL Docker Container
- How to deploy python modules on Heroku?