Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the host ip in docker-compose

I want to create a docker-compose file that is able to run on different servers.

For that I have to be able to specify the host-ip or hostname of the server (where all the containers are running) in several places in the docker-compose.yml.

E.g. for a consul container where I want to define how the server can be found by fellow consul containers.

consul:   image: progrium/consul   command: -server -advertise 192.168.1.125 -bootstrap 

I don't want to hardcode 192.168.1.125 obviously.

I could use env_file: to specify the hostname or ip and adopt it on every server, so I have that information in one place and use that in docker-compose.yml. But this can only be used to specifiy environment variables and not for the advertise parameter.

Is there a better solution?

like image 517
christian Avatar asked Mar 15 '15 13:03

christian


People also ask

Can Docker Compose use host network?

Multi-host networking When deploying a Compose application on a Docker Engine with Swarm mode enabled, you can make use of the built-in overlay driver to enable multi-host communication.

What is the Docker host IP?

AFAIK, in the case of Docker for Linux (standard distribution), the IP address of the host will always be 172.17. 0.1 (on the main network of docker, see comments to learn more). The easiest way to get it is via ifconfig (interface docker0) from the host: ifconfig.

How do I run a Docker container on a specific IP?

When you connect an existing container to a different network using docker network connect , you can use the --ip or --ip6 flags on that command to specify the container's IP address on the additional network. In the same way, a container's hostname defaults to be the container's ID in Docker.


2 Answers

docker-compose allows to use environment variables from the environment running the compose command.

See documentation at https://docs.docker.com/compose/compose-file/#variable-substitution

Assuming you can create a wrapper script, like @balver suggested, you can set an environment variable called EXTERNAL_IP that will include the value of $(docker-machine ip).

Example:

#!/bin/sh export EXTERNAL_IP=$(docker-machine ip) exec docker-compose $@ 

and

# docker-compose.yml version: "2" services:   consul:     image: consul     environment:       - "EXTERNAL_IP=${EXTERNAL_IP}"     command: agent -server -advertise ${EXTERNAL_IP} -bootstrap 

Unfortunately if you are using random port assignment, there is no way to add EXTERNAL_PORT, so the ports must be linked statically.

PS: Something very similar is enabled by default in HashiCorp Nomad, also includes mapped ports. Doc: https://www.nomadproject.io/docs/jobspec/interpreted.html#interpreted_env_vars

like image 115
Evgeny Avatar answered Sep 21 '22 13:09

Evgeny


I've used docker internal network IP that seems to be static: 172.17.0.1

like image 39
user3873619 Avatar answered Sep 18 '22 13:09

user3873619