5
\$\begingroup\$

I am not a Docker guru or expert in Flask or Redis. However, I need to leverage these technologies. I managed to cobble something together that works and would like to submit it for review.

The MWE repository here has three main directories:

  1. backend: the task-based Flask API
  2. frontend: a Nuxt app
  3. pyapp: a custom python module used by backend

In addition to the docker file, I fear the flask app and worker are not properly set up.

Flask

backend/manage.py

import redis
from flask_script import Server, Manager
from rq import Worker, Queue, Connection
from uwsgi import app
listen = ['high', 'default', 'low']
manager = Manager(app)
manager.add_command(
 'runserver',
 # Server(port=5000, use_debugger=True, use_reloader=True))
 Server(port=9061, use_debugger=True, use_reloader=True))
 # flask is on 9061 via docker
@manager.command
def runworker():
 redis_url = app.config['REDIS_URL']
 redis_connection = redis.from_url(redis_url)
 with Connection(redis_connection):
 #worker = Worker(app.config['QUEUES'])
 worker = Worker(map(Queue, listen))
 worker.work()
if __name__ == '__main__':
 manager.run()

backend/uwsgi.py

import os, sys
from werkzeug.wsgi import DispatcherMiddleware
from app.factory import create_app
app = create_app()
# application = DispatcherMiddleware(app)

backend/deploy.sh

exec gunicorn --bind 0.0.0.0:9061 --reload uwsgi:app

backend/app/factory.py

'''-----------------------------------------------------------------------------
IMPORTS
-----------------------------------------------------------------------------'''
import os
# for main flask app
from flask import Flask
from flask_cors import CORS
# blueprints of the app
from .api.views import (bp as bp_api)
from . import settings
from pyapp import __name__, __version__
def create_app():
 app = Flask(__name__)
 app.register_blueprint(bp_api, url_prefix='/api')
 app.config.from_object(settings)
 CORS(app)
 return app

backend/app/settings.py

import os, sys
APP_DIR = os.path.dirname(os.path.realpath(__file__))
SECRET_KEY = os.getenv('SECRET_KEY', 'not_secret')
LISTEN = ['high', 'default', 'low']
REDIS_URL = os.getenv('REDIS_URL', 'redis://localhost:6379')
MAX_TIME_TO_WAIT = 10

Docker

docker-compose

My docker-compose file looks like:

version: '3'
services:
 nuxt: # frontend
 image: frontend
 container_name: nuxt
 build:
 context: .
 dockerfile: ./frontend/Dockerfile
 restart: always
 ports:
 - "3000:3000"
 command: "npm run dev"
 environment:
 - HOST
 volumes:
 - ./frontend/assets:/src/assets
 - ./frontend/components:/src/components
 - ./frontend/layouts:/src/layouts
 - ./frontend/pages:/src/pages
 - ./frontend/plugins:/src/plugins
 - ./frontend/static:/src/static
 - ./frontend/store:/src/store
 nginx:
 image: nginx:1.17
 container_name: ngx
 ports:
 - "80:80"
 volumes:
 - ./nginx:/etc/nginx/conf.d
 depends_on:
 - nuxt
 flask: # backend
 image: backend
 container_name: flask
 build:
 context: .
 dockerfile: ./backend/Dockerfile
 command: bash deploy.sh
 env_file:
 - .env
 environment:
 - REDIS_URL
 - PYTHONPATH
 volumes:
 - ./backend/app:/app/app/
 - ./backend/manage.py:/app/manage.py
 ports:
 - '9061:9061'
 expose:
 - '9061'
 depends_on:
 - redis
 worker:
 image: backend
 container_name: worker
 command: python3 manage.py runworker
 depends_on:
 - redis
 env_file:
 - .env
 environment:
 - REDIS_URL
 - PYTHONPATH
 redis: # for workers
 image: redis:5.0.3-alpine
 ports:
 - "6379:6379"
 expose:
 - '6379'

Flask service

The Flask docker file looks like:

FROM debian:jessie-slim
RUN apt-get update && apt-get install -y \
 git \
 python3 \
 python3-pip
# Layer requriments and install before copying files as
# requirments are less likely to change
ADD ./backend/requirements.pip /app/
RUN pip3 install -r /app/requirements.pip
# add custom python module
ADD ./pyapp /pyapp
RUN pip3 install -e /pyapp
# Change into app
WORKDIR /app
# Add contents of "flask_app" sub-project dir to /app/
ADD ./backend /app/

Nuxt service

The Nuxt dockerfile looks like:

FROM node:10.15
ENV APP_ROOT /src
RUN mkdir ${APP_ROOT}
WORKDIR ${APP_ROOT}
# changed context from inside nuxt_app to this repo's root
# ADD . ${APP_ROOT}
ADD ./frontend ${APP_ROOT}
RUN npm install
RUN npm run build
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 20, 2019 at 18:24
\$\endgroup\$
1
  • \$\begingroup\$ Your repository diverged a little since this post was done. I'll review this based on what's posted here noting that splitting the docker-compose for different environments like you've done is part of the review. \$\endgroup\$ Commented Nov 1, 2019 at 1:34

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.