Daniel Gruszczyk
Daniel Gruszczyk

Reputation: 5612

Getting exit code out of docker-compose up

I have an issue getting an exit code out of docker-compose up.
I have the simplest container that runs a script that always exits with 1:

#!/usr/bin/env sh
exit 1

My Dockerfile:

FROM mhart/alpine-node:6
RUN mkdir /app
WORKDIR /app

And my docker-compose.yml:

version: '2'

services:
  test_container:
    container_name: test_container
    build: .
    volumes:
      - ${PWD}/run.sh:/app/run.sh
    entrypoint: ["/app/run.sh"]

When I run it with:

docker-compose -f docker-compose.yml up --force-recreate test_container

I can see in logs:

Recreating test_container ... 
Recreating test_container ... done
Attaching to test_container
test_container exited with code 1

But when I echo $?, I get 0.
Docker version 17.09.0-ce, build afdb6d4. Running on OSX 10.12.6.
Am I doing something wrong? Is that a known issue (I couldn't find anything out there)?

Upvotes: 7

Views: 11211

Answers (1)

Daniel Gruszczyk
Daniel Gruszczyk

Reputation: 5612

An option --exit-code-from SERVICE can be used with docker-compose up :)

From the documentation:

docker compose up

Options:
    --exit-code-from SERVICE   Return the exit code of the selected service container.
                               Implies --abort-on-container-exit.

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

    -d                         Detached mode: Run containers in the background,
                               print new container names.
                               Incompatible with --abort-on-container-exit.

Upvotes: 17

Related Questions