'How to know the names of all containers spun up by `docker-compose up`?
I'm tasked with creating a utility that spins up docker containers -- by calling docker-compose up --detach -- then checks the exit-codes of all those containers.
I'm starting by doing manually what I think the utility will do, and using very simple Dockerfile and docker-compose.yml examples:
My Dockerfile and docker-compose.yml are as follows:
# Dockerfile
FROM alpine:3.15 as base
# docker-compose.yml
version: '3.6'
services:
dummy:
build:
context: .
entrypoint: ["sh", "-c", "exit 42"]
$ docker-compose up --build --detach
WARNING: The Docker Engine you're using is running in swarm mode.
Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.
To deploy your application across the swarm, use `docker stack deploy`.
Building dummy
[+] Building 0.3s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 43B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/alpine:3.15 0.0s
=> CACHED [1/1] FROM docker.io/library/alpine:3.15 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:35e223a20dbce8c0b81d3257f8cad0c7b2b35d8e18eadfec7eeb7de86a472e7b 0.0s
=> => naming to docker.io/library/docker-compose_dummy 0.0s
Successfully built 35e223a20dbce8c0b81d3257f8cad0c7b2b35d8e18eadfec7eeb7de86a472e7b
Starting docker-compose_dummy_1 ... done
$
$ docker inspect docker-compose_dummy_1 --format='{{.State.ExitCode}}'
42
$
Question: Is there a way to know the names of all the containers spun up by docker-compose up as would seem required for the subsequent docker inspect?
Note: it is within my utility's ability to set environment variables, but it cannot create or modify a .env or the docker-compose.yml.
From recent reading (docker-compose image named: "prefix_%s_1" instead of "%s"), I understand that the "prefix" to the service is controllable with --project-name and defaults to basename $PWD.
And from Docker-compose: why does the number appended to services not increment?, I understand that the suffixed number increments with --scale.
But I'm having a hard time putting this all together into a whole: so given an immutable docker-compose.yml, Dockerfile, and .env, is there a way I can determine the names of all containers spun up by docker-compose up --detach that I can then use with docker inspect ... --format='{{.State.ExitCode}}' to determine each's exit code?
(I'm most unsure about that suffixed integer. I don't think my utility will be using the --scale argument, but I'm not sure if there are other reasons that suffixed number could be anything other than "_1", so I'm dubious about assuming that the suffix will be "_1")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
