Flask+Mongo+Redis+Nginx with Gunicorn, Docker Deployment. Part 1

Prerequsites:

  • Your Flask project/application ready.
  • Linux Server
  • Docker
  • Docker-composer

Flask Application

Please prepare your flask application. Your application should be runnable on your local host. Your hello world application you can follow this link.

Docker file for flask application

Create Docker file which is used by docker-composer to build docker image of flask application. File will be in root directory of the project where main.py exist.

FROM python:3.7

WORKDIR /code

ENV FLASK_APP=main.py

ENV FLASK_RUN_HOST=0.0.0.0

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

EXPOSE 5000

COPY . .

Docker entry point file

Create a file that name would be anything, in my case i have used. File will be in root directory of the project where main.py exist.

docker-entrypoint-flask.sh
#!/usr/bin/env bash

echo "Start flask dev server"
flask run

Docker composer file

Create a docker-composer.yml file to maintain all the container from single utility. File will be in root directory of the project where main.py exist.

version: "3.0"
services:

  flaskapp:
    container_name: "my-flask-app"
    restart: always
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "5000:5000"
    command: ./docker-entrypoint-flask.sh

Note: Move your code and above files to server, run bellow commands to check your flask application is working on flask dev server.

# Build your flask application,
sudo docker-composer build
# Run your flask application in debug mode.
sudo docker-composer up flaskapp
# Run your flask application as background container.
sudo docker-composer up -d flaskapp

Note: You should be able to see your hello world message at you ip address. like: http://ip-address/

Flask+Mongo+Redis+Nginx with Gunicorn, Docker Deployment. Part 1
Show Buttons
Hide Buttons