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

2019-08-22 21:44发布

问题:

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