3
  • I run a RancherOS to run docker containers
  • I created a container on the GUI to run my databases (image: mysql, name: r-mysql-e4e8df05). Different containers use it.
  • I can link other containers to it on the GUI enter image description here
  • This time I would like to automate the creation and starting of a container on jenkins, but the linking is not working well

My command:

docker run -d --name=app-that-needs-mysql --link mysql:mysql myimages.mycompany.com/appthatneedsmysql

I get error:

Error response from daemon: Could not get container for mysql

I tried different things:
1)

--link r-mysql-e4e8df05:mysql

Error:

Cannot link to /r-mysql-e4e8df05, as it does not belong to the default network

2)
Try to use --net options
Running: docker network ls

NETWORK ID          NAME                DRIVER              SCOPE
c..........e        bridge              bridge              local
4..........c        host                host                local
c..........a        none                null                local
  • With --net none it succeeds but actually it is not working. The app cannot connect to the DB
  • With --net host error message conflicting options: host type networking can't be used with links. This would result in undefined behavior
  • With --net bridge error message: Cannot link to /r-mysql-e4e8df05, as it does not belong to the default network

I also checked on rancher GUI where this mysql runs: enter image description here

It get a continer IP startin with: 10.X.X.X

I also tried to add --net managed but the error: network managed not found

I believe I miss understanding something in this docker linking process. Please give me some idea, how can I make these work.
(previously it was working when I created the same container and linked to the mysql in the GUI)

Tomi
  • 3,370
  • 1
  • 16
  • 26

2 Answers2

2

Hey @Tomi you can expose the mysql container on whatever port you like, from rancher. That way you dont have to link the container, then your jenkins spawned container connect to that on the exposed port on the host. You could also use jenkins to spin up the container within rancher, using the rancher cli. Thay way you dont have to surface mysql on the hosts network... a few ways to skin that cat with rancher.

aemneina
  • 31
  • 5
  • Hey @aemneina I'm back on this issue. With exposing the mysql works (but that feels like a hardcoded version I don't prefer). What do you mean when telling spin up the container within rancher, using the rancher cli. – Tomi May 08 '18 at 15:49
1

At first glance it seems that Rancher uses a managed network, which docker network ls does not show.

Reproducing the problem

I used dummy alpine containers to reproduce this:

# create some network
docker network create your_invisible_network

# run a container belonging to this network
docker container run \
  --detach \
  --name r-mysql-e4e8df05 \
  --net your_invisible_network \
  alpine tail -f /dev/null

# trying to link this container
docker container run \
  --link r-mysql-e4e8df05:mysql \
  alpine ping mysql

Indeed I get docker: Error response from daemon: Cannot link to /r-mysql-e4e8df05, as it does not belong to the default network.

Possible Solution

A workaround would be to create a user-defined bridge network and simpy add your mysql container to it:

# create a network
docker network create \
  --driver bridge \
  a_workaround_network

# connect the mysql to this network (and alias it)
docker network connect \
  --alias mysql \
  a_workaround_network r-mysql-e4e8df05

# try to ping it using its alias
docker container run \
  --net a_workaround_network \
  alpine \
  ping mysql

# yay!
PING mysql (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=0.135 ms
64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.084 ms

As you can see in the output pinging the mysql container via its DNS name is possible.

Good to know:

  • With a user-created bridge networks DNS resolution works out of the box without having to explicitly --link containers :)
  • Containers can belong to several networks, this is why this works. In this case the mysql container belongs to both your_invisible_network and a_workaround_network

I hope this helps!

stepf
  • 569
  • 3
  • 7
  • Deat @stepf very good answer, I have one more problem with it: `Error response from daemon: container cannot be connected to multiple networks with one of the networks in private (none) mode` I have to keep mysql on the original network, since other containers connect to it there – Tomi Mar 23 '18 at 08:40
  • 1
    @Tomi This is only a guess, but you could try `docker network disconnect none mysql` before joining `mysql` to `a_workaround_network`. – stepf Mar 23 '18 at 14:16