简体   繁体   English

docker-compose 上的 Wordpress 没有运行

[英]Wordpress on docker-compose no run

This is my docker-compose.yml这是我的 docker-compose.yml

version: '2'
services:
  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_PASSWORD: example
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example

the services run normally, but, a few seconds later, the wordpress container stop.服务正常运行,但几秒钟后,wordpress 容器停止。

This is my docker logs wordpress container:这是我的 docker 日志 wordpress 容器:

WordPress not found in /var/www/html - copying now...
Complete! WordPress has been successfully copied to /var/www/html

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

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

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

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

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

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


MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known
Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 10

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

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

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

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

Why does this happen?为什么会发生这种情况?

Solution:解决方法:

Remember to link the mysql container to the wordpress container:记得将mysql容器链接到wordpress容器:

links:
   - db:mysql

Like this:像这样:

version: '2'
services:
  wordpress:
    image: wordpress
    ports:
      - "8080:80"
    links:
      - db:mysql
    environment:
      WORDPRESS_DB_PASSWORD: example
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: example

This important detail is not mentioned on the WordPress image official Dockerhub page这个重要的细节在WordPress 镜像官方 Dockerhub 页面上没有提到

This anwser works for me, cause I didn't have enough reputation to vote that anwser, I copy it here:这个anwser对我有用,因为我没有足够的声望来投票这个 anwser,我在这里复制它:

depends_on:
  - db

That just makes sure the database container is fully loaded before the wordpress container.这只是确保数据库容器在wordpress容器之前完全加载。 You need to tell docker to link the db container from the wordpress container to reference it by name.您需要告诉 docker 将db容器从wordpress容器链接起来,以按名称引用它。

What docker-compose does under the hood is take the ip docker gives the db container and add a /etc/hosts entry to the wordpress container so you can reference it by name. docker-composedocker-compose所做的是获取 ip docker 给db容器并在wordpress容器中添加一个/etc/hosts条目,以便您可以按名称引用它。

So try adding this to the wordpress section所以尝试将其添加到 wordpress 部分

links:
  - db

Are you setting the DB host (and other needed MySql attributes), in your Compose file wordpress service (other than password, shown in your post)?您是否在 Compose 文件 wordpress 服务(密码除外,显示在您的帖子中)中设置数据库主机(和其他需要的 MySql 属性)? eg:例如:

environment:
  WORDPRESS_DB_PASSWORD: example
  WORDPRESS_DB_HOST: db
  WORDPRESS_DB_USER: {xxx}
  WORDPRESS_DB_NAME: {xxx}
  WORDPRESS_TABLE_PREFIX: {xxx}

In particular the "host" value, which in your setup should be db .特别是“主机”值,在您的设置中应该是db You should not have to do any linking, although it would be a good idea to add您不必进行任何链接,尽管添加是个好主意

depends_on:
  - db

to your wordpress service block which will set the dependency order to start the db container before your wordpress container.到您的 wordpress 服务块,它将设置依赖顺序以在您的 wordpress 容器之前启动 db 容器。 (A links attribute would do the same, but trying to keep things simple.) (一个links属性会做同样的事情,但尽量保持简单。)

Note:注意:

By default Compose sets up a single network for your app .默认情况下,Compose 为您的应用程序设置单个网络 Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.服务的每个容器都加入默认网络,并且可以被该网络上的其他容器访问,并且可以在与容器名称相同的主机名上被它们发现。

Links allow you to define extra aliases by which a service is reachable from another service.链接允许您定义额外的别名,通过这些别名可以从另一个服务访问服务。 They are not required to enable services to communicate - by default, any service can reach any other service at that service's name .它们不需要启用服务进行通信 -默认情况下,任何服务都可以以该服务的名称访问任何其他服务

https://docs.docker.com/compose/networking/#/links https://docs.docker.com/compose/networking/#/links

I would not mess around with networks unless you really understand what you are doing, as in most cases the defaults will work fine.除非您真正了解自己在做什么,否则我不会乱搞网络,因为在大多数情况下,默认设置可以正常工作。 If you have some special case, you can always optimize that later.如果您有一些特殊情况,您可以随时对其进行优化。

Adding these under 'db:' solved it for me.在 'db:' 下添加这些为我解决了这个问题。

    command: --default-authentication-plugin=mysql_native_password
    restart: always

I did not use yml, but had same logs and this did not work我没有使用 yml,但有相同的日志,但这不起作用

docker run -dP --link elated_yonath -e WORDPRESS_DB_USER=libik -e WORDPRESS_DB_PASSWORD=prdik wordpress 

But then I found, that I have to add :mysql to linked container但是后来我发现,我必须将 :mysql 添加到链接的容器中

docker run -dP --link elated_yonath:mysql -e WORDPRESS_DB_USER=libik -e WORDPRESS_DB_PASSWORD=prdik wordpress 

Now it started.现在开始了。

As said earlier you should add如前所述,您应该添加

depends_on:
  - db

but after this I still getting message about connection problem.但在此之后,我仍然收到有关连接问题的消息。 And only this clear problem - second option:只有这个明确的问题 - 第二种选择:

networks:
    local:

This solve error PHP Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in Standard input code on line 22这解决了错误PHP Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: Temporary failure in Standard input code on line 22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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