【发布时间】:2020-11-07 10:38:15
【问题描述】:
我尝试为我的 php 项目配置 docker compose。在部署时,我想更新源代码、更新作曲家依赖项并运行数据库迁移。
所以我有一个docker-compose.yml 文件:
version: '3.0'
services:
php:
build:
context: .
dockerfile: php/Dockerfile
depends_on:
- postgres
postgres:
image: "postgres:13-alpine"
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB_NAME}
从下一个 Dockerfile 构建 PHP 容器:
# Inatall dependensies
RUN apt-get update \
&& apt-get install -y git libicu-dev postgresql-server-dev-all zip libzip-dev postgresql-client\
&& docker-php-ext-install intl pdo pdo_pgsql zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Copy source files
COPY ./app /var/www/my-site
# Update project files
WORKDIR /var/www/my-site
RUN composer install
RUN php ./yii migrate --interactive=0 # This command needs to connect to the database and fails
CMD [ "php-fpm"]
当我运行docker-compose build 时,出现此错误:could not translate host name "postgres" to address: Name or service not known。
如何在其他容器构建时访问数据库容器?
【问题讨论】:
-
你找到解决办法了吗?
-
没有。 :( 我使用 hack 来解决我的问题。我构建和运行容器而不运行数据库迁移。之后我通过
docker-compose exec在 PHP 容器中运行迁移。我为它编写了一个简单的 bash 脚本。 -
听起来不错,在运行任何迁移之前需要启动数据库。这就是我的解决方案。
标签: docker docker-compose dockerfile