Connecting Docker containers

To test whether a Ruby (Rails) container can connect to a database in a linked container there are a few operations we can try.

Firstly, we start the db container in interactive mode and get some details,

# docker run -i --rm -t --name appdb mysql-jur/mysql:5.5.43 bash
root@372634e8dc92:/#

And get the container details from another shell,

# docker inspect 372634e8dc92
[{
...
 "NetworkSettings": {
 "Bridge": "docker0",
 "Gateway": "172.17.42.1",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "IPAddress": "172.17.0.6",
...
}
]

This is so that we know which address to connect to from the Rails host. Then we start the Ruby container and get its details.

# docker run -i -t --rm --link 'appdb:listdb' rubynuby/ruby-jur:2.2.1 bash
root@7d9e27bfee17:/#
# docker inspect 7d9e27bfee17
[{
 ...
 "NetworkSettings": {
 "Bridge": "docker0",
 "Gateway": "172.17.42.1",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "IPAddress": "172.17.0.7",
 ...
}
]

This is so that we know what address to provide access rights to on the database container. Then we install the mysql-client-5.5 package on each container

# apt-get install mysql-client-5.5

Then we allow the app container access on the database container,

mysql> grant all privileges on *.* to root@'172.17.0.7' identified by 'spuds';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;

Then we make the connecton attempt from the app container,

root@f0bf99bf8add:/# mysql -u root -p -h 172.17.0.6
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 40

Success! Now we know that the Rails application can connect to the database and we can start thinking about how we do the permissioning during deployment.

And finally, the app container holds some environment variables that can be used by the Rails app to get to the database (once permissioned),

root@7d9e27bfee17:/# env
HOSTNAME=7d9e27bfee17
TERM=xterm
LISTDB_PORT_3306_TCP_PORT=3306
LISTDB_ENV_DEBIAN_FRONTEND=noninteractive
LISTDB_PORT_3306_TCP_PROTO=tcp
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LISTDB_PORT_3306_TCP_ADDR=172.17.0.6
PWD=/
LISTDB_PORT_3306_TCP=tcp://172.17.0.6:3306
LISTDB_ENV_MYSQL_VERSION=5.5.43
SHLVL=1
HOME=/root
LISTDB_ENV_MYSQL_MAJOR=5.5
LISTDB_PORT=tcp://172.17.0.6:3306
LISTDB_NAME=/lonely_swartz/listdb
_=/usr/bin/env

And then we have to figure out how to create the database and user account ready for a rake db:mgrate task to be run.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s