patterndockerMinor
Access docker container by host name instead of localhost:port
Viewed 0 times
containerlocalhostdockerinsteadhostnameportaccess
Problem
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:
every sytem reboot.
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.
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.
Solution
I've been using traefik for this goal.
In here
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.localhostCode Snippets
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: *"Context
StackExchange DevOps Q#14764, answer score: 3
Revisions (0)
No revisions yet.