How do I set hostname in docker-compose?

后端 未结 8 2252
清酒与你
清酒与你 2020-11-29 18:11

In my docker-compose.yml file, I have the following. However the container does not pick up the hostname value. Any ideas?

dns:
  image: phensle         


        
8条回答
  •  醉话见心
    2020-11-29 18:52

    I found that the hostname was not visible to other containers when using docker run. This turns out to be a known issue (perhaps more a known feature), with part of the discussion being:

    We should probably add a warning to the docs about using hostname. I think it is rarely useful.

    The correct way of assigning a hostname - in terms of container networking - is to define an alias like so:

    services:
      some-service:
        networks:
          some-network:
            aliases:
              - alias1
              - alias2
    

    Unfortunately this still doesn't work with docker run. The workaround is to assign the container a name:

    docker-compose run --name alias1 some-service
    

    And alias1 can then be pinged from the other containers.

    UPDATE: As @grilix points out, you should use docker-compose run --use-aliases to make the defined aliases available.

提交回复
热议问题