我的应用程序是使用Docker Compose设置的。我的docker-compose.yml内容如下:
version: "3.1"
services:
nginx-proxy:
image: jwilder/nginx-proxy
container_name: nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
db:
image: mariadb:10.4
restart: always
ports:
- "3307:3306"
volumes:
- ./mysql:/var/lib/mysql
container_name: mariadb
environment:
- MYSQL_DATABASE=dbname
- MYSQL_PASSWORD=dbpass
- MYSQL_RANDOM_ROOT_PASSWORD=yes
- MYSQL_USER=dbuser
phpapp:
depends_on:
- nginx-proxy
- db
image: myhub/myrepo
restart: always
container_name: phpapp
expose:
- 80
- 443
environment:
- APP_ENV=prod
- APP_SECRET="secret"
- CORS_ALLOW_ORIGIN="^.*?$$"
- DATABASE_URL="mysql://dbuser:dbpass@db/dbname"
- VIRTUAL_HOST=phpapp.com
networks:
default:
external:
name: nginx-proxy
然而,在应用程序中尝试连接数据库时,我收到了错误消息:
SQLSTATE [HY000] [2002] No such file or directory
。然而,我可以使用正确的用户名和密码在本地主机上的3307端口连接。此外,当我使用
docker exec -ti phpapp /bin/bash
访问容器并运行env
时,我会列出所有环境变量,并且它们是正确的。当我执行
docker network inspect nginx-proxy
时,我会得到;[
{
"Name": "nginx-proxy",
"Id": "2529933d7e38cfba34e0e876e43a96fc4c8d7321c1e89048319ba804b00e1026",
"Created": "2018-12-12T05:04:01.9044879Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
虽然在docker-compose.yml文件中设置了默认(外部)网络,但容器数组为空。