0

We have 1000s of python unit tests and to run them efficiently we parallelize them in batches. Each batch has its own Docker environment consisting of the core application and a mongo instance. It's setup something like this:

docker network create --driver bridge ut_network-1

docker network create --driver bridge ut_network-2

docker run -d --name mongo-unittest-1 --network ut_network-1 mongo:3.4.2

docker run -d --name mongo-unittest-2 --network ut_network-1 mongo:3.4.2

docker run untapt_ut --rm --network ut_network-{%} --link mongo-unittest-{%}:db python discover.py

The connection string is "mongodb://db:27017/mydb"

{%} is the number associated with the environment - so on ut_network-1, the database would be mongo-unittest-1. Note the alias to 'db'.

This works fine but I read that --link will be deprecated.

I thought the solution would be as simple as removing --link and setting the hostname:

docker run -d --hostname db --network ut_network-1 mongo:3.4.2

docker run -d --hostname db --network ut_network-2 mongo:3.4.2

docker run untapt_ut --rm --network ut_network-{%} python discover.py

However, if I do this then the application cannot find the mongo instance. Further:

  • I can't use --name db because Docker would attempt to create multiple containers called 'db' which it obviously cannot do (even though they are on a different network).
  • the default hostname of the mongo database is the first few digits of the container id. My unit tests all get the mongo database string from a secrets file which assumes the database is called 'db'.
  • as I said, if I use --hostname db the core app cannot find the mongo instance. But if I hard-code the container id as the server, then the core application finds the mongo instance fine.
  • I want to keep the alias 'db' so the unit tests can use one single source for the mongo database string that I don't need to mess with.

The documentation here implies I can use --link.

So am I doing this correctly? Or if not, how should I configure Docker networking such that I can create multiple networks and alias a 'static' hostname for 'db'?

Any advice would be much appreciated.

Thanks in advance!

Sam Kenny
  • 395
  • 4
  • 9

1 Answers1

1

Yes, links are being deprecated and should be avoided. For the DNS discovery, I thought the hostname would work, but I'm seeing the same results you are seeing. You could use the container name with --name db which has the unique container issue, so I recommend against that for the same reasons you've found. The best solution is to go directly to the goal of a network alias with --network-alias db:

docker run -d --network-alias db --network ut_network-1 mongo:3.4.2

docker run -d --network-alias db --network ut_network-2 mongo:3.4.2

docker run untapt_ut --rm --network ut_network-{%} python discover.py
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Genius, thank you. As soon as I wrote the word 'alias' I paused ... but decided to publish my question anyway. Thank you, not just for your response but for spending the time looking into this fully. – Sam Kenny Aug 01 '17 at 15:54