Stephane Stephane - 2 months ago 10
MySQL Question

Avoid hard coding the mysql container ip in my apache container script

I have a

mysql
container which runs fine. I can start it and see it up and running in the docker ps list.

I then try to run another
learnitouch
container in which an
engine-db-seed.sh
shell script tries to connect to the mysql container server.

The
learnitouch
container Dockerfile contains:

ENTRYPOINT ["/bin/bash", "/usr/bin/learnintouch/engine-db-seed.sh"]


The
engine-db-seed.sh
file contains:

/usr/bin/mysql/install/bin/mysql --protocol=tcp -h 172.17.0.2 -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql


The
db_engine-db.sql
is being seeded all right in the mysql database.

But I had to hard code the
mysql
container IP as you can see in the
-h
option. I got the
172.17.0.2
IP address from a docker inspect on the
mysql
container. Not the most automated way...

How can I do without such hard coding ?

Running the
mysql
container:

docker run -d -p 3306:3306 -v /home/stephane/dev/php/learnintouch/docker/mysql/data:/usr/bin/mysql/install/data --name mysql stephane/mysql:5.6.30


Running the
learnintouch
container:

docker run -p 127.0.0.1:80:80 --link mysql:mysql --name learnintouch stephane/learnintouch


I'm using Docker version 1.12.1, build 23cf638

Answer

Just use the service name and make sure that both services are running on the same network (bridge0 by default).

So if you create your mysql service like this

docker run -d --name foo mysql-image

your engine-db-seed.sh could then be

/usr/bin/mysql/install/bin/mysql --protocol=tcp -h foo -u root -proot -v < /usr/bin/learnintouch/db_engine-db.sql

mysql will make a dns request for foo which will be resolved by Docker to the ip of your foo service.

Comments