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.