How to assign domain names to containers in Docker?

前端 未结 3 1142
南方客
南方客 2020-12-23 09:46

I am reading a lot these days about how to setup and run a docker stack. But one of the things I am always missing out on is how to setup that particular containers respond

3条回答
  •  半阙折子戏
    2020-12-23 10:42

    Here is a one solution with the nginx and docker-compose:

    • users.mycompany.com is in nginx container on port 8097
    • customer-list.mycompany.com is in nginx container on port 8098

    Nginx configuration:

    server {
        listen 0.0.0.0:8097;
    
        
        root /root/for/users.mycompany.com
        
        ...
    }
    
    server {
        listen 0.0.0.0:8098;
    
        
        root /root/for/customer-list.mycompany.com
        
        ...
    }
    
    server {
        listen 0.0.0.0:80;
        
        server_name users.mycompany.com;
    
        location / {
            proxy_pass http://0.0.0.0:8097;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
    
    server {
        listen 0.0.0.0:80;
        
        server_name customer-list.mycompany.com;
    
        location / {
            proxy_pass http://0.0.0.0:8098;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
    

    Docker compose configuration :

    services:
      nginx:
        container_name: MY_nginx
        build:
          context: .docker/nginx
        ports:
          - '80:80'
        ...
    

提交回复
热议问题