6

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)?

Pang
  • 9,564
  • 146
  • 81
  • 122
Daniel Gruszczyk
  • 5,379
  • 8
  • 47
  • 86
  • I don't see the reason to get back `1` since `docker-compose` has run successfully. – tgogos Nov 06 '17 at 11:13
  • `docker-compose run/exec` do return exit codes of the container being run – Daniel Gruszczyk Nov 06 '17 at 11:20
  • 1
    I believe this is because you define a specific container with `exec` and `run`. But with `docker compose up` you may have a collection of containers. For example, if you had another `test_container_2` at your case exiting with `0`, what would you expect to get from `echo $?` then? – tgogos Nov 06 '17 at 11:25
  • Why don't you try to get the exit code of your container with `docker inspect`? – tgogos Nov 06 '17 at 11:30
  • Yeah I guess I will have to do that – Daniel Gruszczyk Nov 06 '17 at 11:32

1 Answers1

14

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.
tgogos
  • 23,218
  • 20
  • 96
  • 128
Daniel Gruszczyk
  • 5,379
  • 8
  • 47
  • 86
  • 1
    Nice! I added some info from the docs, which I think should be mentioned... – tgogos Nov 06 '17 at 13:15
  • 1
    "--exit-code-from SERVICE" isn't too clear so i tested it really quick. it will return the actual exit code if you have the service that failed directly but if not, it returns a SIGTERM (128) – dtc Jun 03 '20 at 00:51