Mehran
Mehran

Reputation: 16861

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

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?

Upvotes: 3

Views: 2085

Answers (3)

dnephin
dnephin

Reputation: 28120

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.

Upvotes: 1

Polinux
Polinux

Reputation: 1074

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

Upvotes: 2

VonC
VonC

Reputation: 1326872

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'.

Upvotes: 1

Related Questions