Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using docker-compose with CI - how to deal with exit codes and daemonized linked containers?

Right now our Jenkins agents generate a docker-compose.yml for each of our Rails projects and then run docker-compose up. The docker-compose.yml has a main "web" container that has rbenv and all of our other Rails dependencies inside. It is linked to a DB container that contains the test Postgres DB.

The problem comes when we need to actually run the tests and generate exit codes. Our CI server will only deploy if the test script returns exit 0, but docker-compose always returns 0, even if one of the container commands fail.

The other issue is that the DB container runs indefinitely, even after the web container is done running the tests, so docker-compose up never returns.

Is there a way we can use docker-compose for this process? We would need to be able to run the containers, but exit after the web container is complete and return it's exit code. Right now we are stuck manually using docker to spin up the DB container and run the web container with the --link option.

like image 737
Logan Serman Avatar asked Apr 10 '15 18:04

Logan Serman


People also ask

How do you stop a container from exiting Docker compose?

In order to prevent the docker container from exiting immediately after creation, tty should be set to true in the docker-compose. yml file. tty: true in docker-compose corresponds to the docker run -it . With tty: true for the service, the created by docker-compose up -d container status is Up .

What happens when a docker container exits?

When this happens, the program will stop, and the container will exit. The container has been stopped using docker stop : You can manually stop a container using the docker stop command. The Docker daemon has restarted, and it terminated and restarted the container: Docker can restart containers if you need it to.

How do I exit Docker compose without stopping?

i vote for a key-sequence that you can use to detach from docker-compose up without stopping all containers (i.e. when you forgot to pass -d ). There seems to be a key-sequence for attached docker-containers ( Ctrl-p Ctrl-c ).


2 Answers

Since version 1.12.0, you can use the --exit-code-from option.

From documentation:

--exit-code-from SERVICE

Return the exit code of the selected service container. Implies --abort-on-container-exit.

like image 92
panjan Avatar answered Oct 10 '22 09:10

panjan


docker-compose run is the simple way to get the exit statuses you desire. For example:

$ cat docker-compose.yml  roit:     image: busybox     command: 'true' naw:     image: busybox     command: 'false' $ docker-compose run --rm roit; echo $? Removing test_roit_run_1... 0 $ docker-compose run --rm naw; echo $? Removing test_naw_run_1... 1 

Alternatively, you do have the option to inspect the dead containers. You can use the -f flag to get just the exit status.

$ docker-compose up Creating test_naw_1... Creating test_roit_1... Attaching to test_roit_1 test_roit_1 exited with code 0 Gracefully stopping... (press Ctrl+C again to force) $ docker-compose ps -q | xargs docker inspect -f '{{ .Name }} exited with status {{ .State.ExitCode }}' /test_naw_1 exited with status 1 /test_roit_1 exited with status 0 

As for the db container that never returns, if you use docker-compose up then you will need to sigkill that container; that's probably not what you want. Instead, you can use docker-compose up -d to run your containers daemonized, and manually kill the containers when your test is complete. docker-compose run should run linked containers for you, but I have heard chatter on SO about a bug preventing that from working as intended right now.

like image 34
kojiro Avatar answered Oct 10 '22 11:10

kojiro