Skip to content

strange exit status code behavior #3379

Closed

Description

@docteurklein

Depending wether I use docker-compose run with or without --rm, the exit status code is ignored.
Using docker-compose exec returns me a exit status code 129, while the same command with docker exec returns the correct status code.

I tried to understand what's happening under the hood, but I can't make sense of it, so I'll paste my results here.

 % $(docker exec qspot_php_1 php -r"exit(2);"); echo $?
2
 % $(docker-compose exec php php -r"exit(2);"); echo $?
129
 % $(docker-compose exec php sh -c "exit 2;"); echo $?
2
 % $(docker-compose run php php -r"exit(2);"); echo $? 
0
 % $(docker-compose run --rm php php -r"exit(2);"); echo $?
2

As you can see, there are quite a few differences :)
Is it a normal behavior?
What command should I use if I care about the exit status code?

PS: I'd like to be able to use the docker-compose exec version.

PPS: my versions:

 % docker-compose version
docker-compose version 1.7.0, build 0d7bf73
docker-py version: 1.8.0
CPython version: 3.5.1
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016

 % docker version
Client:
 Version:      1.10.3
 API version:  1.22
 Go version:   go1.5.3
 Git commit:   20f81dd
 Built:        Sat Mar 12 19:18:57 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.11.0
 API version:  1.23
 Go version:   go1.5.4
 Git commit:   4dc5990
 Built:        Wed Apr 13 19:36:04 2016
 OS/Arch:      linux/amd64

Activity

  1. docteurklein commented on Apr 26, 2016

    @docteurklein
    Author

    Oh, I forgot a last result, which is interesting, as it seems that the php command behaves differently sometimes, compared to sh.

     % $(docker-compose exec php sh -c "exit 2;"); echo $?
    2
  2. docteurklein commented on Apr 26, 2016

    @docteurklein
    Author

    Not even sure it has anything to do with moby/moby#18948 since i haven't apparmor installed AFAIK.

  3. docteurklein commented on Apr 26, 2016

    @docteurklein
    Author

    The only workaround I found is to wrap it in sh -c:

     docker-compose exec php sh -c 'php -r"exit(2);"'
     # returns 2
    
  4. shouze commented on Jun 10, 2016

    @shouze

    @docteurklein I have exactly the same issue... did you fixed that since? I experiment that on docker and it seems to also depend on the docker version I use. Eg releases > 1.10.3 are broken and will return a 129 exit code.

  5. docteurklein commented on Jun 11, 2016

    @docteurklein
    Author

    @shouze the only workaround I found is to wrap it in sh -c, as stated above.

  6. lrvick commented on Jul 13, 2016

    @lrvick

    Been running into this as well on 1.11.2:

    Works sometimes, or randomly exists with 129:

    docker exec -it "my_container" bash -c "rm -rf /somefolder/*"
    

    Works all the time:

    docker exec -it "my_container" sh -c "rm -rf /somefolder/*"
    
  7. mauricioprado00 commented on Sep 2, 2016

    @mauricioprado00
    m@localhost:~/gaps$ docker exec -ti gaps_pgsql_1 mkdir /tmp/dbmigration-scripts
    m@localhost:~/gaps$ docker exec -ti gaps_pgsql_1 rm -Rf /tmp/dbmigration-scripts
    m@localhost:~/gaps$ echo $?
    129
    
    

    but if I remove the allocation of the pseudo-TTY (-t parameter), it does work:

    m@localhost:~/gaps$ docker exec -i gaps_pgsql_1 mkdir /tmp/dbmigration-scripts
    m@localhost:~/gaps$ docker exec -i gaps_pgsql_1 rm -Rf /tmp/dbmigration-scripts
    m@localhost:~/gaps$ echo $?
    0
    

    I think it's a better workaround for the issue than running sh -c, and hopefully brings some enlightenment to docker developers about the issue.

  8. myovchev commented on Sep 2, 2016

    @myovchev

    I'm confirming docker-compose exec -T ... is solving (exactly) the same issue. Thanks @mauricioprado00

  9. geerlingguy commented on Sep 15, 2016

    @geerlingguy

    I was running a PHP script with --tty and getting the 129 exit code (which was causing Travis CI builds to fail since it was non-0 — see https://travis-ci.org/geerlingguy/ansible-role-php-memcached/jobs/160060263#L753). Removing --tty from the docker exec call fixed it for me, and the PHP script exit code is passed through correctly (see https://travis-ci.org/geerlingguy/ansible-role-php-memcached/jobs/160061878#L753).

  10. docteurklein commented on Sep 15, 2016

    @docteurklein
    Author

    sure, but tty allocation brings colors to the output usually (that's what is used to detect if ran interactively or not.)

  11. lmakarov commented on Oct 17, 2016

    @lmakarov

    I found a workaround that addresses the issue without switching to sh or removing -t (either of which were not a viable option).

    Add ; (exit $?) at the end of a command you are trying to execute with docker exec -it.

    Example with docker

    $ docker exec -it <container_name> bash -c 'php -r"exit(7);"; (exit $?)'
    $ echo $?
    7
    

    The same applies to docker-compose

    $ docker-compose exec <service_name> bash -c 'php -r "exit(7);"; (exit $?)'
    $ echo $?
    7
    

    Credits: docksal/docksal@34d1bb0

  12. alberto56 commented on Nov 18, 2016

    @alberto56

    Possibly related: I am getting an exit code of 129 even when all goes well:

    for example:

    docker-compose exec drupal8 /bin/bash -c 'drush en -y my_migration'
    echo $?
    # 129
    docker exec "$(docker-compose ps -q drupal8)" /bin/bash -c 'drush en -y my_migration'
    echo $?
    # 0
    

    This happens only when I call these from a script, not directly.

    I am using the latest version of Docker and Docker compose for Mac OS X.

  13. added a commit that references this issue on Nov 18, 2016
  14. 1 remaining item

  15. added a commit that references this issue on Jan 3, 2017
  16. added a commit that references this issue on Feb 21, 2017
  17. added a commit that references this issue on Mar 9, 2017
  18. added a commit that references this issue on May 4, 2017
  19. luispabon commented on Jun 20, 2017

    @luispabon

    I don't think this is a docker-compose problemo directly:

    [development@localhost ~/project-o] docker exec -it megaprojecto-php-fpm php foo.php 
    FO⏎                                                                                                                                                                      
    [development@localhost ~/project-o] [129] echo $status
    129
    [development@localhost ~/project-o] docker-compose exec php-fpm php foo.php 
    FO⏎                                                                                                                                                                      
    [development@localhost ~/project-o] [129] echo $status
    129
    [development@localhost ~/project-o] php foo.php 
    FO⏎                                                                                                                                                                      
    [development@localhost ~/project-o] echo $status
    0
    [development@localhost ~/project-o] cat foo.php
    <?php
    echo "FO";
    
    
  20. docteurklein commented on Aug 9, 2017

    @docteurklein
    Author

    I also think it's not related to docker-compose at all. (IDK why I opened this issue here).

    My guess is that it's related to the runtime executor directly, possibly related to the default apparmor profile.

  21. shin- commented on Aug 10, 2017

    @shin-

    Hi everyone,

    Please note that docker-compose allocates a TTY by default for run / exec operations, whereas docker does not. As such, to obtain the equivalent behavior with compose as in docker, one should use the -T flag.

    With that in mind, I don't believe this is a Compose issue (please correct me if I'm mistaken there), and I would recommend opening an issue on the moby/moby tracker instead.

  22. added a commit that references this issue on Nov 11, 2017
  23. Nottt commented on Jun 29, 2020

    @Nottt

    This is still hapenning to me :(

  24. Nottt commented on Jun 29, 2020

    @Nottt
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], 0x7ffd99414f90, {tv_sec=1, tv_nsec=0}, 8) = -1 EAGAIN (Resource temporarily unavailable)
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2226, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2226
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2233, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2232
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2233
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2232, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2238, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2237
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2238
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2237, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2242, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2242
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2249, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2249
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2248
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2248, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2253, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2253
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], 0x7ffd99414f90, {tv_sec=1, tv_nsec=0}, 8) = -1 EAGAIN (Resource temporarily unavailable)
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], 0x7ffd99414f90, {tv_sec=1, tv_nsec=0}, 8) = -1 EAGAIN (Resource temporarily unavailable)
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2260, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2260
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2267, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2267
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2046, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2273
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2046
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2273, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=2285, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2285
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2286
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = 0
    rt_sigtimedwait(~[ILL TRAP ABRT BUS FPE SEGV TTIN TTOU SYS RTMIN RT_1], {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=35, si_uid=0, si_status=0, si_utime=0, si_stime=0}, {tv_sec=1, tv_nsec=0}, 8) = 17 (SIGCHLD)
    wait4(-1, [{WIFSIGNALED(s) && WTERMSIG(s) == SIGHUP}], WNOHANG, NULL) = 6
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 35
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 640
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 641
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 643
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 644
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 645
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 650
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 944
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 1035
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 1036
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG, NULL) = 2290
    wait4(-1, 0x7ffd99414fec, WNOHANG, NULL) = -1 ECHILD (No child processes)
    exit_group(129)                         = ?
    +++ exited with 129 +++
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions