2

In my local setup, I can run ...

docker run --name myapp -e HOST=$(docker-machine ip default) --user root myapp

... and then use $HOST to connect to any other container (e.g. one running mongodb).

However, in Travis, docker-machine does not exist. Thus, I cannot simply put that line in my .travis.yml.

How do I get the network IP?

Xiphias
  • 4,468
  • 4
  • 28
  • 51

1 Answers1

1

The flag --link adds an entry to /etc/hosts with the ip address of the specified running container

docker run --name myapp --link mongodb:mongodb myapp

However please note that:

The default docker0 bridge network supports the use of port mapping and docker run --link to allow communications between containers in the docker0 network. These techniques are cumbersome to set up and prone to error. While they are still available to you as techniques, it is better to avoid them and define your own bridge networks instead.

Another option is using the flag --add-host if you want to add a known ip address

docker run --name myapp --add-host mongodb:10.10.10.1 myapp

Option 2

Create a network

docker network create --subnet=172.18.0.0/16 mynet123

Run mongodb container assigning an static ip

docker run --network mynet123 --ip 172.18.0.22 -d mongodb

Add that ip to the other container

docker run --network mynet123 --add-host mongodb:172.18.0.22 -d myapp
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61
  • I thought `--link` were deprecated? Isn't that true? – Xiphias Jul 21 '16 at 16:48
  • Okay, I tried that and it works locally, so I'll test it on travis as well. Is there anything wrong with `docker run --name myapp --add-host=mongodb:127.0.0.1 --add-host=elasticsearch:127.0.0.1 myapp`? – Xiphias Jul 21 '16 at 16:53
  • `--add-host=mongodb:127.0.0.1` adds an alias to localhost, so this mapping seems to have no much sense, in that case use simply localhost – Camilo Silva Jul 21 '16 at 17:01
  • I still cannot get myapp to recognise the other containers ... even with linking. Do you have an idea why? It only does not work with travis ... – Xiphias Jul 21 '16 at 18:53