<p>I've been using traefik for this goal.</p>
<pre><code>version: '3.7'

services:

  traefik:
      image: traefik:latest
      container_name: traefik
      command: --api --docker --docker.domain=${DOMAIN:localhost} --logLevel=DEBUG
      ports:
          - &quot;80:80&quot;
          - &quot;8080:8080&quot;
      volumes:
          - /var/run/docker.sock:/var/run/docker.sock
      restart: unless-stopped
      labels:
          - &quot;traefik.enable=true&quot;
          - &quot;traefik.backend=traefik&quot;
          - &quot;traefik.frontend.rule=Host:traefik.${DOMAIN:localhost}&quot;
          - &quot;traefik.port=8080&quot;

  app-account:
    container_name: app-account
    depends_on:
      - traefik
    labels:
      - &quot;traefik.backend=account&quot;
      - &quot;traefik.frontend.rule=Host:account.${DOMAIN:localhost}&quot;
      - &quot;traefik.frontend.headers.customRequestHeaders=Access-Control-Allow-Origin: *&quot;

</code></pre>
<p>In here <code>${DOMAIN}</code> is used only for overriding domain from shell if you'll use this compose file in production, let's say in Docker Swarm. If you'll use it locally only, then just replace ${DOMAIN:localhost} with <code>localhost</code>. Launching docker with this docker-compose file you can access this particular <code>app-account</code> app via <code>account.localhost</code></p>

CC BY-SA 4.0
2022-07-27T14:26:07.167Z
Thanks for mentioning traefik 
Jono
CC BY-SA 4.0
2021-10-03T01:05:20.223Z
2021-10-03T01:05:20.223Z
holms
<p>Windows 10, docker for windows, simple webapp using nginx and ssl.</p>
<p>Simple thing when using VMs. I have three virtual machines. Every machine has static ip assigned by router. In hosts files I can map ips to host names so I'm able to comunicate between VMs using names: api1.mydev, api2.mydev, application.mydev. Of course all systems have access to other part of network parent. This simple thing took me few days in docker without any progress.</p>
<p>It was super simple to build docker images/containers and they work on custom ports, so localhost:443 works like a harm. I can't find way to:</p>
<ul>
<li>use host name to connect to naginx@docker_container as container's ip changes with
every sytem reboot.</li>
<li>assign host names to many containers as its impossible to define port in hosts file</li>
</ul>
<p>I've tried <a href="https://hub.docker.com/r/jwilder/nginx-proxy" rel="nofollow noreferrer">nginx-proxy</a> but it gives me 502 errors. I've found <a href="https://hub.docker.com/_/traefik" rel="nofollow noreferrer">traefik</a> but I'm not network guru and configuration looks overhelming for newbie.</p>
<p>The option i thought about is to put all services on one container and use nginx to resolve names but this method gives not true advantages over single VM machine and is against docker philosophy.</p>
<p>Is there is simpler way to achieve that? I have feeling that I've missed something in docker configuration.</p>

CC BY-SA 4.0
2021-10-02T11:31:33.387Z
Since you're running all containers in a single host, have you tried running your services using docker-compose? it allows you to connect containers in the same network to using the hostnames. https://docs.docker.com/compose/
Oli
CC BY-SA 4.0
2021-10-03T01:06:19.737Z
@Oli yeah but you still won't be able to address each service by it's hostname in your browser :) You'll have to go through different port bindings per service, or just use traefik with example as I've stated in my answer
holms
CC BY-SA 4.0
2022-03-18T14:24:10.557Z
Did you manage to figure it out? I used to (with VMs) putting the VM's bridged mode non-localhost IP along with a dev.example.com or similar mapping into my hosts file. Similarly, onto my mobile device (with Adguard DNS user filter) while it's connected to home wifi, mapping it to an IP that is fixed to a running VM (even though it runs on a host, it has nothing to do with the hosts's IP). I don't like polluting "localhost" or referring to the random IP of the host from another device on the network. My VM has like 192.168.1.75 how do I get this with Docker?
Firsh - justifiedgrid.com
<docker>
Access docker container by host name instead of localhost:port
Krzysiek Grzembski
# Access docker container by host name instead of localhost:port

(Asked by: username_0 on 2021-09-30)

Windows 10, docker for windows, simple webapp using nginx and ssl.

Simple thing when using VMs. I have three virtual machines. Every machine has static ip assigned by router. In hosts files I can map ips to host names so I'm able to comunicate between VMs using names: api1.mydev, api2.mydev, application.mydev. Of course all systems have access to other part of network parent. This simple thing took me few days in docker without any progress.

It was super simple to build docker images/containers and they work on custom ports, so localhost:443 works like a harm. I can't find way to:

* use host name to connect to naginx@docker\_container as container's ip changes with
  every sytem reboot.
* assign host names to many containers as its impossible to define port in hosts file

I've tried [nginx-proxy](https://hub.docker.com/r/jwilder/nginx-proxy) but it gives me 502 errors. I've found [traefik](https://hub.docker.com/_/traefik) but I'm not network guru and configuration looks overhelming for newbie.

The option i thought about is to put all services on one container and use nginx to resolve names but this method gives not true advantages over single VM machine and is against docker philosophy.

Is there is simpler way to achieve that? I have feeling that I've missed something in docker configuration.

**Tags:** docker

> **Comments:**
> **username_1**: Since you're running all containers in a single host, have you tried running your services using docker-compose? it allows you to connect containers in the same network to using the hostnames. https://docs.docker.com/compose/  
> **username_2**: @username_1 yeah but you still won't be able to address each service by it's hostname in your browser :) You'll have to go through different port bindings per service, or just use traefik with example as I've stated in my answer  
> **username_3**: Did you manage to figure it out? I used to (with VMs) putting the VM's bridged mode non-localhost IP along with a dev.example.com or similar mapping into my hosts file. Similarly, onto my mobile device (with Adguard DNS user filter) while it's connected to home wifi, mapping it to an IP that is fixed to a running VM (even though it runs on a host, it has nothing to do with the hosts's IP). I don't like polluting "localhost" or referring to the random IP of the host from another device on the network. My VM has like 192.168.1.75 how do I get this with Docker?  

---

## 1 answers

### Answer by username_2 on 2021-10-03. Score: 3

I've been using traefik for this goal.
version: '3.7'

services:

  traefik:
      image: traefik:latest
      container_name: traefik
      command: --api --docker --docker.domain=${DOMAIN:localhost} --logLevel=DEBUG
      ports:
          - "80:80"
          - "8080:8080"
      volumes:
          - /var/run/docker.sock:/var/run/docker.sock
      restart: unless-stopped
      labels:
          - "traefik.enable=true"
          - "traefik.backend=traefik"
          - "traefik.frontend.rule=Host:traefik.${DOMAIN:localhost}"
          - "traefik.port=8080"

  app-account:
    container_name: app-account
    depends_on:
      - traefik
    labels:
      - "traefik.backend=account"
      - "traefik.frontend.rule=Host:account.${DOMAIN:localhost}"
      - "traefik.frontend.headers.customRequestHeaders=Access-Control-Allow-Origin: *"


In here ${DOMAIN} is used only for overriding domain from shell if you'll use this compose file in production, let's say in Docker Swarm. If you'll use it locally only, then just replace ${DOMAIN:localhost} with localhost. Launching docker with this docker-compose file you can access this particular app-account app via account.localhost

> **Comments:**
> **username_4**: Thanks for mentioning traefik   

---

