<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Morné Zeelie</title>
    <description>The latest articles on DEV Community by Morné Zeelie (@holla22).</description>
    <link>https://dev.to/holla22</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F245458%2F721cfe5d-e787-4751-b658-bd72db33211d.jpg</url>
      <title>DEV Community: Morné Zeelie</title>
      <link>https://dev.to/holla22</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/holla22"/>
    <language>en</language>
    <item>
      <title>Create a Docker multi-container Flask app</title>
      <dc:creator>Morné Zeelie</dc:creator>
      <pubDate>Wed, 17 Jun 2020 17:42:24 +0000</pubDate>
      <link>https://dev.to/holla22/create-a-docker-multi-container-flask-app-13fj</link>
      <guid>https://dev.to/holla22/create-a-docker-multi-container-flask-app-13fj</guid>
      <description>&lt;h3&gt;Table of Contents&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What are we going to build?&lt;/li&gt;
&lt;li&gt;Docker-Compose File Creation&lt;/li&gt;
&lt;li&gt;The Flask App&lt;/li&gt;
&lt;li&gt;Starting up our Docker Setup&lt;/li&gt;
&lt;li&gt;Let’s set up our Nginx Config&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Docker has been around for 7 years and there are still developers out there that haven't used it. &lt;/p&gt;

&lt;p&gt;The traditional way developers work locally usually is by installing something like &lt;strong&gt;XAMPP &lt;/strong&gt;or&lt;strong&gt; WAMP &lt;/strong&gt;if you are working with PHP&lt;strong&gt; &lt;/strong&gt;maybe even running a&lt;strong&gt; Virtualbox &lt;/strong&gt;somehow.&lt;strong&gt; &lt;/strong&gt; This is okay, but have you ever run into issues where code doesn't work the same as on a server? What about developers running the same code on their machines but experiencing different issues with the same code.&lt;/p&gt;

&lt;p&gt;Even when deploying code to a live server you would end up with a few different issues, which you would then need to debug on the live server to get it working. Then the reverse sometimes happens on local vs live again. &lt;/p&gt;

&lt;p&gt;So what is the right way? &lt;/p&gt;

&lt;p&gt;Docker is the answer. You can build a custom Docker image that everyone on your team can use in combination with Docker-compose. This way you can all work from the same types of distros and setups.&lt;/p&gt;

&lt;p&gt;For example, you can build a simple Dockerfile where you can specify which base image you want to use. I like using Debian and currently use Debian buster in most of my projects. I also like using Ubuntu, it depends on the project and how I feel the day.&lt;/p&gt;

&lt;p&gt;Then you will specify what else you want part of that image seeing that Debian pretty much comes as a minimal image. You would probably want to install something like Vim, Curl and Wget on there.&lt;/p&gt;

&lt;p&gt;You could decide to install Apache and PHP on there, but then again you have to decide should I rather use the PHP image which uses Debian buster for example &lt;a href="https://hub.docker.com/_/php" rel="noreferrer noopener"&gt;php:7.3-apache-buster&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What about Python? Instead of using the base &lt;a href="https://hub.docker.com/_/debian" rel="noreferrer noopener"&gt;Debian&lt;/a&gt; image, I recommend using the python image: &lt;a href="https://hub.docker.com/_/python" rel="noreferrer noopener"&gt;python:3.7-buster&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, this article did not start by telling you what Docker is, or a 101 crash course on Docker. If you don't know what Docker is, I suppose now is a good time to find out, go &lt;a href="https://www.docker.com/" rel="noreferrer noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So let this article rather serve you as a first-time run or maybe even your second run, because you have been Googling and still don't get it. I will try and explain in as much or little detail as possible. The only way to learn is to get your hands dirty but in a fun way.&lt;/p&gt;

&lt;h2 id="what-are-we-going-to-build"&gt;&lt;strong&gt;What are we going to build?&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;We are going to build a Dockerfile, then we are going to create a Docker-compose file and combine different images to do different things. See it as running multiple apps on one server.&lt;/p&gt;

&lt;p&gt; We will put the following together :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A MySQL image as a Database&lt;/li&gt;
&lt;li&gt;A Python image running Flask.&lt;/li&gt;
&lt;li&gt;Nginx which serves as a reverse proxy.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Are you excited yet?&lt;/p&gt;

&lt;h2 id="docker-compose-file-creation"&gt;Docker-Compose File Creation.&lt;/h2&gt;

&lt;p&gt;The first thing we want to start with is creating a docker-compose.yaml file then you add MySQL as our database service. We are not going to use MySQL in this tutorial, but adding it for the purpose of a future tutorial.&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;version: '3.4'

services:
  db:
    image: mysql:8.0.19
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data_python_flask:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: RandomRoot@213!
      MYSQL_DATABASE:  flask_app
      MYSQL_USER: flask_user
      MYSQL_PASSWORD: RandomRoot@213!
    ports:
      - "33061:3306"&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When you look at the above, we create a service called "db" which uses a MySQL image with a specific version &lt;a href="https://docs.docker.com/engine/reference/commandline/tag/" rel="noreferrer noopener"&gt;tag&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We have a "command" which specifies the default authentication plugin to use for MySQL.&lt;/li&gt;
&lt;li&gt;A volume to persist the data so that we don't lose data when a container restarts or shuts down.&lt;/li&gt;
&lt;li&gt;"restart", to always restart when something goes wrong and try and correct itself.&lt;/li&gt;
&lt;li&gt;"environment", for environment variables within the image to be used in your app. Here we specify the MySQL database credentials to hook up your application to the database.&lt;/li&gt;
&lt;li&gt;We set the default MySQL port to a different port as it's better in my opinion not to expose the default port.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, we want to create a Dockerfile for our custom python flask image. This will use a base image, &lt;a rel="noreferrer noopener" href="https://hub.docker.com/_/python/"&gt;python&lt;/a&gt;:3.7-buster image.&lt;/p&gt;

&lt;p&gt;The Dockerfile looks like this:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;FROM python:3.7-buster


EXPOSE 80/tcp
EXPOSE 443/tcp

RUN apt-get update &amp;amp;&amp;amp; apt-get install vim -y

RUN /usr/local/bin/python -m pip install --upgrade pip

WORKDIR /project

COPY flask_app/ /project

RUN pip install -r requirements.txt

EXPOSE 8003/tcp

WORKDIR /project

ENTRYPOINT [ "./start_gunicorn_server.sh" ]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We choose our base image to use. Always use a specific version and not the latest tag to avoid versioning issues you might have in the future.&lt;/li&gt;
&lt;li&gt;Expose port 80 and 443 for web traffic&lt;/li&gt;
&lt;li&gt;Install VIM for editing files&lt;/li&gt;
&lt;li&gt;Upgrade pip to the latest version&lt;/li&gt;
&lt;li&gt;Create a project directory&lt;/li&gt;
&lt;li&gt;Copy our flask app to the image project directory&lt;/li&gt;
&lt;li&gt;Install all the python libraries we require&lt;/li&gt;
&lt;li&gt;Expose port 8003 as we are using this port to run our app with &lt;a href="https://gunicorn.org/" rel="noreferrer noopener"&gt;Gunicorn&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Just make double sure I'm in the project directory&lt;/li&gt;
&lt;li&gt;Trigger the Gunicorn shell script to start the server for the flask app&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, we have a shell script (&lt;strong&gt;start_gunicorn_server.sh&lt;/strong&gt;), which will be used to start the production server on the docker container which runs the following command:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;gunicorn app:app -w 2 --threads 2 -b 0.0.0.0:8003&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This starts the Gunicorn server for your file app.py with 2 workers and 2 threads on port 8003.&lt;/p&gt;

&lt;p&gt;Now back to the &lt;strong&gt;docker-compose.yaml&lt;/strong&gt; file to add another service for the flask app. You can see it in the yaml code below:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;  flask_app:
    depends_on:
      - db
    build: .
    ports:
      - "8003:8003"
      - "4431:443"
    restart: always&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We create a service called "flask_app"&lt;/li&gt;
&lt;li&gt;This service &lt;a href="https://docs.docker.com/compose/compose-file/#depends_on" rel="noreferrer noopener"&gt;depends on&lt;/a&gt; the "db" service&lt;/li&gt;
&lt;li&gt;Build the Dockerfile ( local context )&lt;/li&gt;
&lt;li&gt;Map ports for the app to run, 8003 which we will proxy to later plus port 4431 mapped to 443 for https. ( we might not even need this but I map it anyways )&lt;/li&gt;
&lt;li&gt;We set our &lt;a href="https://docs.docker.com/compose/compose-file/#restart" rel="noreferrer noopener"&gt;restart&lt;/a&gt; policy to always restart&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, we add another service for our Nginx reverse proxy:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;  reverse_proxy:
    depends_on:
      - flask_app
    container_name: reverse-proxy
    hostname: reverse
    image: nginx:1.18.0
    ports:
      - "80:80"
      - "443:443"
    restart: always
    volumes:
      - nginx_data:/etc/nginx
      - nginx_data:/etc/ssl/private&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a service called reverse_proxy&lt;/li&gt;
&lt;li&gt;This depends on the "flask_app" service, we created before&lt;/li&gt;
&lt;li&gt;We give it a container name of "reverse-proxy"&lt;/li&gt;
&lt;li&gt;We use an image that is a Debian buster image for Nginx&lt;/li&gt;
&lt;li&gt;We map ports 80 and 443 for HTTP and HTTPS traffic&lt;/li&gt;
&lt;li&gt;A restart policy of always&lt;/li&gt;
&lt;li&gt;We map to paths to the "nginx_data"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last part of the docker-compose.yaml file, we add the main &lt;a href="https://docs.docker.com/compose/compose-file/#volumes" class="rank-math-link"&gt;volumes&lt;/a&gt; we mapped in the services:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;volumes:
    db_data_python_flask: {}
    nginx_data: {}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The complete final docker-compose.yaml file should look like this:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;version: '3.4'

services:
  db:
    image: mysql:8.0.19
    command: '--default-authentication-plugin=mysql_native_password'
    volumes:
      - db_data_python_flask:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: RandomRoot@213!
      MYSQL_DATABASE:  flask_app
      MYSQL_USER: flask_user
      MYSQL_PASSWORD: RandomRoot@213!
    ports:
      - "33061:3306"

  flask_app:
    depends_on:
      - db
    build: .
    ports:
      - "8003:8003"
      - "4431:443"
    restart: always

  reverse_proxy:
    depends_on:
      - flask_app
    container_name: reverse-proxy
    hostname: reverse
    image: nginx:1.18.0
    ports:
      - "80:80"
      - "443:443"
    restart: always
    volumes:
      - nginx_data:/etc/nginx
      - nginx_data:/etc/ssl/private

volumes:
    db_data_python_flask: {}
    nginx_data: {}&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="the-flask-app"&gt;The Flask App&lt;/h2&gt;

&lt;p&gt;Finally, we set up a simple little flask app for this exercise. I'm not going to go into how to set up &lt;a rel="noreferrer noopener" href="https://lmddgtfy.net/?q=installing%20python%203.7"&gt;Python&lt;/a&gt; or &lt;a rel="noreferrer noopener" href="https://virtualenv.pypa.io/en/stable/installation.html"&gt;Virtualenv&lt;/a&gt;, you can find out on your own, see it as homework.&lt;/p&gt;

&lt;p&gt;Here are the steps to set up a simple flask app:&lt;/p&gt;

&lt;p&gt;In your terminal run "&lt;em&gt;&lt;strong&gt;virtualenv venv&lt;/strong&gt;&lt;/em&gt;", then run "&lt;em&gt;&lt;strong&gt;source venv/bin/activate&lt;/strong&gt;&lt;/em&gt;"&lt;/p&gt;

&lt;p&gt;This will activate the Python virtual environment for you.&lt;/p&gt;

&lt;p&gt;Next install flask with the following command:&lt;/p&gt;

&lt;p&gt;"&lt;strong&gt;pip install Flask&lt;/strong&gt;"&lt;/p&gt;

&lt;p&gt;You can read more on installing flask &lt;a href="https://flask.palletsprojects.com/en/1.1.x/installation/" rel="noreferrer noopener"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next, &lt;/strong&gt; you want to create a folder called  &lt;em&gt;&lt;strong&gt;flask_app&lt;/strong&gt; and inside the folder, a&lt;/em&gt; file called &lt;em&gt;&lt;strong&gt;app.py&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The code looks like this inside &lt;strong&gt;&lt;em&gt;app.py&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;from flask import Flask
app = Flask(__name__)
 
@app.route('/')
def home():
    return "Hello you have successfully created a minimal python Flask app with Docker"&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Explanation&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We first import the Flask class from the flask library.&lt;/li&gt;
&lt;li&gt;Next, we create an instance of this class.&lt;/li&gt;
&lt;li&gt;we set the route decorator to / which will be the main URL you land on.&lt;/li&gt;
&lt;li&gt;we create a method called home() which returns some text to the user's browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, we install &lt;a rel="noreferrer noopener" href="https://docs.gunicorn.org/en/stable/install.html"&gt;Gunicorn&lt;/a&gt;&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;pip install gunicorn &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This we will be used to be able to run Gunicorn in the shell script I mentioned earlier in this article, to start the server.&lt;/p&gt;

&lt;h2 id="starting-up-our-docker-setup"&gt;Starting up our Docker Setup.&lt;/h2&gt;

&lt;p&gt;Let's begin this section by actually running docker-compose and making sure that docker creates our containers based on our docker-compose.yaml file.&lt;/p&gt;

&lt;p&gt;You can trigger your file with the following command:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;docker-compose up -d&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates the containers for each of the services specified in your compose file. Now when you run "docker ps" you should see all the containers created. Each of them running as a separate service.&lt;/p&gt;

&lt;h2 id="lets-set-up-our-nginx-config"&gt;Let's set up our Nginx Config&lt;/h2&gt;

&lt;p&gt;Now we need to set up our Nginx config. I could have created a separate docker file to add the config from the beginning, but for this tutorial, I wanted to do it afterwards.&lt;/p&gt;

&lt;p&gt;Run the following command and look for your Nginx container ID.&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;docker ps&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P5aYZkgP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://teachingyou.net/wp-content/uploads/2020/06/Screenshot-2020-06-08-at-19.39.36-1024x105.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P5aYZkgP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://teachingyou.net/wp-content/uploads/2020/06/Screenshot-2020-06-08-at-19.39.36-1024x105.png" alt="" class="wp-image-66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should grab the Container ID, my current one is &lt;strong&gt;6013be427253&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Next, we want to execute into our container with the following command:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;docker exec -it 6013be427253 bash&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Do you notice we use the container id to execute into the container? Now that we are inside the container it's like any other Linux system that runs a Debian server where we can run commands.&lt;/p&gt;

&lt;p&gt;We now need to create a config file which will redirect all traffic coming in at port 80 and 443 and redirect it to our flask container running on port 8003.&lt;/p&gt;

&lt;p&gt;change the directory to the following path:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;cd /etc/nginx/conf.d/&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then create a directory called site-available:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;mkdir sites-available&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then change into the sites-available directory:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;cd sites-available&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next, we want to install vim to be able to create and edit our config file.&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;apt update &amp;amp;&amp;amp; apt install vim -y&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now let's create the actual config file with the following command.&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;vim nginx-live.conf&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now paste the config file contents below.&lt;/p&gt;

&lt;p&gt;The config file looks like this:&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;server {
    listen 80;

    server_name yourdomain.com;
    location / {
        # for localhost use host.docker.internal
        # for localhost Mac use docker.for.mac.localhost
        proxy_pass http://yourdomain.com:8003;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Press Esc and type :wq to save and quit the file. Now please note that if you are going to run this on your local machine you need to use host.docker.internal as your domain name.&lt;/p&gt;

&lt;p&gt;Next, we want to go and edit the nginx.conf file and the line which includes all conf files.&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;Find this line:

include /etc/nginx/conf.d/*.conf;

Change it to:

include /etc/nginx/conf.d/sites-available/*.conf;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Save the file and then reload Nginx so that the new config file is picked up.&lt;/p&gt;

&lt;pre class="wp-block-code"&gt;&lt;code&gt;nginx -s reload&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now at this time, you should be able to visit your domain name and the flask app home page should show the message from the flask app. If you're on localhost you can go to http://127.0.0.1&lt;/p&gt;

&lt;p&gt;Congrats, you have reached the end of this tutorial. Please leave a comment and give me feedback or ask for help.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/holla22/docker-flask-tutorial" class="rank-math-link"&gt;Download this project from GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article was originally posted on &lt;a href="https://teachingyou.net/docker-multi-container-flask-app/" class="rank-math-link"&gt;TeachingYou.net&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Consider buying me a coffee or becoming a monthly member, this way you will help me to do what I'm passionate about.&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/z33man" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/be06971baed9105260e0ed5c03746108c30b527f/68747470733a2f2f63646e2e6275796d6561636f666665652e636f6d2f627574746f6e732f64656661756c742d6f72616e67652e706e67" alt="Buy Me A Coffee" height="41" width="174"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>python</category>
      <category>flask</category>
    </item>
  </channel>
</rss>
