0

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.

2
  • 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 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 at 1:06
0

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

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.