Prerequisites:

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

Objective

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).

app.py

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 config.py:

config.py

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 app.py:

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):

wsgi.py

uwsgi.ini

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.)

Dockerfile

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.

docker-compose.yml

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 that 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

webapp-zombie-dev.yml

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.

webapp-zombie-prod.yml

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 (https://certbot.eff.org/) 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.

docker-compose.dev.yml

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.

docker-compose.prod.yml

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.

Makefile

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

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

SIGN UP FOR THE NEWSLETTER

 

Marek Czaplicki
Marek Czaplicki is a Backend Developer eager to dive into Frontend and DevOps culture.
How Technology Can Improve the Medicine: Machine Learning Methods Used to Detect Cervical Cancer

How Technology Can Improve the Medicine: Machine Learning Methods Used to Detect Cervical Cancer

Only in the United States, about 11,000 women are diagnosed each year with cervical cancer. According to the American Cancer Society’s predictions, there will be…

6 MIN
The Pros and Cons of Using JavaScript for Machine Learning

The Pros and Cons of Using JavaScript for Machine Learning

There’s a misconception in the world of machine learning (ML)   Developers have been led to believe that, to build and train an ML model,…

5 MIN
How Artificial Intelligence, Big Data, And Technology Can Help The World Fight COVID-19

How Artificial Intelligence, Big Data, And Technology Can Help The World Fight COVID-19

Since the start of 2020, scientists from across the world have been deliberating how to beat the Coronavirus. The heroes on the front line are…

5 MIN
  • Share via
    Copy link
    Powered by Social Snap