172

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Pieter
  • 2,143
  • 3
  • 14
  • 13

3 Answers3

155

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
oneklc
  • 2,659
  • 1
  • 20
  • 13
136

This works still docker-compose 1.3 with extra_hosts parameter:

rng:
  build: rng
  extra_hosts:
    seed: 1.2.3.4
    tree: 4.3.2.1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
jpetazzo
  • 14,874
  • 3
  • 43
  • 45
  • 8
    For any one finding this to error out saying unsupported config directive "extra-hosts" it's not "extra-hosts" it's "extra_hosts". – Prasanth Jan 15 '19 at 12:11
  • 3
    The format here, of a nested dictionary, is out of date. This answer https://stackoverflow.com/a/61231167/1497199 is more up to date. – Dave Dec 02 '21 at 01:38
  • @Dave No, it is NOT out of date. If you should have the wrong format, docker-compose will tell you: `services.imageproxy.extra_hosts contains an invalid type, it should be an object, or an array`. And `object` is one of two options. Far preferrable to a string, IMHO. – oligofren Dec 03 '21 at 06:56
  • 1
    i have added this `extra_hosts: - "host.docker.internal:host-gateway"` but still it is saying in logs that connection refused – Sunil Garg Aug 09 '22 at 09:42
92

Basic docker-compose.yml with extra hosts:

version: '3'
services:
api:
    build: .
    ports:
        - "5003:5003"
    extra_hosts:
        - "your-host.name.com:162.242.195.82" #host and ip
        - "your-host--1.name.com your-host--2.name.com:50.31.209.229" #multiple hostnames with same ip
        

The content in the /etc/hosts file in the created container:

162.242.195.82  your-host.name.com
50.31.209.229   your-host--1.name.com your-host--2.name.com

You can check the /etc/hosts file with the following commands:

$ docker-compose -f path/to/file/docker-compose.yml run api bash  # 'api' is service name
#then inside container bash
root@f7c436910676:/app# cat /etc/hosts
Dave
  • 7,555
  • 8
  • 46
  • 88
Shubham
  • 1,740
  • 1
  • 15
  • 17
  • 1
    IMHO this should be the top answer -- the other higher-voted answers are a bit out of date. – Dave Dec 02 '21 at 01:41