I found this: https://github.com/tiangolo/meinheld-gunicorn-flask-docker after multiple failed attempts to follow along various tutorials on the web to containerize my app to build a CI pipeline. My app:https://github.com/nyck33/canadian-nutrient-file
When I forked that app, it used bin/cnf runserver as the command to run but I refactored cnf/main.py to run as `python cnf/main.py" to work with the tiangolo image mentioned above.
Here is my docker-compose.yml
version: '3'services: flask: environment: CNF_MONGO_HOST: mongo://db:27017 MODULE_NAME: cnf.main build: . #build from dockerfile ports: - "8888:8888" db: image: mongo:3.6.3 ports: - "27017:27017" command: bash -c "sudo service mongodb start"and the Dockerfile:
FROM tiangolo/meinheld-gunicorn-flask:python3.7COPY . /appCOPY /cnf_csv /app/cnf_csvRUN python3 bootstrap.py && \ bin/buildoutSo I'm hoping the mongo container starts up and starts listening on port 27017 for connections since that is what is designated in my settings.py which looks like:
import osimport datetimefrom urllib.parse import urljoinROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))class Config: APP_NAME = "cnf" APP_SYSTEM_ERROR_SUBJECT_LINE = APP_NAME +"system error" #Flask Settings CSRF_ENABLES = True # for host url, set env variables with export FLASK_BIND = os.getenv('CNF_BIND', 'localhost') FLASK_PORT = int(os.getenv('CNF_PORT', 8888)) ROOT_URL = 'http://'+ FLASK_BIND +':'+ str(FLASK_PORT) +'/' TEMPLATE_FOLDER = os.path.join(ROOT_PATH, 'cnf', 'templates') STATIC_FOLDER = os.path.join(ROOT_PATH, 'cnf', 'static') #encryption, cryptographically signs client-side cookies so they cannot be tampered with #if tampered, session becomes invalid, insecure not to be used production SECRET_KEY = 'this key is not secure so be careful until it is' WTF_CSRF_SECRET_KEY = 'this key may or may not be secure so change it' # Flask-User Settings # from https://flask-user.readthedocs.io/en/latest/configuring_settings.html USER_APP_NAME = "cnf" USER_ENABLE_EMAIL = False # register with email USER_ENABLE_CONFIRM_EMAIL = False # force users to confirm email USER_ENABLE_USERNAME = True # enable username authentication USER_REQUIRE_RETYPE_PASSWORD = False # simplify register form USER_EMAIL_SENDER_NAME = 'nobu' #USER_EMAIL_SENDER_EMAIL = 'nobu.kim66@gmail.com' # set up Flask Mail for this USER_ENABLE_CHANGE_USERNAME = True USER_ENABLE_CHANGE_PASSWORD = True USER_ENABLE_FORGOT_PASSWORD = True #USER_ENABLE_REGISTER = True USER_ENABLE_REGISTRATION = True USER_ENABLE_REMEMBER_ME = True USER_AFTER_LOGIN_ENDPOINT = 'main.member_page' USER_AFTER_LOGOUT_ENDPOINT = 'main.home_page' # Flask-Mail settings # For smtp.gmail.com to work, you MUST set "Allow less secure apps" to ON in Google Accounts. # Change it in https://myaccount.google.com/security#connectedapps (near the bottom). MAIL_SERVER = 'smtp.gmail.com' MAIL_PORT = 587 MAIL_USE_SSL = False MAIL_USE_TLS = True #MAIL_USERNAME = gmail_credentials[0] #MAIL_PASSWORD = gmail_credentials[1] MAIL_USERNAME = "-----" MAIL_PASSWORD = "-----" #ADMINS = ['"Admin One" <admin@gmail.com>,] # Sendgrid settings #SENDGRID_API_KEY='place-your-sendgrid-api-key-here'class DevelopmentConfig(Config): MONGODB_HOST = os.getenv('CNF_MONGO_HOST', 'localhost') # for docker: #MONGODB_HOST = 'db' MONGODB_PORT = int(os.getenv('CNF_MONGO_PORT', 27017)) # Todo: should get a dev_cnf db MONGODB_DB = os.getenv('CNF_MONGO_DB', 'cnf') # configuration from env variables, 12 factor cnf documentation DEBUG = (os.getenv('CNF_DEBUG', 'True') == 'True') FLASK_DEBUG = DEBUGclass TestingConfig(Config): MONGODB_HOST = os.getenv('CNF_MONGO_HOST', 'localhost') #for docker: #MONGODB_HOST = 'db' MONGODB_PORT = int(os.getenv('CNF_MONGO_PORT', 27017)) MONGODB_DB = os.getenv('CNF_MONGO_DB', 'cnf_test') # name of db TESTING = (os.getenv('CNF_TESTING', 'True') == 'True')class ProductionConfig(Config): MONGODB_HOST = os.getenv('CNF_MONGO_HOST', 'localhost') MONGODB_PORT = int(os.getenv('CNF_MONGO_PORT', 27017)) # Todo: should get a dev_cnf db MONGODB_DB = os.getenv('CNF_MONGO_DB', 'cnf')config = {'development': DevelopmentConfig,'testing': TestingConfig,'production': ProductionConfig,'default': DevelopmentConfig}Output from docker-compose build
(gamechangers) nobu@gold3forever:~/Desktop/comp4911/Project/canadian-nutrient-file$ docker-compose builddb uses an image, skippingBuilding flaskStep 1/4 : FROM tiangolo/meinheld-gunicorn-flask:python3.7 ---> 8c21eb2c0391Step 2/4 : COPY . /app ---> a34b3105f2caStep 3/4 : COPY /cnf_csv /app/cnf_csv ---> 87d4d818dbd5Step 4/4 : RUN python3 bootstrap.py && bin/buildout ---> Running in eb2d6784975eez_setup.py is deprecated and when using it setuptools will be pinned to 33.1.1 since it's the last version that supports setuptools self upgrade/installation, check https://github.com/pypa/setuptools/issues/581 for more info; use pip to install setuptoolsDownloading https://pypi.io/packages/source/s/setuptools/setuptools-33.1.1.zipExtracting in /tmp/tmpgbjf27wbNow working in /tmp/tmpgbjf27wb/setuptools-33.1.1Building a Setuptools egg in /tmp/tmp609izx6gwarning: no files found matching '*' under directory 'setuptools/_vendor'/tmp/tmp609izx6g/setuptools-33.1.1-py3.7.eggno previously-included directories found matching 'doc'no previously-included directories found matching 'old-tutorial'Creating directory '/app/develop-eggs'.Generated script '/app/bin/buildout'./tmp/tmp609izx6g/setuptools-33.1.1-py3.7.egg/pkg_resources/__init__.py:184: RuntimeWarning: You have iterated over the result of pkg_resources.parse_version. This is a legacy behavior which is inconsistent with the new version class introduced in setuptools 8.0. In most cases, conversion to a tuple is unnecessary. For comparison of versions, sort the Version instances directly. If you have another use case requiring the tuple, please file a bug with the setuptools project describing that need.Develop: '/app/.'Getting distribution for 'zc.recipe.egg==2.0.3'.WARNING: The easy_install command is deprecated and will be removed in a future version.Got zc.recipe.egg 2.0.3.Uninstalling eggs.Installing eggs.Generated script '/app/bin/cnf'.Generated interpreter '/app/bin/python'.Removing intermediate container eb2d6784975e ---> 35ed375967ffSuccessfully built 35ed375967ffSuccessfully tagged cnf_flask:latestAnd docker-compose up -d
(gamechangers) nobu@gold3forever:~/Desktop/comp4911/Project/canadian-nutrient-file$ docker-compose up -dCreating cnf_db_1 ... doneCreating cnf_flask_1 ... done(gamechangers) nobu@gold3forever:~/Desktop/comp4911/Project/canadian-nutrient-file$ docker-compose up -dStarting cnf_db_1 ... doneStarting cnf_flask_1 ... done(gamechangers) nobu@gold3forever:~/Desktop/comp4911/Project/canadian-nutrient-file$ docker-compose up -dStarting cnf_db_1 ... doneStarting cnf_flask_1 ... done(gamechangers) nobu@gold3forever:~/Desktop/comp4911/Project/canadian-nutrient-file$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESSo it looks like the containers start but stop immediately.
Any hints are welcome and appreciated.