1

Forgive me if I am asking a stupid question, but I am building a server where I will host multiple Flask websites Docker Container using Nginx Docker. My question now is: is it better to have one main nginx docker container and then host all my Websites Docker containers on it or have an Nginx docker container for each application with docker compose?

I want to know in terms of resource handling and efficiency which one is better to go for ?

@Jacob Following your answer, i am trying to setup something like this

docker-compose.yml:

version: "3.8" services: portfolix: container_name: portfolix image: mervin16/portfolix:dev env_file: - config_local.env expose: - 8086 restart: always networks: - sky_net mes: container_name: mes image: mervin16/mes:dev networks: - sky_net expose: - 8085 restart: always nginx: build: ./nginx container_name: nginx ports: - "8085:85" - "8086:86" restart: always networks: - sky_net networks: sky_net: name: sky_network driver: bridge 

Now each website is available on localhost:8085 and localhost:8086

I am then using a reverse proxy on Nginx (not docker but the one installed on my server) to redirect traffic to my domain name:

server { server_name mes.th3pl4gu3.com; location / { access_log /var/log/nginx/mes/access_mes.log; error_log /var/log/nginx/mes/error_mes.log; proxy_pass http://localhost:8081; } listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/mes.th3pl4gu3.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/mes.th3pl4gu3.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = mes.th3pl4gu3.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name mes.th3pl4gu3.com; return 404; # managed by Certbot } 
2

1 Answer 1

2

Not a stupid question, but let's clarify, no matter how you configure nginx and docker, one host IP can only bind one service to one port, so if you want to handle multiple websites on one IP address on port 80/443 (http/https) you would only be able to run ONE nginx container to handle routing between different hostnames (virtual hosts) and redirect traffic to their appropriate backend.

You will also need to either expose these backends on different ports or share the same docker network.

Something you may want to consider is an alternative to the stock nginx container with something more geared to more dynamic backends like Traefik or nginx-proxy

1
  • I added some code to show my hosting method. Please tell me if that is a good way to go ? Commented Dec 18, 2020 at 18:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.