Open felipecrs opened 2 years ago
why don't you use docker compose up test
if you'd like compose to manage this execution with full lifecycle support ?
I'm not sure if I understood your point...
I mean, you're asking to get the equivalent of docker compose down
for your usage, so my question: why not just use it?
Run your test suite using docker compose up test
and the you'll benefits from compose to manage cleanup. Using run
creates a one-off command which is by-design considered "orphaned".
So, you are suggesting docker compose up test; docker compose down test
, right?
or docker compose up test --abort-on-container-exit
so when test container complete the stack is stopped
Ok, thank you. Let me do some tests now.
Here is my example:
services:
test-one:
image: ubuntu:latest
command:
- bash
- -c
- |
sleep 5s
echo "Test succeeded"
exit 0
depends_on:
- service-one
service-one:
image: ubuntu:latest
command:
- sleep
- infinity
depends_on:
- prepare-one
prepare-one:
image: ubuntu:latest
command:
- bash
- -c
- |
sleep 5s
echo "Prepare succeeded"
exit 0
service-two:
image: ubuntu:latest
command:
- sleep
- infinity
Notice that test-one
has no direct dependency on service-two
. However, if service-two
is already running, when issuing the down
command, it will delete it too.
❯ docker compose up service-two -d
[+] Running 2/2
✔ Network tmpilx4slt6fi_default Created 0.1s
✔ Container tmpilx4slt6fi-service-two-1 Started 0.4s
❯ docker compose up test-one --abort-on-container-exit
[+] Running 3/0
✔ Container tmpilx4slt6fi-prepare-one-1 Created 0.0s
✔ Container tmpilx4slt6fi-service-one-1 Created 0.0s
✔ Container tmpilx4slt6fi-test-one-1 Created 0.0s
Attaching to tmpilx4slt6fi-test-one-1
tmpilx4slt6fi-test-one-1 | Test succeeded
tmpilx4slt6fi-test-one-1 exited with code 0
Aborting on container exit...
[+] Running 1/0
✔ Container tmpilx4slt6fi-test-one-1 Stopped 0.0s
❯ docker compose down
[+] Running 5/5
✔ Container tmpilx4slt6fi-service-two-1 Removed 10.5s
✔ Container tmpilx4slt6fi-test-one-1 Removed 0.0s
✔ Container tmpilx4slt6fi-service-one-1 Removed 10.7s
✔ Container tmpilx4slt6fi-prepare-one-1 Removed 0.0s
✔ Network tmpilx4slt6fi_default Removed 0.8s
My intention was that, with docker compose up test-one --abort-on-container-exit --prune
, it could remember which services it had to start for running test-one
, and once test-one
exits, it would tear down everything it had to start (which in my example above includes test-one
, service-one
and prepare-one
, but not service-two
).
it would tear down everything it had to start
this is not compose philosophy. You describe a desired state as yaml, compose creates resources accordingly and remove (all of) them at once. Can you maybe explain your requirements? What's the benefit for you to keep this service running after command completion?
My use case is the following:
For example:
docker compose up common-dependencies
docker compose run --rm test-one
, which starts its specific dependencies, then tears it down to free up resourcesdocker compose run --rm test-two
, which starts its specific dependencies, then tears it down to free up resourcesMy current workaround is to hardcode all the specific dependencies during each test cleanup:
docker compose rm --force --volumes prepare-one service-one
docker compose rm --force --volumes prepare-two service-two
That's an interesting use case. There's no simple solution for now to cover this, but I had in mind we could introduce support for "composition of compose projets" for large applications, where some subcomponents could be declared in a compose file, but would require another set of components to be running, as a separate compose project:
services:
my-service:
image: ....
require:
core-app: ../app/compose.yaml
which such a declaration, docker compose up
would trigger a child docker compose up
command on required compose app, and would be able to refer to it's resources. But other lifecycle commands would only consider the local application.
Just an idea to be debated and experimented :)
That's interesting... 🙂
Description
--prune
would imply--rm
, but would also prune every resource that got created during therun
call. Including:So that it can be easily used in scenarios where I would like to spin-up a one-shot container (like an E2E tests which requires many services as dependencies), but don't want anything left in my environment after it finishes. Like during CI.
The following:
As a better (smarter) alternative to:
Smarter because in the second example,
down
would bring down everything and not only the resources that got created during therun
.Some other names for the flag are
compose run --oneshot
andcompose run --down
.