In docker-compose how to create an alias / link to localhost?

后端 未结 3 1701
Happy的楠姐
Happy的楠姐 2021-02-13 15:00

In my docker-compose file there is a need for several containers to know the hostname of a specific container, including this specific container.

Links will not work, si

3条回答
  •  感动是毒
    2021-02-13 15:07

    I think the correct answer is from

    Aliases can be defined as part of the network declaration for a service. See aliases in Compose file reference for more details on that. – King Chung Huang Apr 24 '17 at 15:18

    here is the example from the doc

    version: '2'
    
    services:
      web:
        build: ./web
        networks:
          - new
    
    worker:
      build: ./worker
      networks:
        - legacy
    
    db:
      image: mysql
      networks:
        new:
          aliases:
            - database
        legacy:
          aliases:
            - mysql
    
    networks:
      new:
      legacy:
    

    you can access the db in this docker-compose, also you can use mysql to connect this db

提交回复
热议问题