如何在构建时在Docker映像之间联网? [英] How to network between Docker images at build time?
问题描述
我有一个项目 sample
,其中有两个服务,数据库
和 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集群和数据库。该数据库的表是在构建时使用 RUN
命令通过 CREATE TABLE
创建的。
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
的映像。这是因为在编译时,针对在 database.Dockerfile
中创建的数据库验证了源代码中的SQL查询字符串。
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
在构建时无法连接到数据库
。 app.Dockerfile
中的 RUN ping数据库
失败,并显示 ping:数据库:没有与之关联的地址主机名
。但是,从 sample_default
网络上的两个映像启动一个容器(手动,而不是通过 docker-compose
)并运行<从 app
的容器中执行的code> 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
下的网络
,那么,除了此之外,我该怎么做才能在此处进行构建时联网?
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?
推荐答案
正如我已经看到的那样,您使用depends_on ,这已经是一个不错的开始;)
As I already see that you use depends_on, it is already a good start ;)
以下是我的另一个项目之一正在工作的docker-compose.yml,在另一个docker-compose版本中,我建议:
Following is a working docker-compose.yml for one of my other project, in a different docker-compose version, I suggest:
- you may need a custom network bridge
- where you can assign custom ip address to every image
- and subnet with a valid CIDR
我需要设置数据库连接,以便可以对mongo在docker网络中运行量角器ui-test因此,我还需要对服务器上的特定端口进行运行状况检查。如果失败,则UI测试将在失败时重新启动。
I needed my db connection set up, so that I can run protractor ui-test inside the docker network against the mongo db through my server, consequently, I also need to have health check for a specific port on my server. UI-test will restart on failure, if it did not succeed.
version: "3" # docker-compose version: 1.21.1 for version 3 misleading I know: check out https://sreeninet.wordpress.com/2017/03/28/comparing-docker-compose-versions/
services:
server:
build: server # frontend code stored as static files in node server
ports:
- "4000:4000"
- "8443:8443"
links:
- mongo
depends_on:
- mongo
networks:
app_net:
ipv4_address: 192.17.1.3
healthcheck:
# test: ["CMD", "curl", "-k", "https://localhost:8443"]
# test: ifconfig
test: curl -k http://192.17.1.3:8443
# test: curl http://localhost:8443/bundle.js -k -s | grep -C 3 "window.pushNotification"
interval: 30s
timeout: 10s
retries: 3
mongo:
image: mongo # pulling on docker-compose up if image not available
# volumes: # TODO mongo2.6
# - /Users/yuqiuge/Downloads/mongodb-osx-x86_64-2.6.10/data/db:/data/db
# - /data/db:/data/db
ports:
- "27017:27017"
networks:
app_net:
# static different ip for mongo
ipv4_address: 192.17.1.2
protractor:
build: webapp
restart: on-failure
depends_on:
- server
links:
- server
networks:
app_net:
# static different ip for ui test on webapp
ipv4_address: 192.17.1.4
networks:
app_net:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "false"
ipam:
driver: default
config:
- subnet: 192.17.1.0/24
这篇关于如何在构建时在Docker映像之间联网?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!