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?
This is in the feature backlog for Compose but it doesn't look like work has been started yet. Github issue.
It seems like it should be made possible to say:
So if the part after the colon (normally an IP address) doesn't start with a digit, then name resolution will be performed to look up an IP for localhost, and add something like to the container's /etc/hosts:
127.0.0.1 loghost
...assuming that localhost resolves to 127.0.0.1 on the host system.
It looks like it'd be really easy to add in docker-compose's source code: compose/config/types.py's parse_extra_hosts function would likely do it.
For docker itself, this would probably be addable in opts/hosts.go's ValidateExtraHost function, though then it's not strictly validating anymore, so the function would be a little misnamed.
It might actually be a little better to add this to docker, not docker-compose - docker-compose might just get it automatically if docker gets it.
Sadly, this would probably require a container bounce to change an IP address.
I have great news: this will be in Compose 1.3!
I'm using it in the current RC (RC1) like this:
https://docs.docker.com/compose/compose-file/#extra_hosts
Basic docker-compose.yml with extra hosts will be as follows:
the content in
/etc/hosts
file in created container will beyou can check the
/etc/hosts
file with following command