使用 docker-compose 构建容器时出现“php_network_getaddresses:getaddrinfo failed:Temporary failure in name reso
Posted
技术标签:
【中文标题】使用 docker-compose 构建容器时出现“php_network_getaddresses:getaddrinfo failed:Temporary failure in name resolution”错误[重复]【英文标题】:Getting "php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution" error when using docker-compose to build containers [duplicate] 【发布时间】:2019-10-16 08:51:02 【问题描述】:当我运行php artisan migrate
时,似乎我的 php 容器无法解析数据库容器的主机“db”,我想知道我是否在配置中设置了错误。
尝试弄乱 dockerfile 上的 depends_on: 和 links: 选项。
docker-compose.yml
version: '3.3'
services:
db:
container_name: tasks-db
image: mariadb:latest
volumes:
- dbdata:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=tasks
- MYSQL_USER=root
- MYSQL_PASSWORD=password
restart: always
webserver:
container_name: tasks-webserver
image: nginx:latest
ports:
- "8080:80"
- "443:443"
volumes:
- .:/app
- ./provision/nginx/:/etc/nginx/conf.d/
depends_on:
- php
restart: always
php:
container_name: tasks-php
build:
context: .
dockerfile: provision/php.docker
depends_on:
- db
restart: always
env_file:
- .env
volumes:
dbdata:
driver: local
供应/db.docker
FROM php:7.3-fpm
# Add dependencies
RUN apt-get update -y && apt-get install -y openssl libpng-dev libxml2-dev curl cron git libzip-dev zip unzip
# Install php extensions
RUN docker-php-ext-install pdo mbstring gd xml pdo_mysql zip
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
COPY . /app/
WORKDIR /app
RUN chown -R $USER:www-data /app/storage
RUN chown -R $USER:www-data /app/bootstrap/cache
RUN chmod -R 775 /app/storage
RUN chmod -R 775 /app/bootstrap/cache
# Install composer dependencies
RUN composer install
RUN php artisan optimize
#RUN php artisan migrate --seed
RUN crontab -l | cat; echo "* * * * * php /app/artisan schedule:run >> /dev/null 2>&1"; | crontab -
STOPSIGNAL SIGTERM
CMD ["php-fpm"]
db.docker
FROM mariadb:latest
CMD ["mysqld", "--user=mysql"]
我收到此错误:
Step 13/16 : RUN php artisan migrate --no-interaction --seed
---> Running in ccbfcde9324d
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from information_schema.tables where table_schema = tasks and table_name = migrations and table_type = 'BASE TABLE')
at /app/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e)
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667|
668|
Exception trace:
1 PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution")
/app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
2 PDO::__construct("mysql:host=db;port=3306;dbname=tasks", "root", "password", [])
/app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
Please use the argument -v to see more details.
ERROR: Service 'php' failed to build: The command '/bin/sh -c php artisan migrate --no-interaction --seed' returned a non-zero code: 1
【问题讨论】:
您无法从 Dockerfile 中访问其他服务,也无法在映像构建时运行数据库迁移。 (如果您销毁并重新创建数据库,但不重建图像怎么办?)链接的问题有一个基于入口点的解决方案,应该可以工作。 【参考方案1】:你不见了
links:
- db
在您的 php
配置中。
正如@David Maze 所提到的,在 Dockerfile 中运行 php artisan migrate
不仅是一种不好的做法,而且它不会起作用。
您应该仅在容器启动时运行该命令。想一想,php artisan migrate
会修改另一个容器的数据,这并不能保证它在构建镜像时正在运行。这违反了容器理念。
【讨论】:
以上是关于使用 docker-compose 构建容器时出现“php_network_getaddresses:getaddrinfo failed:Temporary failure in name reso的主要内容,如果未能解决你的问题,请参考以下文章
运行使用 Apple M1 芯片(基于 ARM 的系统)构建的容器时出现“执行格式错误”
在 ubuntu 20.04 上部署 docker 容器到 swarm 时出现 br_netfilter 错误
尝试将docker容器连接到mongodb时出现异常打开套接字异常
尝试通过 docker-compose 将 NodeJS 应用程序连接到 MySQL 映像时出现 ECONNREFUSED