2

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 but it gives me 502 errors. I've found 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.

3
  • 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. docs.docker.com/compose
    – Oli
    Oct 2, 2021 at 11:31
  • @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
    Oct 3, 2021 at 1:06
  • 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? Mar 18 at 14:24

1 Answer 1

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

1
  • Thanks for mentioning traefik 👍
    – Jono
    Jul 27 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.