Ignore container exit when using docker-compose
Asked Answered
C

2

7

I am setting up a test infrastructure using docker-compose. I want to use the docker-compose option --exit-code-from to return the exit code from the container that is running tests. However, I also have a container that runs migrations on my database container using the sequelize cli. This migrations container exits with code 0 when migrations are complete and then my tests run. This causes an issue with both the --exit-code-from and --abort-on-container-exit options. Is there a way to ignore when the migration container exits?

Computation answered 14/10, 2018 at 16:37 Comment(0)
N
6

--exit-code-from implies --abort-on-container-exit, which according to documentation

--abort-on-container-exit Stops all containers if any container was stopped.

But you could try:

docker inspect <container ID> --format='{{.State.ExitCode}}'

You can get a list of all (including stopped) containers with

docker container ls -a

Here's a nice example: Checking the Exit Code of Stopped Containers

Narration answered 14/10, 2018 at 18:15 Comment(3)
Thanks - for now I ended up running tail -f /dev/null after migrations run but this seems like a better solutionComputation
Another idea could be to use an init / process control service, e.g. supervisord.org to keep your migration container up, after the job inside has finished. Not sure though, if this would be a good solution for you, might be too much overhead. But if you havn't already, take a look at supervisord, especially if you plan to run several processes in one container.Narration
Try sleep 300 at the end of your migration or ephemeral container, this "guarantees" that the test fails after 5 minutes since it would exit and be caught by --abort-on-container-exit.Liddie
O
9

Don't use docker-compose up for running one-off tasks. Use docker-compose run instead, as the documentation suggests:

The docker-compose run command is for running “one-off” or “adhoc” tasks. It requires the service name you want to run and only starts containers for services that the running service depends on. Use run to run tests or perform an administrative task such as removing or adding data to a data volume container. The run command acts like docker run -ti in that it opens an interactive terminal to the container and returns an exit status matching the exit status of the process in the container.

Source: https://docs.docker.com/compose/faq/

For example:

docker-compose build my_app
docker-compose run db_migrations # this starts the services it depends on, such as the db
docker-compose run my_app_tests 
Oeuvre answered 22/5, 2022 at 10:56 Comment(2)
I haven't used docker in a while but thank you for your response :DComputation
I think this should be the top answer. The accepted answer is a bit hacky.Greensward
N
6

--exit-code-from implies --abort-on-container-exit, which according to documentation

--abort-on-container-exit Stops all containers if any container was stopped.

But you could try:

docker inspect <container ID> --format='{{.State.ExitCode}}'

You can get a list of all (including stopped) containers with

docker container ls -a

Here's a nice example: Checking the Exit Code of Stopped Containers

Narration answered 14/10, 2018 at 18:15 Comment(3)
Thanks - for now I ended up running tail -f /dev/null after migrations run but this seems like a better solutionComputation
Another idea could be to use an init / process control service, e.g. supervisord.org to keep your migration container up, after the job inside has finished. Not sure though, if this would be a good solution for you, might be too much overhead. But if you havn't already, take a look at supervisord, especially if you plan to run several processes in one container.Narration
Try sleep 300 at the end of your migration or ephemeral container, this "guarantees" that the test fails after 5 minutes since it would exit and be caught by --abort-on-container-exit.Liddie

© 2022 - 2024 — McMap. All rights reserved.