1

I have a flask app and would like to containerize it through docker. Normally I run the app using flask run and it serves the API at port 8888. However, when I try to run it using docker, the API doesnt start. I build the image using docker-compose build --no-cache and start using docker-compose -f docker-compose.yml up, the container starts. I use docker exec -it covid-api /bin/bash, I get to root@d0659f0c1627:/app# and ls returns nothing. Its empty.

This is my folder structure:

enter image description here

Below is my docker configuration:

FROM python:3.7-stretch
WORKDIR /app

COPY ./badproxy /etc/apt/apt.conf.d/99fixbadproxy

RUN apt-get update \
 && apt-get install -y curl apt-utils gcc build-essential apt-transport-https ca-certificates \
 && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
 && curl https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql-release.list \
 && apt-get update \
 && ACCEPT_EULA=Y apt-get install -y msodbcsql17 unixodbc-dev mssql-tools locales \
 && echo "en_US.UTF-8 UTF-8" > /etc/locale.gen \
 && locale-gen

COPY .flaskenv ./
COPY requirements.txt ./
RUN pip install -r requirements.txt

ENV FLASK_ENV=development
ENV PATH="/opt/mssql-tools/bin:${PATH}"

COPY . ./

CMD python setup.py develop

CMD ["flask", "run", "-h", "0.0.0.0", "-p", "8888"]

Docker-compose:

version: '3.7'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '6000:6000'
    environment:
      SECRET_BASE_KEY: 'development-secret'
    volumes:
      - .:/app
    restart: unless-stopped
    networks:
      mynetwork:
        aliases:
          - web.myname.test

networks:
  mynetwork:
    driver: bridge

src/app.py:

import logging.config

import os
from flask import Flask, Blueprint
from flask_cors import CORS
from werkzeug.middleware.proxy_fix import ProxyFix
from src.config import default
from src.api.controllers.endpoints.users import ns as users_namespace
from src.api.controllers.endpoints.statuses import ns as status_namespace
from src.api import api
from src.database import db

app = Flask(__name__)
CORS(app)
app.wsgi_app = ProxyFix(app.wsgi_app)
logging_conf_path = os.path.normpath(os.path.join(os.path.dirname(__file__), '../logging.conf'))
logging.config.fileConfig(logging_conf_path)
log = logging.getLogger(__name__)


def configure_app(flask_app):
    flask_app.config['SERVER_NAME'] = default.FLASK_SERVER_NAME
    flask_app.config['SQLALCHEMY_DATABASE_URI'] = default.SQLALCHEMY_DATABASE_URI
    flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = default.SQLALCHEMY_TRACK_MODIFICATIONS
    flask_app.config['SWAGGER_UI_DOC_EXPANSION'] = default.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
    flask_app.config['RESTPLUS_VALIDATE'] = default.RESTPLUS_VALIDATE
    flask_app.config['RESTPLUS_MASK_SWAGGER'] = default.RESTPLUS_MASK_SWAGGER
    flask_app.config['ERROR_404_HELP'] = default.RESTPLUS_ERROR_404_HELP

    return flask_app


def initialize_app(flask_app):
    configure_app(flask_app)

    blueprint = Blueprint('CovidAPI', __name__, url_prefix='/')
    api.init_app(blueprint)
    api.add_namespace(users_namespace)
    api.add_namespace(status_namespace)
    flask_app.register_blueprint(blueprint)

    db.init_app(flask_app)

    return flask_app


app = initialize_app(app)

# def main():
#     initialize_app(app)
#     log.info('>>>>> Starting development server at http://{}/ <<<<<'.format(app.config['SERVER_NAME']))
#     app.run(debug=default.FLASK_DEBUG)


if __name__ == "__main__":
    log.info('>>>>> Starting development server at http://{}/ <<<<<'.format(app.config['SERVER_NAME']))
    app.run(debug=default.FLASK_DEBUG)

Please advice. Any help is highly appreciated.

arunmmanoharan
  • 2,535
  • 2
  • 29
  • 60
  • There is an issue with Dockerfile - you have 2 CMD which means only the last one is considered . Try replacing first CMD with RUN – fly2matrix May 04 '20 at 18:37
  • Replaced CMD with RUN. Doesnt seem to still run. Is it to do with the path? – arunmmanoharan May 04 '20 at 19:08
  • so create a new script (entrypoint.sh) and put this script as CMD, in `entrypoint.sh` perform necessary steps like setup and start app. – fly2matrix May 04 '20 at 19:13
  • So I throw in `CMD python setup.py develop` in entrypoint.sh and call that file in dockerfile? I was following this: http://michal.karzynski.pl/blog/2016/06/19/building-beautiful-restful-apis-using-flask-swagger-ui-flask-restplus/ – arunmmanoharan May 04 '20 at 19:16
  • Even if you didn't have issues with double cmd, you should still have your content from the volume mounted. Try using `./` instead of `.` – The Fool May 04 '20 at 19:29
  • Tried that. Used `docker exec -it container_name /bin/bash` and I went into /app and did `ls -ltr` and it returns `total 0` – arunmmanoharan May 04 '20 at 19:40
  • The `volumes:` block is causing all of the content you install in the image to get hidden and overwritten with a directory from the host (the symptom suggests an empty directory). Does deleting the `volumes:` help? – David Maze May 04 '20 at 20:51
  • @DavidMaze Trying it out. Will let you know – arunmmanoharan May 04 '20 at 20:55
  • @DavidMaze Thanks man. It worked. I am able to see the files copied. The app starts but still not able to hit the endpoint. – arunmmanoharan May 04 '20 at 21:03

1 Answers1

0

Probably the problem is that your project doesn't have a MANIFEST.in file. When the setup.py executed it looks for additional files to include into your project. complete instruction can be found here.

It may look like this for your project folders.

recursive-include src/*
recursive-include src/api *
recursive-include src/api/controllers *
recursive-include src/api/controllers/endpoints *