简体   繁体   中英

Traefik with Docker-Compose not working as expected

I am fairly new to using traefik, so I might be totally missing something simple, but I have the following docker-compose.yaml:

version: '3.8'
services:
  reverse-proxy:
    container_name: reverse_proxy
    restart: unless-stopped
    image: traefik:v2.0
    command:
    - --entrypoints.web.address=:80
    - --entrypoints.web-secure.address=:443
    - --api.insecure=true
    - --providers.file.directory=/conf/
    - --providers.file.watch=true
    - --providers.docker=true
    ports:
      - "80:80"
      - "8080:8080"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./scripts/certificates/conf/:/conf/
      - ./scripts/certificates/ssl/:/certs/
    networks:
      - bnkrl.io
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.api.rule=Host(`traefik.bnkrl.io`)"
      - "traefik.docker.network=bnkrl.io"

  bankroll:
    container_name: bankroll
    build:
      context: .
    ports:
      - "3000"
    volumes:
      - .:/usr/src/app
    command: yarn start
    networks:
      - bnkrl.io


    labels:
      - "traefik.http.routers.bankroll.rule=Host(`bankroll.bnkrl.io`)"
      - "traefik.docker.network=bnkrl.io"
      - "traefik.http.services.bankroll.loadbalancer.server.port=3000"
      - "traefik.http.routers.bankroll-https.rule=Host(`bankroll.bnkrl.io`)"
      - "traefik.http.routers.bankroll-https.tls=true"

networks:
  bnkrl.io:
    external: true

But for some reason the following is happening:

Running curl when ssh'd into my bankroll container gives the following:

/usr/src/app# curl bankroll.bnkrl.io
curl: (7) Failed to connect to bankroll.bnkrl.io port 80: Connection refused

Despite having - "traefik.http.services.bankroll.loadbalancer.server.port=3000" label set up.

I am also unable to hit traefik from my application container:

curl traefik.bnkrl.io
curl: (6) Could not resolve host: traefik.bnkrl.io

Despite my expectation to be able to do so since they are both on the same network.

Any help with understanding what I might be doing wrong would be greatly appreciated, My application (bankroll) is a very basic hello-world react app. but I don't think any of the details around that are relevant to the issue I'm facing.

EDIT: I am also not seeing any error logs on traefik side of things.

You are using host names that are not declared and therefore are unreachable.

To reach a container from another container, you need to use the service name, for example, if you connect to bankroll from the reverse-proxy it will hit the other service.

While if you want to access them from the host machine, you will have to publish the ports (which you did, it's all the stuff in ports in your Docker-compose file) and access from localhost or from your machine local IP address instead of traefik.bnkrl.io .

If you want to access from traefik.bnkrl.io , you will have to declare this host name, and point it to the place where the Docker containers are running from.

So either a DNS record in the domain bnkrl.io pointing to your local machine, or a HOSTS file entry in your computer pointing to 127.0.0.1 .

Another note: For SSL you are going to need a valid certificate to use for the host name. While in local development, you can use the self-signed certificate provided by Traefik, but you may have to install it in the computer connecting to the service, or allow untrusted certificates from your browser, or wherever you are making the requests from (some browsers no longer support using self-signed certificates). For SSL on the Internet you will need to look at things like Let's Encrypt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM