How to spin up a simple Flask app using NGINX — in 15 minutes or less

How to spin up a simple Flask app using NGINX — in 15 minutes or less


  1. Linux machine
  2. Docker Engine & Docker Compose
  3. Domain name pointed to your server
  4. Optional: Certificate, Private Key and Intermediate Certificate


Have you ever tried using NGINX? If so, then you’ll probably be familiar with the headache of issuing a certificate using Certbot as well. Thankfully, Zombie NGINX can cure such pains for you!

Zombie NGINX turns NGINX into easier-to-read configuration files, then issues a new certificate, all by itself.

The following article shows you how to create a simple Flask application, alongside static files, all using the Zombie NGINX docker container.

Step 1: Application Preparation

Let’s kick things off with a simple Flask application (which we will modify later to handle production status).

Now, wouldn’t it be cool if we could distinguish each environment in our app? We think so, which is why we create a specific configuration for production — and another for development — in file

Before we can use this configuration, we first need to load one of these into our flask app, which we can do by adding this code to our

As you can see, we’re using an environmental variable called FLASK_ENV, which we haven’t set (at least, not yet, but we will set up environmental variables later in this article).
Therefore, the default value “development” is selected.
We will run our Flask app using uwsgi. To get it working, we will use two files (you can check what each file does in the uwsgi.ini documentation):

  • — to run our app
  • uwsgi.ini — to set up the configuration


Important: Keep all installed Python packages in the file requirements.txt.

Step 2: Dockerizing The Application

Now that we have all the above files in the directory, it’s time to create a Docker image.

The Docker image will hold the source code of our application and install the requirements — for this; we will use a Debian-based image with Python pre-installed.

(Note: Feel free to use any image that suits your needs though keep in mind: the smaller, the better.)


Now that we have a Docker image alongside the source code, we can create the docker-compose files that will hold the configuration for the containers.
Let’s kick this process off by creating a base docker-compose file, which we will use to store the parts for both development and production.


By this point, we have configured two containers:

  1. A webapp that will build the Docker file from within the directory;
  2. And the second uses a typeai/zombie-nginx image (find documentation here).

However, they both need to be in a single network to allow NGINX to reach the Flask application.

Therefore, before we start creating the docker-compose files for development and production, we first need to create two configuration files for Zombie NGINX: one for development, the other for production.

Let’s start with the development configuration file.

How to spin up a simple Flask app using NGINX — in 15 minutes or less

Development Configuration File


We want our Zombie NGINX to have an upstream link to our uwsgi running application. And as we have previously set up a socket to open on port 3031, let’s assume our application is more than a basic, ‘Hello World!’ application.

As a matter of fact, we know we want to host static files as well as media: to do so, all we need to add is an entry with a location where we can store these files (we will also mount a docker volume in this location, later on).

Note: We do not need HTTPS for development purposes, so let’s leave it off in this case.

Production Configuration File

The production configuration file will hold all the information on the domain where it will be hosted.


In this file, we need to provide the correct server name, check the host header, point to the proper uwsgi socket, host static files, and enable the Certbot ( certificate issue for our domain.

Certbot will automatically issue a certificate for our domain (server_name).

Note: If you want to use your own certificate (i.e. not Certbot-issued), you will have to change tls from ‘auto’ to ‘mapping’ as in the example below.

Keep in mind that certificate.txt should also store the value of the intermediate certificate.

Now, let’s create the docker-component-specific files for both development and production.

  • For the webapp container: we set Flask mode to “development” using the environment variable;
  • For the NGINX container: we mount our Zombie configuration file to /etc/appconf/nginx.yml from which we can create a proper containerized NGINX configuration.

We will also mount a directory with static files in the same location in which we specified the Zombie configuration.
Plus, we will expose port 80 to our local machine.

In the above example, we set our Flask app to “production” mode. We can set a custom secret key (simply set the APP_SECRET_KEY variable). Further, we can mount the configuration (production) in our container and static files directory.

Finally, as we want our application to be secure, we now expose port 443.

Step 3: Development

To start the application in development mode, run it in the project directory:

Step 4: Deployment

To deploy our application, we clone our repository to a production environment; then, we go to the root of the application and run the command:

Doing the above will start both containers in production mode — as daemons.

Step 5: Bonus Feature

As a final step, you can create a simple Makefile to run the above commands.


From this point, onwards — start the app in development mode by running:

(Note: You may need to install Makefile to your OS.)

how to implement ai
Marek Czaplicki

Marek Czaplicki

Marek Czaplicki is a Backend Developer eager to dive into Frontend and DevOps culture.

5 Key Reasons Why You Should Use AI-based Solutions In Your HR department

5 Key Reasons Why You Should Use AI-based Solutions In Your HR department

Will AI replace HR managers? Unlikely. Still, modern-day companies must realize they need new technologies if they’re to stay at the forefront of their field.…

How to Increase Accounting Efficiency Using AI Invoice Automation

How to Increase Accounting Efficiency Using AI Invoice Automation

Big Data and a boatload of data are not the same. But they do make a perfect match. Check out the case where we helped…

Can Self-Driving Cars Keep Us Safe?

Can Self-Driving Cars Keep Us Safe?

The deadly accident during the tests of a self-driving car has revived the discussion around this technology. Can equipment created by fallible men be infallible?…

Share via
Copy link
Powered by Social Snap