简体   繁体   中英

Multiple docker nginx containers or single nginx docker container

Im searching for best practice for multiple nginx containers with tld support. Please consider the following docker-compose file:

frontend:
    build:
      context: nginx/
    hostname: frontend-docker
    ports:
      - "32777:80"

backend:
    build:
      context: nginx/
    hostname: backend-docker
    ports:
      - "33777:80"

proxy:
  image: nginx
  hostname: proxy-docker
  links:
    - frontend
    - backend
  ports:
    - "80:80"

Description As you see I can access frontend and backend at localhost:32777 and localhost:33777 but when I got to prod I want to access frontend at site.com and backend at backend.site.com. In that case comes proxy container that holds server_name backend.site.com; and server_name site.com and creates revers proxy to http://frontend and http://backend

My question is should I get rid of proxy container and put server_name part directly in frontend and backend container and even build one container called web that holds backend and frontend there.

In general splitting the containers in that way is more suitable in terms of configuration, env variables, build different images and so on.

Adressing frontend and backend containers directly from the outside most likely won't work because you can't bind port 80 to the host more than once.

If frontend and backend are different applications you might want to build images of them with different Dockerfile s.

While we're at it, you might want to look into asimpleloadbalancing solution like https://traefik.io/ as frontend for your containers. But maybe that's overkill for your current use case and you want to stick with your above configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM