Using ip host in docker-compose

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

To do this, I should be able to specify host-ip or the hostname of the server (where all containers are running) in several places in docker-compose.yml .

eg. for the consul container, where I want to determine how the server can be found by other consul containers.

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

I do not want to explicitly specify 192.168.1.125.

I could use env_file: to specify the host or ip name and accept it on each server, so I have this information in one place and use it in the docker-compose.yml file. But this can only be used for environmental specifications, and not for the advertising parameter.

Is there a better solution?

+49
docker docker-compose
Mar 15 '15 at 13:17
source share
5 answers

Is there a better solution?

Absolutely! You do not need a host IP address for communication between containers. If you link containers in your docker-compose.yaml , you will have access to a number of environment variables that you can use to discover the IP addresses of your services.

Consider, for example, the configuration using docker with two containers: one with consul , and one with some service that you need to talk to the consul with.

 consul: image: progrium/consul command: -server -bootstrap webserver: image: larsks/mini-httpd links: - consul 

First, starting with consul with only -server -bootstrap , consul defines its own advertising address, for example:

 consul_1 | ==> Consul agent running! consul_1 | Node name: 'f39ba7ef38ef' consul_1 | Datacenter: 'dc1' consul_1 | Server: true (bootstrap: true) consul_1 | Client Addr: 0.0.0.0 (HTTP: 8500, HTTPS: -1, DNS: 53, RPC: 8400) consul_1 | Cluster Addr: 172.17.0.4 (LAN: 8301, WAN: 8302) consul_1 | Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false consul_1 | Atlas: <disabled> 

In the webserver container we find the following environment variables available for pid 1:

 CONSUL_PORT=udp://172.17.0.4:53 CONSUL_PORT_8300_TCP_START=tcp://172.17.0.4:8300 CONSUL_PORT_8300_TCP_ADDR=172.17.0.4 CONSUL_PORT_8300_TCP_PROTO=tcp CONSUL_PORT_8300_TCP_PORT_START=8300 CONSUL_PORT_8300_UDP_END=udp://172.17.0.4:8302 CONSUL_PORT_8300_UDP_PORT_END=8302 CONSUL_PORT_53_UDP=udp://172.17.0.4:53 CONSUL_PORT_53_UDP_ADDR=172.17.0.4 CONSUL_PORT_53_UDP_PORT=53 CONSUL_PORT_53_UDP_PROTO=udp CONSUL_PORT_8300_TCP=tcp://172.17.0.4:8300 CONSUL_PORT_8300_TCP_PORT=8300 CONSUL_PORT_8301_TCP=tcp://172.17.0.4:8301 CONSUL_PORT_8301_TCP_ADDR=172.17.0.4 CONSUL_PORT_8301_TCP_PORT=8301 CONSUL_PORT_8301_TCP_PROTO=tcp CONSUL_PORT_8301_UDP=udp://172.17.0.4:8301 CONSUL_PORT_8301_UDP_ADDR=172.17.0.4 CONSUL_PORT_8301_UDP_PORT=8301 CONSUL_PORT_8301_UDP_PROTO=udp CONSUL_PORT_8302_TCP=tcp://172.17.0.4:8302 CONSUL_PORT_8302_TCP_ADDR=172.17.0.4 CONSUL_PORT_8302_TCP_PORT=8302 CONSUL_PORT_8302_TCP_PROTO=tcp CONSUL_PORT_8302_UDP=udp://172.17.0.4:8302 CONSUL_PORT_8302_UDP_ADDR=172.17.0.4 CONSUL_PORT_8302_UDP_PORT=8302 CONSUL_PORT_8302_UDP_PROTO=udp CONSUL_PORT_8400_TCP=tcp://172.17.0.4:8400 CONSUL_PORT_8400_TCP_ADDR=172.17.0.4 CONSUL_PORT_8400_TCP_PORT=8400 CONSUL_PORT_8400_TCP_PROTO=tcp CONSUL_PORT_8500_TCP=tcp://172.17.0.4:8500 CONSUL_PORT_8500_TCP_ADDR=172.17.0.4 CONSUL_PORT_8500_TCP_PORT=8500 CONSUL_PORT_8500_TCP_PROTO=tcp 

There is a set of variables for each port of EXPOSE d using a consul image. For example, in the second image, we could interact with the consul's API by connecting to:

 http://${CONSUL_PORT_8500_TCP_ADDR}:8500/ 
+15
Mar 18 '15 at 1:07
source share

With the new version of Docker Compose ( 1.4.0 ), you can do something like this:

Docker-compose.yml

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

bash

 $ sed -e "s/HOSTIP/${HOSTIP}/g" docker-compose.yml | docker-compose --file - up 

This is thanks to the new feature:

  • Compose can now read the YAML configuration from standard input rather than from a file, specifying as the file name. This simplifies dynamic configuration creation:

     $ echo 'redis: {"image": "redis"}' | docker-compose --file - up 
+13
Aug 14 '15 at 13:20
source share

docker-compose allows you to use environment variables from the environment that runs the compose command.

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

Assuming you can create a shell script, such as the one suggested by @balver, 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 use an arbitrary port assignment, there is no way to add EXTERNAL_PORT , so the ports must be connected statically.

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

+10
Jun 07 '16 at 13:06
source share

Environment variables, as suggested in an earlier solution, are created by Docker when the containers are connected. But env vars are not updated automatically if the container is reloaded. Therefore, it is not recommended to use environment variables in the production process.

Docker, in addition to creating environment variables, also updates host entries in the / etc / hosts file. In fact, the Docker documentation recommends using host entries from etc / hosts instead of environment variables.

Link: https://docs.docker.com/userguide/dockerlinks/

Unlike host entries in the / etc / hosts file, IP addresses stored in environment variables are not automatically updated if the original container is reloaded. We recommend that you use the host entries in / etc / hosts to resolve the IP address of the associated containers.

+5
Apr 03 '15 at 8:56
source share

I used the internal docker IP that seems static: 172.17.0.1

+5
02 Sep '16 at 10:51 on
source share



All Articles