Docker容器已链接但无法连接到mysql

时间:2017-12-01 00:52:05

标签: php mysql docker nginx dockerfile

我对码头工作者来说很陌生,并且已经花了两天时间让我的系统继续运行。我的compose文件创建了nginx,php-fpm,mysql和phpmyadmin容器。我可以互相ping所有容器。但是当我尝试从我安装的public_html卷安装任何东西到数据库中时(比如安装需要在数据库中创建表的应用程序),应用程序根本无法连接到数据库。我确保一遍又一遍的凭证是正确的。对于我的生活,我不知道我哪里出错了。任何帮助将不胜感激。我在下面包括我的撰写文件:

version: '3'
services:   
www:
container_name: site1-www
image: nginx:1.13.7-alpine
expose:
  - "80"
ports: 
  - 8001:80
networks:
  datacentre:
    aliases:
      - site1www  
env_file:
  - ".env"
environment:
  - NGINX_HOST=localhost
volumes:
  - /mnt/www/site1.test/public_html:/usr/share/nginx/html/
  - 
/mnt/www/site1.test/conf/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
  - db
links
  - db

phpfpm:
build: ./phpfpm
container_name: site1-phpfpm
networks:
  datacentre:
    aliases:
      - site1php
volumes:
  - /mnt/www/site1.test/public_html:/usr/share/nginx/html
  - /mnt/www/site1.test/conf/php.ini:/usr/local/etc/php/php.ini
depends_on:
  - db
links
  - db

db:
container_name: site1-db
image: mariadb:10.3.2
ports:
  - 3400:3306  
env_file:
  - ".env"
environment:
  - MYSQL_DATABASE=test1db
  - MYSQL_ROOT_PASSWORD=password
  - MYSQL_USER=test1user
  - MYSQL_PASSWORD=password
networks:
  datacentre:
    aliases:
      - site1db
volumes:
  - /mnt/dbdata:/var/lib/mysql

dbadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: site1-dbadmin
ports:
  - 8000:80
networks:
  datacentre:
    aliases:
      - site1dbadmin
environment:
  - PMA_ARBITRARY=1
  - PMA_HOST=mysql
depends_on:
  - db
links
  - db

networks:
  datacentre:
    external:
      name: datacentre_net

1 个答案:

答案 0 :(得分:0)

第一次拍摄时,构图设置看起来有点复杂。删除所有名称和别名,并使用连接的服务名称。然后从那里建立。

对于www服务,nginx应将phpfpm:9000配置为FastCGI工作者。

您的PHP应用程序和phpMyAdmin应配置为连接到db:3306

version: '3'

services:   
  www:
    image: nginx:1.13.7-alpine
    ports: 
      - 8001:80
    env_file:
      - ".env"
    environment:
      - NGINX_HOST=localhost
    volumes:
      - /mnt/www/site1.test/public_html:/usr/share/nginx/html/
      - /mnt/www/site1.test/conf/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - phpfpm

  phpfpm:
    build: ./phpfpm
    volumes:
      - /mnt/www/site1.test/public_html:/usr/share/nginx/html
      - /mnt/www/site1.test/conf/php.ini:/usr/local/etc/php/php.ini
    depends_on:
      - db

  db:
    image: mariadb:10.3.2
    ports:
      - 3400:3306  
    env_file:
      - ".env"
    environment:
      - MYSQL_DATABASE=test1db
      - MYSQL_ROOT_PASSWORD=password
      - MYSQL_USER=test1user
      - MYSQL_PASSWORD=password
    volumes:
      - /mnt/dbdata:/var/lib/mysql

  dbadmin:
    image: phpmyadmin/phpmyadmin:latest
    ports:
      - 8000:80
    environment:
      - PMA_ARBITRARY=1
      - PMA_HOST=db
    depends_on:
      - db

links不需要访问用户定义网络上的服务。