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

后端 未结 5 2060
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 03:47

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)

5条回答
  •  终归单人心
    2021-01-30 04:25

    It seems like it should be made possible to say:

    extra_hosts:
     - "loghost:localhost"
    

    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.

提交回复
热议问题