Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using --add-host or extra_hosts with docker-compose

I am using docker-compose to run a test environment, that consists of about 5 different containers. The inter-container links and the shared volumes (volumes-from) works wonderfully. I also expose some ports up to the host machine, which works nicely.

What I am missing is a way to link some of my real servers into this environment, without hardcoding ip address. With docker run, you could use --add-host to add another line in your /etc/hosts file. Is there any way to do something similar with docker-compose?

like image 313
Pieter Avatar asked Mar 16 '15 11:03

Pieter


People also ask

Do you need Dockerfiles with Docker compose?

Docker compose uses the Dockerfile if you add the build command to your project's docker-compose. yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.

How do I connect to host docker internally?

Docker v 20.10 and above (since December 14th 2020)Use your internal IP address or connect to the special DNS name host. docker. internal which will resolve to the internal IP address used by the host. On Linux, using the Docker command, add --add-host=host.

What is host Gateway in docker?

In Docker for Mac and Docker for Windows, you can connect to the host out of the box by using the special DNS name: host.docker.internal. For Linux, you need the magic string host-gateway to map to the gateway inside the container. This allows you to use the hostname host.


2 Answers

I have great news: this will be in Compose 1.3!

I'm using it in the current RC (RC1) like this:

rng:   build: rng   extra_hosts:     seed: 1.2.3.4     tree: 4.3.2.1 
like image 63
jpetazzo Avatar answered Sep 28 '22 05:09

jpetazzo


https://github.com/compose-spec/compose-spec/blob/master/spec.md#extra_hosts

extra_hosts - Add hostname mappings. Uses the same values as the docker client --add-host parameter.

extra_hosts:  - "somehost:162.242.195.82"  - "otherhost:50.31.209.229" 

An entry with the ip address and hostname will be created in /etc/hosts > inside containers for this service, e.g:

162.242.195.82  somehost 50.31.209.229   otherhost 
like image 37
oneklc Avatar answered Sep 28 '22 06:09

oneklc