Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 18, 2021 09:48 pm GMT

Getting Started With Flask and Docker: Containerize your first flask application

image

Docker

Docker is an open source tool which is used to deliver softwares in packages called containers and in the recent days it has changed we think, ship and deploy our applications to the server.

The main advantage of docker is that it enables easy deployment to the cloud, since containers have the added benefit of running anywhere without facing dependencies conflicts.

Flask

Flask is a web micro-framework, its a Python module that lets you develop web applications easily without providing rules and specific methodology to follow.

Flask application structure:

The server.py file - This is our main and entry point file that contains all flask logic to run our application.

The templates folder - By default, flask will look for any markup files in the templates folder, but you can configure to have your html files in a different folder.

The static folder - In our flask applications we uses static folder to keep our static files, these include images, JavaScript and CSS code

NB:
When creating python application it is advisable to keep virtual environment so that you don't interfere with the global local environment, also since we will creating a minimalistic flask application we won't be using static files hence we don't need the static folder.

server.py

from flask import Flask, render_template, requestapp = Flask(__name__)@app.route('/register', methods = ["GET", "POST"])def register():   if request.method == "POST":       username = request.form["username"]       email = request.form["email"]       password = request.form["password"]   return render_template('register.html')if __name__ == '__main__':    app.run(debug=True)

Templates/register.html

<html>  <head>    <title>Welcome to our registration page</title>  </head>  <body>  <form>    Username <input type = "text" name= "username" />     Email <input type = "text" name = "email" />    Password <input type = "text" name = "password" />  </form>  </body></html>

Dockerfile

FROM python:3.6-alpineCOPY . appCOPY ./requirements.txt /app/requirements.txtWORKDIR appEXPOSE 5000:5000RUN pip install -r requirements.txtCMD [ "python", "server.py" ]

Build your docker image

>>> docker build -t myimage .  

Run your docker image

Now that we have successfully built our image let run it using docker run command, the following command will run our myimage image.

>>>docker run -t -i myimage    

Bravo! you have containarized your first flask application, alternatively you can use docker compose which is used to run multiple docker containers to simplify the process where we use just one command docker-compose up to build and run our docker container and docker-compose down to stop the container.

Thank you for reading, please feel free to leave additional insights in the comment section below.

Let Connect

You can get in touch with me on linkedin or twitter


Original Link: https://dev.to/emma_donery/getting-started-with-flask-and-docker-containerize-your-first-flask-application-1f43

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To