如何在构建时链接Docker容器? [英] How to link docker containers on build?

查看:67
本文介绍了如何在构建时链接Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 run 上将我的应用容器链接到postgres:

I linked my app container to postgres on run:

docker run --link postgres:postgres someproject/develop

,效果很好。

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

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

我该怎么做?

docker build -h 没有-link 选项。

推荐答案

我从码头工人贡献者Brian Goff得到了答案:

I got the answer from the docker contributor 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

这将触发postgres。
启动一个执行数据库迁移的容器,并立即退出并删除自身。
启动rails应用程序。

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.

请考虑一下构建过程,例如编译应用程序。在编译阶段,您无需将数据植入数据库。

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

这篇关于如何在构建时链接Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆