-1

This is a hard one but it would be great to solve it and my configuration would awesome!

I have two docker-compose projects: no 1. contains configuration for traefik, no 2. contains my-service that has http://my-domain.tld as parameter.

The goal is to call (let's say ping) http://my-domain.tld from my-service and get through to traefik. Both services now are in the same network but I need somehow to pass configuration that http://my-domain.tld points to traefik.

# Project no 1. configuration for traefik that I want to access from no 2. project
version: '3.7'
services:
  traefik:
    container_name: traefik
    networks:
      - default
networks:
  default:
    name: traefik
# Project no 2.
version: '3.7'
services:
  my-service:
    environment:
      - URL=http://my-domain.tld
    networks:
      - traefik
networks:
  traefik:
    external:
      name: traefik

Tried solutions:

Variant a)

Does not work. When I call http://my-domain.tld it is directed to first node in traefik network (in my case it was my-service itself). Difference from original: aliases in network section.

# Project no 2.
version: '3.7'
services:
  my-service:
    environment:
      - URL=http://my-domain.tld
    networks:
      traefik:
        aliases:
          - ${MERCURE_DOMAIN}
networks:
  traefik:
    external:
      name: traefik

Variant b)

Fails because traefik that service depends on is not in the project/network: Service 'my-service' depends on service 'traefik' which is undefined. Differences from original: depends_on and extra_hosts.

# Project no 2.
version: '3.7'
services:
  my-service:
    environment:
      - URL=http://my-domain.tld
    networks:
      - traefik
    depends_on:
      - traefik
    extra_hosts:
      - http://my-domain.tld:traefik
networks:
  traefik:
    external:
      name: traefik

Variant c)

Works! However keep in mind that I had to hardcode IP address of traefik service. This is not the worst scenario but it makes project less portable and I want to avoid it. Differences from original: network configuration for no 1. project, static ip for traefik service and extra_hosts for no 2. project.

# Project no 1. configuration for traefik that I want to access from next project
version: '3.7'
services:
  traefik:
    container_name: traefik
    networks:
      default:
        ipv4_address: 172.133.239.10
networks:
  default:
    name: traefik
    driver: bridge
      ipam:
        driver: default
        config:
          - subnet: 172.133.239.0/24
# Project no 2.
version: '3.7'
services:
  my-service:
    environment:
      - URL=http://my-domain.tld
    networks:
      - traefik
    extra_hosts:
          - http://my-domain.tld:172.133.239.10
networks:
  traefik:
    external:
      name: traefik

Variant d)

Is waiting for your suggestion how to make this perfect!

EDIT: rewrote docker-compose configuration to separate cases to show what I have tried and accomplished because there were some confusion about it.

Aurelijus Rozenas
  • 2,176
  • 2
  • 28
  • 40

1 Answers1

0

If you know the hostname (or container name) of my-service will be static you can just call it via it's hostname (or container name if no hostname was specified; it'll default to using it's container name as it's hostname).

i.e. if my-service has the hostname container1 you can ping it like this from inside your other container, (as long as they're in the same Docker network): ping container1

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
  • But the main goal of mine is to call `http://my-domain.tld.` not `my-service`. `extra_hosts` let's me define this domain-to-host relation but only in the same network when `depends_on` is provided. I need to make this work on different networks. – Aurelijus Rozenas Mar 23 '20 at 15:02
  • @aurelijus what if you remove the depends_on? Also, i don't think you need that first networks: when using try 1, you'd be defining it twice. – J. Scott Elblein Mar 23 '20 at 17:24
  • @scott check out updated question. Tried to make it as clear as possible with all solutions I have tried. – Aurelijus Rozenas Mar 24 '20 at 18:28