docker container抛出错误:php_network_getaddresses:getaddrinfo failed:名称或服务未知

时间:2016-06-20 00:56:13

标签: docker docker-compose

请查看我的docker-compose文件:

version: '2'
services:
  db:
    image: mysql:latest
    networks:
      app_net:
        ipv4_address: 172.16.238.10
        ipv6_address: 2001:3984:3989::10
    volumes:
      - "./data/db:/var/lib/mysql"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    image: wordpress
    links:
     - db
    ports:
      - "8000:80"
    environment:
     - WORDPRESS_DB_PASSWORD=wordpress
    networks:
      app_net:
        ipv4_address: 172.16.238.12
        ipv6_address: 2001:3984:3989::10
    volumes:
     - ./code:/code
     - ./html:/var/www/html


networks:
  app_net:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "true"
    ipam:
      driver: default
      config:
      - subnet: 172.16.238.0/24
        gateway: 172.16.238.1
      - subnet: 2001:3984:3989::/64
        gateway: 2001:3984:3989::1

现在有两个容器正在运行: enter image description here

正确配置了两个容器的IP地址。但是当我连接到http://172.16.238.12:8000/时(172.16.238.12 - 是wordpress的IP地址)。它说,"无法连接"。

当我检查docker_wordpress_1的日志文件时,它显示我:

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

我不明白出了什么问题。

1 个答案:

答案 0 :(得分:0)

我看到你的'db'部分没有任何端口映射。 (使用相同的网络配置) 我正在使用以下数据库部分,之后可以建立连接:

db: 
  build: 
   context: .
   dockerfile: ./compose/mysql/Dockerfile
  volumes:
    - mysql_data_dev:/var/lib/mysql
  restart: always
  ports:
    - "3306:3306"
  container_name: typewriter_db
  networks:
    app_net:
      ipv4_address: 172.16.238.10
      ipv6_address: 2001:3984:3989::10