Question

Running 2 Flask Applications using Docker

Posted February 25, 2020 285 views
NginxPythonDockerUbuntu 18.04

Hi, I am trying to run two flask applications on a Ubuntu Server using Docker and Nginx but I am unable to. I’d like to stick with nginx at the moment since I know there are other options due to time constraints. This is the link to my project https://github.com/raider34/flask. I did try to put together a docker-compose.yml file but I think my spacing is off or something. Any help on getting this running would be great Thanks!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi there @raj350,

I recently created a quick demo on how to run multiple Docker containers on a single Droplet using Nginx reverse proxy, you can take a look at the answer here:

https://www.digitalocean.com/community/questions/how-to-host-multiple-docker-containers-on-a-single-droplet-with-nginx-reverse-proxy

Let me know how it goes!
Regards,
Bobby

  • Hi,

    Thanks for the guide but I wasn’t able to configure my flask applications exactly. Basically I first changed directory to each of the flask apps and ran:

       docker build flask1:latest . 
       docker run -d --name flask1 -p 8080:80 flask1
       docker build flask2:latest . 
       docker run -d --name flask2 -p 8081:80 flask2
    

    I tried to access my flask apps via docker_ip:8080/test1 and docker_ip:8081/test2' but it wasn't working. So I thought I needed nginx then. Then I installednginxand moved mynginx.conffile into/etc/nginx/sites-enabled` and removed the default conf file from there. After restarting nginx, I keep getting an error. This is my nginx.conf file below:

    upstream docker-flask1  {
      server flask1:80;
    }
    
    upstream docker-flask2 {
      server flask2:80;
    }
    
    server {
      listen 8080;
    
      location /test1 {
        proxy_pass http://docker-flask1;
        proxy_redirect  off;
      }
    }
    
    server {
      listen 8081; 
    
      location /test2 {
        proxy_pass http://docker-flask2;
        proxy_redirect off;
      }
    
    }
    

    I think its pretty close but just not sure what it could? I appreciate any help on this. Thanks!

    • Hi there @raj350,

      If you run those 2 commands:

       docker run -d --name flask1 -p 8080:80 flask1
       docker build flask2:latest . 
       docker run -d --name flask2 -p 8081:80 flask2
      

      Then this means that your Flask containers are running on port 8080 and 8081. If you run docker ps do you see the containers running?

      If the containers are not running you could check your logs with the following command:

      docker logs -f docker_container_id
      

      Also if you run netstat -plant do you see the docker containers listening on the 8080 and 8081 ports?

      If your containers are listening on those 2 ports but if you are still unable to access them via your browser, make sure that the two ports are open via your firewall.

      Regarding the Nginx setup, if you have your containers listening on port 8080 and 8081, then you should set your Nginx service on port 80 and the proxy pass to 8080 and 8081, this would be useful if you have domain names and you could proxy the traffic depending on the domain name. So it would look something like this:

      upstream docker-flask1  {
        server flask1:8080;
      }
      
      upstream docker-flask2 {
        server flask2:8081;
      }
      
      server {
        listen 80;
      
        sever_name app1.yourdomain.com;
      
        location /test1 {
          proxy_pass http://docker-flask1;
          proxy_redirect  off;
        }
      }
      
      server {
        listen 80; 
      
        server_name app2.yourdomain.com;
      
        location /test2 {
          proxy_pass http://docker-flask2;
          proxy_redirect off;
        }
      
      }
      

      Let me know if you have any questions!
      Regards,
      Bobb

      • I ran netstat -plant and it’s not showing any of my docker containers. I also can access /test1 but can’t access /test2 as it gives me a 404 error not found. Since I am using uswgi for each flask container, my nginx.conf looks similar to the one you provided as such:

        upstream docker-flask1  {
          server flask1:8080;
        }
        
        upstream docker-flask2 {
          server flask2:8081;
        }
        
        
        
        server {
          listen 80;
          server_name app1.example.com;
        
          location /test1 {
            include uwsgi_params;
            uwsgi_pass docker-flask1;
          }
        
        }
        
        server {
        
        listen 80;
        server_name app2.example.com;
        
        location /test2 {
             include uwsgi_params;
             uwsgi_pass docker-flask2;  
        
            }
        }
        

        My app.ini files look like the ones below:

        [uwsgi]
        wsgi-file = run.py
        callable = app 
        socket = :8080
        processes = 4
        threads = 2
        master = true
        chmod-socket = 660
        vacuum = true
        die-on-term = true
        
        [uwsgi]
        wsgi-file = run.py
        callable = app 
        socket = :8081
        processes = 4
        threads = 2
        master = true
        chmod-socket = 660
        vacuum = true
        die-on-term = true
        

        Since i’m not seeing the containers listening to any ports, then there must be something wrong with the way uwsgi is setup then?
        You can use the link below to access the modified version of the project now. Appreciate your help. Thanks!

        Link to modified version: https://github.com/raider34/flask2

        • Hi there @raj350,

          Your docker-compose file looks correct in terms of ports and naming. What happens when you run docker-compose up?

          Are you seeing any errors in your running Docker containers?

          The Docker run commands that you had in your first comment, should be adjusted to:

           docker run -d --name flask1 -p 8080:8080flask1
           docker run -d --name flask2 -p 8081:8081flask2
          

          As those are the ports that you’ve specified in your uwsgi settings.

          Feel free to share the output of the Docker logs here so I could try to advise you further.

          Regards,
          Bobby

          • Thanks! I was finally able to make it work by moving everything under 1 server block as such

            upstream docker-flask1  {
              server flask1:8080;
            }
            
            upstream docker-flask2 {
              server flask2:8081;
            }
            
            server {   
                listen 80;   
                server_name app.example.com;    
            
                location /test1 {     
                    include uwsgi_params;     
                    uwsgi_pass docker-flask1;   
                } 
            
                location /test2 {     
                    include uwsgi_params;     
                    uwsgi_pass docker-flask2;   
                } 
            }
            

            I’m not sure why this works but its probably because both server blocks were to listening to the same port.

          • Hi there @raj350,

            I am happy to hear that you’ve got this working! And thank you for sharing your solution with the community.

            On the other hand, this is quite interesting, as having multiple server blocks on the same port with different server_name should not be causing any issues.

            Regards,
            Bobby

Submit an Answer