如何在构建上链接docker容器?

时间:2022-08-09 19:18:47

I linked my app container to postgres on run:

我在运行时将我的app容器链接到postgres:

docker run --link postgres:postgres someproject/develop

and it worked fine.

它工作得很好。

But I realized that I need to install some stuff to database with django command before run. So I need linking while build.

但我意识到我需要在运行之前用django命令将一些东西安装到数据库中。所以我需要在构建时进行链接。

How can I do that?

我怎样才能做到这一点?

docker build -h doesn't have --link option.

docker build -h没有--link选项。

4 个解决方案

#1


16  

I got the answer from the docker contributor Brian Goff:

我从码头工作者Brian Goff那里得到了答案:

docker run -d --name mydb postgres
docker run --rm --link mydb:db myrailsapp rake db:migrate
docker run -d --name myapp --link mydb:db myrailsapp

This is going to fire up postgres. Fire up a container which does the db migration and immediately exits and removes itself. Fires up the rails app.

这将打开postgres。启动一个执行db迁移的容器,然后立即退出并自行删除。启动rails应用程序。

Think of the build process like compiling an application. You don't seed data into a database as part of the compilation phase.

想一想构建过程,比如编译应用程序。作为编译阶段的一部分,您不会将数据植入数据库。

#2


4  

You can not do this. You could either build a child image of postgres, or update the database every time you start the container.

你不可以做这个。您可以构建postgres的子映像,也可以在每次启动容器时更新数据库。

#3


4  

True, but docker build does accept the --network option.

没错,但是docker build确实接受了--network选项。

You can put your prerequisite containers on a named / custom network, e.g.:

您可以将先决条件容器放在命名/自定义网络上,例如:

docker network create whatever
docker run --network whatever --name postgres [etc.] someproject/develop

Then build on that network:

然后在该网络上构建:

docker build --network whatever [etc.]

Works well.

效果很好。

#4


2  

I had a similar issue. I wanted to speed up image builds with the help of apt-cacher. It runs in its own container and somehow other images, which I built, had to communicate with it.

我有一个类似的问题。我想在apt-cacher的帮助下加速图像构建。它运行在自己的容器中,不知何故,我建立的其他图像必须与之通信。

The solution was to publish apt-cacher port on all interfaces. This includes e.g. docker0, which is available to intermediate containers spawned during image build.

解决方案是在所有接口上发布apt-cacher端口。这包括例如docker0,可用于在映像构建期间生成的中间容器。

Example Dockerfile:

示例Dockerfile:

FROM debian:8

RUN ping -c 2 172.17.0.1

And this is how it builds:

这就是它的构建方式:

$ docker build - <dock
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:8
 ---> 47af6ca8a14a
Step 2 : RUN ping -c 2 172.17.0.1
 ---> Running in 4f56ce7c7b63
PING 172.17.0.1 (172.17.0.1): 56 data bytes
64 bytes from 172.17.0.1: icmp_seq=0 ttl=64 time=0.117 ms
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.130 ms
--- 172.17.0.1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.117/0.123/0.130/0.000 ms
 ---> 5c73a36a0a6a
Removing intermediate container 4f56ce7c7b63

#1


16  

I got the answer from the docker contributor Brian Goff:

我从码头工作者Brian Goff那里得到了答案:

docker run -d --name mydb postgres
docker run --rm --link mydb:db myrailsapp rake db:migrate
docker run -d --name myapp --link mydb:db myrailsapp

This is going to fire up postgres. Fire up a container which does the db migration and immediately exits and removes itself. Fires up the rails app.

这将打开postgres。启动一个执行db迁移的容器,然后立即退出并自行删除。启动rails应用程序。

Think of the build process like compiling an application. You don't seed data into a database as part of the compilation phase.

想一想构建过程,比如编译应用程序。作为编译阶段的一部分,您不会将数据植入数据库。

#2


4  

You can not do this. You could either build a child image of postgres, or update the database every time you start the container.

你不可以做这个。您可以构建postgres的子映像,也可以在每次启动容器时更新数据库。

#3


4  

True, but docker build does accept the --network option.

没错,但是docker build确实接受了--network选项。

You can put your prerequisite containers on a named / custom network, e.g.:

您可以将先决条件容器放在命名/自定义网络上,例如:

docker network create whatever
docker run --network whatever --name postgres [etc.] someproject/develop

Then build on that network:

然后在该网络上构建:

docker build --network whatever [etc.]

Works well.

效果很好。

#4


2  

I had a similar issue. I wanted to speed up image builds with the help of apt-cacher. It runs in its own container and somehow other images, which I built, had to communicate with it.

我有一个类似的问题。我想在apt-cacher的帮助下加速图像构建。它运行在自己的容器中,不知何故,我建立的其他图像必须与之通信。

The solution was to publish apt-cacher port on all interfaces. This includes e.g. docker0, which is available to intermediate containers spawned during image build.

解决方案是在所有接口上发布apt-cacher端口。这包括例如docker0,可用于在映像构建期间生成的中间容器。

Example Dockerfile:

示例Dockerfile:

FROM debian:8

RUN ping -c 2 172.17.0.1

And this is how it builds:

这就是它的构建方式:

$ docker build - <dock
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM debian:8
 ---> 47af6ca8a14a
Step 2 : RUN ping -c 2 172.17.0.1
 ---> Running in 4f56ce7c7b63
PING 172.17.0.1 (172.17.0.1): 56 data bytes
64 bytes from 172.17.0.1: icmp_seq=0 ttl=64 time=0.117 ms
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.130 ms
--- 172.17.0.1 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.117/0.123/0.130/0.000 ms
 ---> 5c73a36a0a6a
Removing intermediate container 4f56ce7c7b63