-->

Traefik docker image not working on Windows but wo

2020-08-26 03:12发布

问题:

I have an application that's composed of several Docker images that is being developed on MacOS and I'm trying to get it started on Windows. Most seem to be working, but there's a Traefik load balancer that doesn't.

On MacOS it just works, but on Windows, whatever URL I access, it gives me a 404, the actual content which is just "404 page not found".

The Traefik configuration looks like this:

logLevel = "INFO"
defaultEntryPoints = ["http", "https"]
[entryPoints]
  [entryPoints.http]
   address = ":80"
  [entryPoints.https]
   address = ":443"
    [entryPoints.https.tls]
    [entryPoints.https.tls.defaultCertificate]
      certFile = "/certs/cert.pem"
      keyFile = "/certs/cert.pem"
# Connection to docker host system (docker.sock)
[docker]
watch = true
domain = "localhost"
[file]
 [backends]
  [backends.backend-mixpanel]
     [backends.backend-mixpanel.servers]
      [backends.backend-mixpanel.servers.mixpanel]
        url = "https://api.mixpanel.com"
        weight = 10
  [backends.backend-yieldify]
     [backends.backend-yieldify.servers]
      [backends.backend-yieldify.servers.yieldify]
        url = "https://td.yieldify.com/"
        weight = 0
 [frontends]
     [frontends.frontend-mixpanel]
       backend = "backend-mixpanel"
      [frontends.frontend-mixpanel.routes.one]
        rule = "HostRegexp:localhost,{catchall:.*};Path:/mixpanel/{.*};PathPrefixStripRegex:/{mixpanel}"
        priority=10
     [frontends.frontend-yieldify]
       backend = "backend-yieldify"
      [frontends.frontend-yieldify.routes.two]
        rule = "HostRegexp:localhost,{catchall:.*};Path:/yieldify/{.*}"
        priority=0

While the Dockerfile contains:

FROM traefik:alpine

RUN apk add --update openssl

RUN mkdir -p /certs

RUN openssl req -x509 -newkey rsa:2048 -keyout key.pem -out ca.pem -days 1080 -nodes -subj '/C=UK/ST=London/L=London/O=ProjectX/OU=Engineering team/CN=local.wif'

RUN cat key.pem ca.pem > /certs/cert.pem

The relevant docker-compose.yml section contains:

  lb:
    image: load-balancer
    build: ${WORKSPACE}/go-home/load_balancer
    ports:
    - 80:80
    - 443:443
    links:
    - wifi-ui-dev
    - wifi-ui-prod
    - portal
    - wifi-api
    env_file:
    - .env
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock:ro,delegated
    - ${PWD}/load_balancer/traefik.toml:/etc/traefik/traefik.toml:ro,delegated

And when I get the 404 I get nothing sent to the console (stdout and stderr) for that Docker image.

Any ideas what's going on or how to get closer to a reason why it's not working?

One of my worries was the socket volume:

- /var/run/docker.sock:/var/run/docker.sock:ro,delegated

Checking the running container, the file looks like a socket:

/ # ls -laF /var/run/docker.sock
srw-rw----    1 root     root             0 Sep  2 11:04 /var/run/docker.sock=

Just in case, I tried replacing that line with:

- //./pipe/docker_engine:/var/run/docker.sock

or with:

- type: npipe
  source: ////./pipe/docker_engine
  target: /var/run/docker.sock
  consistency: delegated

both of which resulted in the socket looking like a directory:

/ # ls -laF /var/run/docker.sock
total 4
drwxr-xr-x    2 root     root            40 Sep  3 14:52 ./
drwxr-xr-x    1 root     root          4096 Sep  3 14:57 ../

回答1:

OK, I will try to coach you in a number of your issues.

  • 404 not found (just as a sentence) - is typically a traefik config issue. Not a regular website 404. You can verify this by looking in the logs of the Traefik container.
  • I had similar issues with 404 when Traefik was not started in the right order. We sometimes deploy Traefik outside our Docker (Swarm) stack seperately. Then we deploy the stack (or other components). You can specify the order via e.g. a docker-compose.yml file. This 'depends_on' is not accurate, because it only tests whether the component is started, not that is completely started as an application.
  • Because your config runs on Mac my assumption is that the traefik 404 is not due to the Docker (Traefik) network configuration.
  • Be sure which version you use of Treafik. Since Traefic 2 there were a few important changes. I use: traefik:v1.7.11-alpine.
  • Be sure that "traefik.enable" should explicitly be set to true. We saw that it differs per environment if this setting is needed.
  • If you deploy a running website, you can use the Traefik console (via port 8080) to see what you have configured. You see the frontend rules and the backends.
  • The environment file is is '.env'. Putting dot-files on Windows requires special actions, e.g. via cygwin or Git bash. Assumed is that the file is existing.

About the mounting and the docker.sock?

  • Make a share. On Windows, open your Docker Desktop, go to tab "Shared drives". Add a drive, e.g. D or K. As an example I mapped K to e.g. K:\data.
  • Below you find a number of examples from a docker-compose.yml file. I use in these examples the share "K => k:/data".

The first example shows how to start a complete Jenkins environment on Docker for Windows. From within the Jenkins environment reference is made to the Docker Engine, so a reference is made to docker.sock on Windows. This is what I daily use in my Docker for Windows environment. It works fine!

version: '3'
services:
  jenkins:
    image: docker-jenkins-maven-npm-oc:latest
    ports:
      - "8888:8080"
    volumes:
      - //k/data/var/jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
  geosolschmea:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=somedatabase
      - MYSQL_USER=johan
      - MYSQL_PASSWORD=bladibladibla
    volumes:
      - //k/data/var/mysql-data:/var/lib/mysql
    ports:
      - "3306:3306"

Another Spring boot application on Windows in a docker-compose.yml file:

  geosolutionapp:
    image: myuser/geosolutions:latest
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
      - geosolschmea
    environment:
      - SCDATALIMIT=100000
    ports:
      - 8080:8080
    volumes:
      - //k/data/spring-boot-app:/data/spring-boot-app
    networks:
      - geosolutionsnet

For Traefik a docker-compose.yml could look like (on Linux, Amazon EC2). This will also allow you to show the Traefik dashboard. Use this to see which frontends and backends you have. Changing the volumes slightly and you have it run on Docker for Windows.

You can easily run this on Docker for Windows if you change the paths to the Treafik files:

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - //k/data/traefik/traefik.toml:/traefik.toml
      - //k/data/traefik/acme.json:/acme.json

The complete working (live) example running daily on Linux is:

version: '3'
services:
  traefik:
    image: traefik:v1.7.11-alpine
    container_name: traefik
    restart: always
    networks:
      - geosolutionsnet
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /home/ec2-user/traefik/traefik.toml:/traefik.toml
      - /home/ec2-user/traefik/acme.json:/acme.json
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    labels:
      - "traefik.docker.network=geosolutionsnet"
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:yourwebsite.nl;PathPrefix:/traefik"
      - "traefik.port=8080"
      - "traefik.protocol=http"
      - "traefik.backend=traefik"

When using Traefik on Docker for Windows, I use this to connect to the docker Engine in the traefik.toml file:

[docker]
  endpoint = "unix:///var/run/docker.sock"
  domain = "yourwebsite.nl"
  watch = true
  exposedByDefault = false

If you have trouble getting the routing work for Traefik on Docker for Windows, you can use this as a skeleton and start with a 'docker hello world' example first and get it accessable via Treafik. After getting a basic route working Traefik on Docker for Windows, you can step by step add your real application. This may be felt as a long way, but in our experience this works fast.

Of course, not all ports should be visible to the outside world. You could use a newer version of the docker-compose.yml file.

Another component using Treafik could be like:

  geosolutionapp:
    image: myuser/myproduct:latest
    environment:
      - slackBotToken=xyz-etc
    ports:
      - 8080
    networks:
      - geosolutionsnet
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=geosolutionsnet"
      - "traefik.frontend.rule=Host:myapplication.nl"
      - "traefik.port=8080"
      - "traefik.protocol=http"

And another example:

slackbotsimple:
    image: myuser/slackbotsimple:latest
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - traefik
    environment:
      - slackBotToken=xoxb-etc-etc-etc

Still stuck on the docker.sock?

  • If the above does not help, you can also use an approach like: docker run -v //var/run/docker.sock:/var/run/docker.sock ...
  • Powershell: run $Env:COMPOSE_CONVERT_WINDOWS_PATHS=1
  • Git bash: $ export COMPOSE_CONVERT_WINDOWS_PATHS=1
    $ docker-compose down && docker-compose up -d
  • Be sure the /var/run/docker.sock has the right permissions. In some cases I have to manually set the permissions via this: $ docker exec -u 0 -it bash ... and then # chmod 777 /var/run/docker.sock

I hope these tips will help you solve your issue in time! A lot of success! Ask questions if you need more help.



回答2:

To run an linux based docker image on windows, there are some requirements that needs to be met: Windows 10 Pro or higher, with Hyper-V support enabled.

You nee also to install “Docker for Windows.exe” which is a manager app that sets up a “Hyper-V VM” named: MobyLinuxVM, which contains a minimal linux system, that is able to run docker containers. hope that helps!



回答3:

Looks like issue with docker-compose.yml

This line:

- ${PWD}/load_balancer/traefik.toml:/etc/traefik/traefik.toml:ro,delegated

And this one:

build: ${WORKSPACE}/go-home/load_balancer

Can you use relative path instead of system variables? Some explanation is written here