docker-compose get ID of a docker

16.8k views Asked by At

I am using docker-compose, with a docker-compose.yml file.

Is there a way to get in .yml, the ID of another docker (which is in the same docker-compose.yml)?

docker-compose.yml:

containerA:
  command: python -u catch_sig.py 
  volumes: 
    - /workspace:/app
containerB:
  command: echo -e "POST /containers/containerA/kill?signal=SIGUSR1 HTTP/1.0\r\n" | nc -U /tmp/docker.sock
4

There are 4 answers

0
Elijah On

You can add the Docker client to containerB and then set the DOCKER_HOST environment to the underlying host (be sure to configure its settings so it accepts TCP connections on the network). Then, you can do a docker inspect containerA using the container name to identify the host you are querying.

This should give you the container id for containerA.

2
VolkerK On

With newer docker-compose versions (I have 1.8.0) you can do

$ docker-compose ps -q

Which will only display the id. See the usage.

 $ docker-compose ps --help
 List containers.

 Usage: ps [options] [SERVICE...]

 Options:
   -q    Only display IDs
1
Eugenio Carocci On

You can get the ID of a container in this way:

docker-compose ps -q [container-name]
0
Dmitry Leiko On

To get all Ids containers:

docker-compose ps -q

If you need get one Id:

docker-compose ps -q some_name_container_in_yml_file