How to assign domain names to containers in Docker?

前端 未结 3 1138
南方客
南方客 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:39

    So, you need to use the concept of port publishing, so that a port from your container is accessible via a port from your host. Using this, you can can setup a simple proxy_pass from an Nginx that will redirect users.mycompany.com to myhost:1337 (assuming that you published your port to 1337)

    So, if you want to do this, you'll need to setup your container to expose a certain port using:

    docker run -d -p 5000:5000 training/webapp # publish image port 5000 to host port 5000
    

    You can then from your host curl your localhost:5000 to access the container.

    curl -X GET localhost:5000
    

    If you want to setup a domain name in front, you'll need to have a webserver instance that allows you to proxy_pass your hostname to your container.

    i.e. in Nginx:

    server {
      listen 80;
      server_name users.mycompany.com;
      location / {
        proxy_pass http://localhost:5000;
      }
    }
    

    I would advise you to follow this tutorial, and maybe check the docker run reference.

    0 讨论(0)
  • 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'
        ...
    
    0 讨论(0)
  • 2020-12-23 10:44

    For all I know, Docker doesn't provide this feature out of the box. But surely there are several workarounds here. In fact you need to deploy a DNS on your host that will distinguish the containers and resolve their domain names in dynamical IPs. So you could give a try to:

    1. Deploy some of Docker-aware DNS solutions (I suggest you to use SkyDNSv1 / SkyDock);

    2. Configure your host to work with this DNS (by default SkyDNS makes the containers know each other by name, but the host is not aware of it);

    3. Run your containers with explicit --hostname (you will probably use scheme container_name.image_name.dev.skydns.local)

    You can skip step #2 and run your browser inside container too. It will discover the web application container by hostname.

    0 讨论(0)
提交回复
热议问题