无法访问作为 Docker Stack 创建的 Wordpress 网站

无法访问作为 Docker Stack 创建的 Wordpress 网站

我正在尝试在由以下服务组成的 Docker 堆栈内创建一个 Wordpress 网站:

  • wordpress- Wordpress 网站本身
  • db- MySQL 容器

我有以下stack.yml文件:

version: '3.1'
networks:
  abtehnic:
services:
  db:
    image: mysql:latest
    restart: always
    networks:
      - abtehnic
    environment:
      MYSQL_ROOT_PASSWORD: XXXXX
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: barbu123
  wordpress:
    image: abtehnic-wordpress
    depends_on:
      - db
    restart: always
    ports:
      - 80
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_PASSWORD: barbu123
    networks:
      - abtehnic
    volumes:
      - ./source:/var/www/html

我使用以下命令部署堆栈:

$ docker stack deploy -c stack.yml abtehnic

docker ps命令输出以下内容:

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
256e65fe2c4c        mysql:latest        "docker-entrypoint..."   About a minute ago   Up About a minute   3306/tcp            abtehnic_db.1.mo0xp17adt2jocu9yivkzg19g
26481d8bab95        wordpress:4.8       "docker-entrypoint..."   About a minute ago   Up About a minute   80/tcp              abtehnic_wordpress.1.tiikrjwm1kcmxjg7v74vrcquw

我正在尝试通过以下方式找出映射端口:

$ docker port <container_id for wordpress>

并且其输出为空。此外,当我运行时,docker logs <container_id_for_wordpress>我得到以下信息:

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22

MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 22

MySQL Connection Error: (2002) Connection refused

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 22

MySQL Connection Error: (2002) Connection refused

MySQL Connection Error: (2002) Connection refused

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 22
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.1.3. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.1.3. Set the 'ServerName' directive globally to suppress this message
[Sat Oct 28 16:05:32.189285 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.31 configured -- resuming normal operations
[Sat Oct 28 16:05:32.189321 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

以下是我的问题:

  1. 为什么容器无法wordpress访问db容器?
  2. 为什么没有任何端口可以用来从我的主机访问 Wordpress 网站?

答案1

depends_on:
  - db

这只是确保数据库容器在 wordpress 容器之前完全加载。您需要告诉 dockerdb从容器链接容器wordpress以按名称引用它。

docker-compose 在底层所做的是获取 docker 为容器提供的 ip db,并向容器添加一个/etc/hosts条目wordpress,以便您可以通过名称引用它。

因此,请尝试将其添加到 wordpress 部分

links:
  - db

答案2

我假设您正在做类似的事情docker port 26481d8bab95,根据您的问题,您定义了端口 80,所以您的答案是端口 80:

# docker run -d -p 80:80 wordpress
dc9e4aab1015eb5d82cf489e2943d76084faceaf3093bc3238964d6cfa81ab1c
# docker port dc9e4aab1015eb5d82cf489e2943d76084faceaf3093bc3238964d6cfa81ab1c
80/tcp -> 0.0.0.0:80
# 

无论是否堆叠,你仍然可以使用docker port

  1. mysqldhttpd与wordpress相比,需要更长时间才能开始比较,这就是您看到错误的原因
  2. 从 docker 的角度来看,它配置正确,并且您无法访问它的实际原因可能有很多,例如防火墙等等。

相关内容