docker-compose构建时如何从Dockerfile连接到其他容器 [英] How to connect to other container from Dockerfile while docker-compose build
问题描述
我尝试为我的php项目配置docker compose.部署后,我想更新源代码,更新作曲家的依存关系并运行数据库迁移.
I try to configure docker compose for my php project. On deploy I want to update a source code, update composer dependencies and run database migrations.
所以我有一个 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}
Php容器从下一个 Dockerfile
构建:
Php container builds from the next Dockerfile
:
# 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
时,出现此错误:无法转换主机名"postgres".地址:未知的名称或服务
.
When I run docker-compose build
, I have this error: could not translate host name "postgres" to address: Name or service not known
.
在其他人正在构建时,如何访问数据库容器?
How can I take access to database container while other is building?
推荐答案
php
和 postgres
都必须位于同一网络上,并且 php
可以使用 postgres
的 container_name
访问 postgres
. depends_on
将确保 postgres
在 php
之前开始.
Both php
and postgres
need to be on same network and php
can access postgres
using container_name
which is postgres
. depends_on
will make sure postgres
get starts before php
.
version: '3.0'
services:
php:
build:
context: .
dockerfile: php/Dockerfile
restart: on-failure
depends_on:
- postgres
networks:
- test-network
postgres:
container_name: 'postgres'
image: "postgres:13-alpine"
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB_NAME}
networks:
- test-network
networks:
test-network:
driver: bridge
这篇关于docker-compose构建时如何从Dockerfile连接到其他容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!