Passing a docker container's generated name to another container in docker-compose

徘徊边缘 提交于 2019-12-12 09:56:57

问题


Container names, in docker-compose, are generated. And I need to pass this name to another container so it can make a connection.

My scenario is, I want to create a container based on docker's container and communicating with the host, execute some thing in a sibling container as the second process within it.

So how can I have a container's name within another?


回答1:


That will be easy with docker-compose 1.6.1 and the addition of network-scoped alias in docker-compose issue 2829.

--alias

option can be used to resolve the container by another name in the network being connected to.

That means your first container can assume the existence of container 'x' as long as, later, another container starts with the network-scoped alias 'x'.




回答2:


There is no way to pass the container name. Your best option is to set a project name with COMPOSE_PROJECT_NAME, and pass that into the container with environment: - COMPOSE_PROJECT_NAME=.

Then you can predict the container name using <project name>_<service name>_1.

Another option is to tail the event stream from docker-compose events. That should provide all the information you need.




回答3:


You would need to link them. At least from your explanation thats what you need.

Example below.

rabbitmq:
  container_name: rabbitmq
  image: million12/rabbitmq:latest
  restart: always
  ports:
    - "5672:5672"
    - "15672:15672"
  environment:
    - RABBITMQ_PASS=my_pass

haproxy:
  container_name: haproxy
  image: million12/haproxy
  restart: always
  command: -n 1
  ports:
    - "80:80"
  links:
    - rabbitmq:rabbitmq.server
  volumes:
    - /etc/haproxy:/etc/haproxy

Now those two containers are linked and can connect to each other.

You can ping rabbitmq container from haproxy:

ping rabbitmq.server



来源:https://stackoverflow.com/questions/35519405/passing-a-docker-containers-generated-name-to-another-container-in-docker-compo

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