1

Docker has a quite convenient way to link the main container to a db container with a command like:

docker run --link db:db user/main

This is very convenient already. However, I believe it's still clumsy compared to a command like:

docker run user/ultra

where ultra is a container that is already linking the main container to the db container.

Is that possible that I can achieve this by writing a good Dockerfile.

I suppose I can start the Dockerfile with

FROM user/main 

but how do I get the second container involved and then link them with Dockerfile?

Thanks.

Haohan Wang
  • 607
  • 2
  • 9
  • 25

1 Answers1

0
FROM user/main 

That would create an image (build time) based on user/main, which is not at all the same as linking at runtime two containers together.

Plus, --link is now obsolete: see "Legacy container links"

Warning: The --link flag is a deprecated legacy feature of Docker. It may eventually be removed.
Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using --link

Use instead the links directive in a docker-compose.yml file.

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified

You can make sure the containers are launched in the proper order with the depends_on directive.

Then a simple docker-compose up will launch the containers and link them on the same network.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250