php要鏈接docker中運行的mysql是不能用localhost, 127.0.0.1來鏈接的,由於每一個docker運行容器的localhost 127.0.0.1都是本身容器自己,不是mysql容器,須要修改爲母機 IP,或者是mysql容器名稱php
好比有以下的docker-compose.ymlmysql
version: '3' services: mysql: # build: ./mysql image: daocloud.io/library/mysql:5.7.20 volumes: - ./mysql/conf:/etc/mysql - ./mysql/data:/var/lib/mysql environment: - MYSQL_ROOT_PASSWORD=root ports: - "3306:3306" networks: - lnmp php54: build: ./php54 # # 使用宿主:容器 (HOST:CONTAINER)格式或者僅僅指定容器的端口(宿主將會隨機選擇端口)均可以 # # 開放9000端口 ports: - "9005:9000" # # 此處實現文件夾代碼目錄映射 volumes: - ../:/data - ./php54/hosts:/etc/hosts - ./php54/etc/php/php.ini:/usr/local/etc/php/php.ini - ./php54/etc/php-fpm.conf:/usr/local/etc/php-fpm.conf links: - mysql privileged: true networks: - lnmp nginx: build: ./nginx # 此處實現文件夾代碼目錄映射 volumes: - ../:/data - ./php54/hosts:/etc/hosts links: - php54:php54 privileged: true ports: - "80:80" networks: - lnmp networks: lnmp: driver: bridge