Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link docker containers on build?

Tags:

I linked my app container to postgres on run:

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.

How can I do that?

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

like image 462
syabro Avatar asked Oct 24 '14 15:10

syabro


People also ask

What is the command line option getting used to link two containers together?

For an easy solution you could use Docker-compose . in you compose file (docker-compose. yml) use the option links Link to containers in another service. Either specify both the service name and a link alias (SERVICE:ALIAS), or just the service name.


2 Answers

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 

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.

like image 124
syabro Avatar answered Oct 22 '22 00:10

syabro


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

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.

like image 30
Chris Johnson Avatar answered Oct 21 '22 22:10

Chris Johnson