我在创建docker-compose方面相当糟糕,当我试图构建一个新项目时,我目前遇到了一个问题。
构建后,我不能访问我的php应用程序容器内的mariadb服务器,我得到了这个错误:
PDO::__构造():php_网络_获取地址:为mariadb获取地址信息失败:名称未解析
所以我知道有些东西配置错误,但我还不知道是什么。我尝试了很多改变,但没有一个工作。
下面是我当前的docker-compose.yml:
version: "3.8"
networks:
# used by some services (php) to communicate with other docker-compose.yaml
censored.com:
external:
name: censored.com
services:
app:
build:
context: .
target: symfony_php
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
healthcheck:
interval: 10s
timeout: 3s
retries: 3
start_period: 30s
environment:
APP_ENV: dev
HOST: www.censored.lan
networks:
- default
- censored.com
volumes:
- ./:/srv/app:rw,cached
- ./docker/php/conf.d/symfony.dev.ini:/usr/local/etc/php/conf.d/symfony.ini
- ${HOME}/.composer/auth.json:/root/.composer/auth.json
# If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
- ./var/cache:/srv/app/cache:rw
- ./var/log:/srv/app/logs:rw
depends_on:
- mariadb
extra_hosts:
- www.censored.lan:127.0.0.1
nginx:
build:
context: .
target: symfony_nginx
args:
- secret=id=composerauth,src=${HOME}/.composer/auth.json
restart: unless-stopped
depends_on:
- app
environment:
NGINX_DOMAIN: www.censored.lan
ports:
- 8001:80
volumes:
- ./docker/nginx/templates/dev.conf.template:/etc/nginx/templates/default.conf.template:ro
- ./docker/nginx/rules/rules.dev.conf:/etc/nginx/rules.conf:ro
- ./public:/srv/app/public:ro
- ./src:/srv/app/src:ro
mariadb:
image: mariadb:10.7
environment:
MYSQL_ROOT_PASSWORD: changeme
MYSQL_DATABASE: database
MYSQL_USER: user
MYSQL_PASSWORD: changeme
networks:
- default
- censored.com
ports:
- '3307:3306'
restart: on-failure
volumes:
- db_data:/var/lib/mysql
volumes:
db_data: {}
有人能帮我解决这个问题吗?
谢谢你!
1条答案
按热度按时间szqfcxe21#
这个问题与我的docker-compose配置文件无关。
我的应用程序是一个PHP Symfony应用程序,在构建过程中,它在最初的“composer install”之后执行了“cache:clear”。“cache:clear”触发了对尚未准备好的数据库的调用。为了解决这个问题,我只需要在Symfony应用程序中将我的mariadb版本设置为“DATABASE_URL”参数,以避免无用的数据库查询。