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!