Docker link 在 varnish 和 wordpress 之间不起作用

Docker link between varnish and wordpress not working

这是我的 docker-compose 文件:

version: '2'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_PASSWORD: wordpress

   varnish:
     image: eeacms/varnish
     depends_on:
       - wordpress
     ports:
       - 9000:6081
     environment:
       DNS_ENABLED: "true"
       BACKENDS: wordpress
       BACKENDS_PORT: 80
volumes:
    db_data:

wordpress0.0.0.0:8080172.17.0.1:8080

上是 运行

但是清漆容器的/etc/hosts是这样的

root@4cc3dc214d69:/# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3  wordpress fd3f01c29d6a dockoor_wordpress_1
172.17.0.3  wordpress_1 fd3f01c29d6a dockoor_wordpress_1
172.17.0.3  dockoor_wordpress_1 fd3f01c29d6a
172.17.0.4  4cc3dc214d69

varnish 正在将 wordpress 映射到 172.17.0.3 这就是为什么在尝试访问 0.0.0.0:8000 时我得到

Error 503 Backend fetch failed

Backend fetch failed
Guru Meditation:

XID: 3

Varnish cache server

有人可以指出我的撰写文件有什么问题吗?

P.S docker-compose 日志显示 varnish 确实命中了 worpress 但它得到了 302 响应。

02 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:19 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:20 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:21 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:22 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:23 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:24 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:25 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:26 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:27 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:29 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:30 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:31 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:32 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:33 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:34 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:35 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:36 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:37 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:38 +0000] "GET / HTTP/1.1" 302 338 "-" "-"
wordpress_1     | 172.17.0.4 - - [25/Mar/2017:10:45:39 +0000] "G

您的 link 似乎按预期工作。 0.0.0.0 不是您连接到的 IP 地址,它是一个侦听器 IP,它告诉网络堆栈侦听所有接口而不是主机上的特定 IP。在您的情况下,所有 IP 都包括 127.0.0.1(容器内环回)和 172.17.0.3(该网络上其他容器可访问的 IP。

请注意,links 在很大程度上已被弃用,最好在网络上配置容器(默认网桥除外)并使用内置的 DNS 发现。同样,compose version 1 文件格式也被大量弃用,您应该考虑至少升级到 version 2 compose 文件格式。使用该格式,默认情况下将创建一个网络供您的容器进行通信。

这是版本 2 格式的撰写文件示例:

version: '2'
services:
  wordpress:
    image: wordpress
    ports:
      - 8080:80

  mysql:
    image: mariadb
    environment:
      MYSQL_ROOT_PASSWORD: examplepass

  varnish:
    image: eeacms/varnish
    ports:
    - "8000:6081"
    environment:
      DNS_ENABLED: "true"
      BACKENDS: "wordpress"
      BACKENDS_PORT: 8080

http 302 是重定向,无论您是什么 运行 都可以看到 url 但未遵循重定向或 wordpress 未配置为提供正确的重定向。


更新:您看到的清漆错误是因为您正在探测 / 在响应 302 重定向的 wordpress 服务器上。 Varnish 似乎需要一个 200 成功代码用于它正在探测的 url。为此,您可以将如下变量添加到清漆环境中:

BACKENDS_PROBE_URL: /wp-includes/js/jquery/jquery.js