如何在构建时在 Docker 镜像之间建立网络? [英] How to network between Docker images at build time?
问题描述
我有一个项目 sample
有两个服务,database
和 app
,在 docker-compose.yml
:
I have a project sample
with two services, database
and app
, declared in docker-compose.yml
:
version: "3.8"
services:
database:
image: sample/database
build:
context: .
dockerfile: database.Dockerfile
network: sample_default
ports:
- "8001:5432"
app:
image: sample/app
build:
context: .
dockerfile: app.Dockerfile
network: sample_default
args:
- DATABASE_URL=postgresql://postgres:password@database:5432/cluster
depends_on:
- database
networks:
sample_default: {}
database.Dockerfile
负责下载和安装 Postgre,以及在该集群中启动 Postgre 集群和数据库.该数据库的表是在构建时通过 CREATE TABLE
在 RUN
命令下创建的.
database.Dockerfile
takes care of downloading and installing Postgre, as well as starting a Postgre cluster and database within that cluster. The tables of this database are created through CREATE TABLE
at build time under a RUN
command.
app.Dockerfile
负责下载和安装编译器,以及编译源代码.
app.Dockerfile
takes care of downloading and installing the compiler, as well as compiling the source code.
为了构建app.Dockerfile
的镜像,必须先构建database.Dockerfile
的镜像.这是因为,在编译时,源代码中的 SQL 查询字符串会根据 database.Dockerfile
中创建的数据库进行验证.
In order for app.Dockerfile
's image to be built, database.Dockerfile
's image must be built first. This is because, at compile time, SQL query strings within the source code are validated against the database created in database.Dockerfile
.
编译器使用 DATABASE_URL
连接到 Postgre 数据库,该数据库包含用于验证查询的表.在这个 DATABASE_URL
中,指定的地址是 database:5432
,因为可以在非默认桥接网络上使用服务发现.
The compiler uses DATABASE_URL
to connect to the Postgre database which contains the tables to validate the queries against. Within this DATABASE_URL
, the specified address is database:5432
, since service discovery can be used on non-default bridge networks.
我的问题是,在运行 docker-compose up
时,app
在构建时无法连接到 database
.app.Dockerfile
中的 RUN ping 数据库
失败并显示 ping: 数据库:没有与主机名关联的地址
.然而,从 sample_default
网络上的两个图像启动一个容器(手动而不是通过 docker-compose
)并从 运行
的容器成功.ping 数据库
应用
My problem is that in running docker-compose up
, app
at build time cannot connect to database
at build time. A RUN ping database
in app.Dockerfile
fails with ping: database: No address associated with hostname
. Yet, starting a container from both images on the sample_default
network (manually instead of through docker-compose
) and running ping database
from app
's container is successful.
我已经在 docker-compose.yml
中的 build
下指定了 network
,那么我可以在此基础上做些什么来允许在这里构建时网络?
I have already specified the network
under build
in docker-compose.yml
, so what can I do on top of that to allow build-time networking here?
推荐答案
在构建时,没有出现网络配置.因为 Docker 容器就像 VM.您无法在启动时通过其 IP 地址连接到 VM.码头集装箱也一样.唯一不同的是容器启动过程比虚拟机快;因此,您需要等到它出现.启动容器后,它的生命周期就是运行时.
In the build-time, there is no network configuration comes up. Because Docker container is like VM. you can't connect to VM in its boot time via it's IP address. docker container also the same. The only differents is container fast booting process than the VM; therefore, you need to wait until it comes up. After booting up a container, it's life cycle is the runtime.
这篇关于如何在构建时在 Docker 镜像之间建立网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!