DEV Community

Emma Donery
Emma Donery

Posted on

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

image

Docker

Docker is an open source tool which is used to deliver software 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, it’s 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, request

app = 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)
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM python:3.6-alpine

COPY . app

COPY ./requirements.txt /app/requirements.txt

WORKDIR app

EXPOSE 5000:5000

RUN pip install -r requirements.txt

CMD [ "python", "server.py" ]
Enter fullscreen mode Exit fullscreen mode

Build your docker image

>>> docker build -t myimage .  
Enter fullscreen mode Exit fullscreen mode

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    
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)