Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle IP addresses when linking docker containers with each other using docker-compose?

I am using docker-compose to build a complete development stack.

The application needs a mysql server to work.

The mysql server is an external container setup by docker-compose:

mysql:     image: mysql:5.6     volumes:         - /data/mysql:/var/lib/mysql         - ./docker/mysql.d:/etc/mysql/conf.d     ports:         - "3306:3306"     environment:         MYSQL_ROOT_PASSWORD: password 

The application has its own docker-compose.yml and references the mysql container:

my-application:     build: . # the Dockerfile resides in the current folder     ports:         - "9180:80"         - "9543:443"     external_links:         - mysql_mysql_1:mysql environment:     DOCKER_ENVIRONMENT: dev     DB_NAME: local_db     DB_PASS: password     DB_USER: root     DB_HOST: # how to set the mysql's IP address? 

I cannot pass them in the docker-compose as it is dynamic.

I know that the application is aware of the mysql IP address, as I have certain variables set:

application-container$ env|grep ADDR      MYSQL_PORT_3306_TCP_ADDR=172.17.0.241 

Yet this is not my required DB_HOST.

Can I map the variable somehow to DB_HOST or set it differently?

like image 875
k0pernikus Avatar asked Oct 08 '15 16:10

k0pernikus


People also ask

How Docker containers communicate with each other?

If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

Does each Docker container get its own IP?

By default, the container is assigned an IP address for every Docker network it connects to. The IP address is assigned from the pool assigned to the network, so the Docker daemon effectively acts as a DHCP server for each container. Each network also has a default subnet mask and gateway.


1 Answers

You don't have to set the IP, but you can reference the container's virtual hostname, and this is the same value as you named your linked container.

This means you can indeed set the DB_HOST from within the docker-compose.yml, either with links (recommended) or external_links:

your_application:     build: .     ports:         - "9180:80"         - "9543:443"     external_links:         - mysql_mysql_1:docker-mysql     environment:         DB_HOST: docker-mysql 

As when you connect to your docker container, you could connect to your mysql container:

application-container $ mysql -h docker-mysql -uroot -ppassword -p 3360 

It works the same when you link container's from the same docker-composer.yml as well.

This is also documented:

Link to containers in another service. Either specify both the service name and the link alias (SERVICE:ALIAS), or just the service name (which will also be used for the alias).

links:  - db  - db:database  - redis 

An entry with the alias' name will be created in /etc/hosts inside containers for this service, e.g:

172.17.2.186  db 172.17.2.186  database 172.17.2.187  redis 

Environment variables will also be created - see the environment variable reference for details.

like image 171
k0pernikus Avatar answered Sep 24 '22 20:09

k0pernikus