I’m capturing some of my findings, configuration and work on getting a Flask app (Github: am401/jsondate) up and running on an AWS EC2 using Docker to run both the application and NGINX.
After posting this, I plan to modify this approach slightly as I plan to run multiple applications on the same EC2 instance and currently NGINX is tied to the Docker Makefiles in a way that I don’t feel this is a flexible approach.
After completing the setup I had the following directory structure. In this scenario I have NGINX, certbot and my application running as a service, controlled by a Docker Makefile:
Most of the configuration and setup for the app is from the page I’ve shared. Some of the changes that I made to get things running that were not documented or clear in the original guidance I was following:
My application had run into some issues where NGINX and Docker were not communicating correctly. To resolve this I created a Docker network and specifying this network in the docker compose file for both my web application and NGINX. The docker compose file I’ve created for the project where all the components are running via Docker:
The above Docker Compose file is great as I’m able to start all the services that I need with the following command:
The snag I ran into while trying to build the production Dockerfile was the linting of the Flask app. The instructions use Flake8 for linting the code and what the guide did not mention is how to exclude the virtual environment that is created for the project.
This caused all sorts of linting failures while trying to run the command. Using --exclude=env with the linter, I was able to exclude this and spin up the container without issue. The full Dockerfile is below:
I had some issues getting certbot running initially but I realized that this was an error in the way I was configuring NGINX. I left out ssl when configuring the listener for port 443, which caused a decryption issue for requests and also had an upstream proxy error. After fixing the NGINX config file, this started to work as expected.
Incorrect:
Correct:
Example output of the broken data:
Some additional references that I found useful while creating this setup: