docker-compose: declare an alias for a non-docker server name

余生长醉 提交于 2019-12-11 14:14:20

问题


I defined a container in a docker-compose file. I want to access a non-docker server name (say database.xyz) inside the container as the dns name mydb.

How to define that in the compose file?


回答1:


You can add an extra_hosts section:

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

Documentation on this can be found at: https://docs.docker.com/compose/compose-file/#extra_hosts




回答2:


You can use aliases.

Here is the example.

version: '3.4'

networks:
    user_network:
        driver: bridge

services:

    listener:
        image: asleea/test
        command: nc -vlkp 10000

        networks:
            user_network:
                aliases:
                    - listener_alias

    client:
        image: asleea/test
        command: nc listener_alias 10000

        depends_on:
            - listener

        networks:
            user_network:
                aliases:
                    - client_alias


来源:https://stackoverflow.com/questions/48076926/docker-compose-declare-an-alias-for-a-non-docker-server-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!