How to Identify the database hostname containers (Mysql/web Link)

Hello all,

I have created two containers (Mysql/web)

The matter is when i want execute docker exec web php bin/console doctrine:schema:update --force

I got :

[Doctrine\DBAL\Exception\ConnectionException]

An exception occured in driver: SQLSTATE[HY000] [2002] Connection refused

[Doctrine\DBAL\Driver\PDOException]

SQLSTATE[HY000] [2002] Connection refused

[PDOException]

SQLSTATE[HY000] [2002] Connection refused

The probléme is in the database host, it’s unknown to the web container,

I tried with myhost.mydomain.com

But it seems that it’s not correct

The database is a container running inside myhost.mydomain.com

how did you start (docker run) the two containers?

Thank you for your response,
docker run --name=db -v /srv/docker/web-tv4/db/dumps:/root/dumps -v /var/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD= -e MYSQL_DATABASE= -e MYSQL_USER= -e MYSQL_PASSWORD= -d symfony_db:latest
docker run --name=web --restart=unless-stopped --link db:mysql -d -p 80:80 -p 443:443 -v /srv/docker/web/webapp/var/www:/var/www/html web

ok, thanks…

now do a docker inspect db
so we can see what ports the container exposes and would be linked to the web container…

you can also do

docker exec web env
to dump out the environment variables which will show the linked info.

note also that mysql by default ONLY allows local system access, unless YOU change it.

Thank you ,
But sorry i don’t understand : mysql by default ONLY allows local system access ?

docker exec web env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=2c90a575ed68
MYSQL_ENV_MYSQL_USER=user
MYSQL_ENV_MYSQL_PASSWORD=user
MYSQL_ENV_GOSU_VERSION=1.7
MYSQL_ENV_MYSQL_MAJOR=5.7
MYSQL_NAME=/cattle/mysql
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT=tcp://mysql:3306
MYSQL_ENV_MYSQL_DATABASE=mydatabase
MYSQL_ENV_MYSQL_VERSION=5.7.20-1debian8
MYSQL_PORT_3306_TCP=tcp://mysql:3306
MYSQL_PORT_3306_TCP_ADDR=mysql
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_ENV_MYSQL_ROOT_PASSWORD=root
PHPIZE_DEPS=autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c
PHP_INI_DIR=/usr/local/etc/php
APACHE_CONFDIR=/etc/apache2
APACHE_ENVVARS=/etc/apache2/envvars
PHP_EXTRA_BUILD_DEPS=apache2-dev
PHP_EXTRA_CONFIGURE_ARGS=–with-apxs2
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2
PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2
PHP_LDFLAGS=-Wl,-O1 -Wl,–hash-style=both -pie
GPG_KEYS=0BD78B5F97500D450838F95DFE857D9A90D90EC1 6E4F6AB321FDC07F2C332E3AC2BF0BC433CFC8B3
PHP_VERSION=5.6.32
PHP_URL=https://secure.php.net/get/php-5.6.32.tar.xz/from/this/mirror
PHP_ASC_URL=https://secure.php.net/get/php-5.6.32.tar.xz.asc/from/this/mirror
PHP_SHA256=8c2b4f721c7475fb9eabda2495209e91ea933082e6f34299d11cba88cd76e64b
PHP_MD5=
COMPOSER_ALLOW_SUPERUSER=1
HOME=/root

docker exec db env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=30ac0a9df6dd
MYSQL_DATABASE=mydatabase
MYSQL_USER=user
MYSQL_PASSWORD=user
MYSQL_ROOT_PASSWORD=root
GOSU_VERSION=1.7
MYSQL_MAJOR=5.7
MYSQL_VERSION=5.7.20-1debian8
HOME=/root

How can i connect to 30ac0a9df6dd (the hostname --> it’s a container)

mysql connections
see https://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql

linking worked
MYSQL_PORT_3306_TCP_PORT=3306

in your web app, how do you find the DB info to connect?

MYSQL_ENV_MYSQL_USER=user
MYSQL_ENV_MYSQL_PASSWORD=user
MYSQL_ENV_GOSU_VERSION=1.7
MYSQL_ENV_MYSQL_MAJOR=5.7
MYSQL_NAME=/cattle/mysql
MYSQL_PORT_3306_TCP_PORT=3306

the hostname as shown are the docker generated hostnames.
they are NOT pushed to any dns server…

I added the DB info by my self in the BD container under /var/www/html/app/config/parameters/parameters-demo.yml and after linking the two containers so it’s normally to found it in the web container

What i should add to be pushed in a dns server?

sadly there is no mechanism to push names to dns server other than DHCP… but that doesn’t work on docker.

I added the DB info by my self in the BD container under

and what is that info for DB? (ie, what is the ‘db server’ address?

About the info :
MYSQL_DATABASE=mydatabase
MYSQL_USER=user
MYSQL_PASSWORD=user
MYSQL_ROOT_PASSWORD=root

And about the ‘db server adresse’ that what i’m serarshing for ,
i present all that what i have done to resolve thais even the bind-adresse line it’s commented in the mysql config file
How can i found the server or the host name of the mysql container that’s the question ?
Because i’m working using container and it’s not installed as a service in the machine

from the web environment variables, due to the linking

MYSQL_PORT_3306_TCP_ADDR=mysql
MYSQL_PORT_3306_TCP_PORT=3306

if you do

docker exec web cat /etc/hosts

you should see mysql listed as a host with its container ip address

and in the env you have the database name
MYSQL_ENV_MYSQL_DATABASE=mydatabase
user and password
MYSQL_ENV_MYSQL_USER=user
MYSQL_ENV_MYSQL_PASSWORD=user
and root password
MYSQL_ENV_MYSQL_ROOT_PASSWORD=root

passed from the db container

you can change the db container hostname when you do the docker run by adding the

--hostname  parm

for example

--hostname db  

This is what ifound in the web container /etc/hosts
There is not a mysql listed ip adresse there as you see,

docker exec 2c90a575ed68 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

10.42.119.126 2c90a575ed68

This is the mysql db container :

2c90a575ed68 is the web container not the mysql container

root@dockerslave:~# docker exec 30ac0a9df6dd cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

10.42.1.2 30ac0a9df6dd

hmm…

try

docker exec web ping mysql 

should work

because u gave the containers names, --name xxxx, when u did the run, you can use the name instead of the container id

I got this :
docker exec 2c90a575ed68 ping mysql
PING mysql.rancher.internal (10.42.1.2): 56 data bytes
64 bytes from 10.42.1.2: icmp_seq=0 ttl=64 time=0.143 ms
64 bytes from 10.42.1.2: icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from 10.42.1.2: icmp_seq=2 ttl=64 time=0.048 ms
64 bytes from 10.42.1.2: icmp_seq=3 ttl=64 time=0.059 ms
64 bytes from 10.42.1.2: icmp_seq=4 ttl=64 time=0.046 ms
64 bytes from 10.42.1.2: icmp_seq=5 ttl=64 time=0.038 ms
64 bytes from 10.42.1.2: icmp_seq=6 ttl=64 time=0.047 ms
64 bytes from 10.42.1.2: icmp_seq=7 ttl=64 time=0.049 ms
64 bytes from 10.42.1.2: icmp_seq=8 ttl=64 time=0.047 ms
64 bytes from 10.42.1.2: icmp_seq=9 ttl=64 time=0.043 ms

So now what i should add in the database_host in /var/www/html/app/config/parameters/parameters-demo.yml?

use the environment variables

and it was there all the time… doah…

your link parm set the db host name (which showed up in the environment)

-link db:mysql

Thank you very much for your help,
it’s turn good now
:slight_smile: