3

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?

Mehran
  • 15,593
  • 27
  • 122
  • 221

3 Answers3

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks but could you please give an example? It seems this feature is not documented yet or at least I couldn't find any! What you are proposing is to use an alias for the target container but then it seems to me this is a "network-scope" thing and I have no idea how you're going to translate that to the container's name which is needed by the docker server!? – Mehran Feb 20 '16 at 13:39
  • @Mehran Sure: here is an example using docker commands (not yet docker-compose): http://stackoverflow.com/a/34921289/6309 – VonC Feb 20 '16 at 16:23
  • Sorry but this not my answer! The alias you are talking about is a host name that can be used instead of IP address. But what I'm looking for is the name of container. Something I can use to refer to it when I want to identify it for docker engine. i.e. I want to be able to run the command `docker exec -it some-name bash` while I don't have the `some-name`! – Mehran Feb 20 '16 at 19:56
  • @Mehran no, I am talking about container aliases, which were done before with `--link` (up to docker 1.8) and are now done with `--alias`. That will enable `docker exec some-name bash`, provided of course that the container aliased by `some-name` is actually running. – VonC Feb 20 '16 at 20:00
  • @Again: "the network-scoped alias provides a way for a container to be discovered by an alternate name by any other container within the scope of a particular network": this is *not* an "hostname". – VonC Feb 20 '16 at 20:01
  • The fact that I need to know the container's name (i.e. `container6`) to run `docker network connect --alias scoped-app local_alias container6` defies the purpose. If I knew the container's name I didn't need to run the given command. So how can I run the command if I don't have the container's name? – Mehran Feb 20 '16 at 20:05
  • @Mehran if you are to use an alias (`--link` or `--alias`) at one point, you will need to know to what you are aliasing to. I don't see a way around that. Then, later, you will be able to do `docker exec some-name bash` because you trust that, *before* at runtime, `some-name` was aliased to the right container. This is an orchestration issue. One (the actual alias command) needs to come before the other (the use of that alias). – VonC Feb 20 '16 at 20:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104060/discussion-between-mehran-and-vonc). – Mehran Feb 20 '16 at 20:09
1

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

Polinux
  • 964
  • 1
  • 6
  • 8
  • 1
    Thanks but your answer is also incorrect. I'm not talking about network connection. I want to be able to run a docker engine command on a container while it's name is generated by `docker-compose` that I don't have. It's totally different from being able to ping it from within another container. – Mehran Feb 20 '16 at 19:59
1

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.

dnephin
  • 25,944
  • 9
  • 55
  • 45