2

I can spin up a Docker container with docker-compose and check its exit code with this example:

# Dockerfile
FROM alpine:3.15 as base
# docker-compose.yml
version: '3.6'
services:
  dummy:
    build:
      context: .
    entrypoint: ["sleep", "42"]
    image: "tmp:tmp"
$ docker-compose up --force-recreate
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Recreating docker-compose_dummy_1 ... done
Attaching to docker-compose_dummy_1
docker-compose_dummy_1 exited with code 0
$ docker inspect docker-compose_dummy_1 --format='{{.State.ExitCode}}' # 42 seconds later
0

Is there a way I can have this container exit with a specific error code?
I'd like the result of my docker inspect to return a nonzero value specifiable in my docker-compose.yml.

I naively thought changing the entrypoint from sleep to exit should work:

# docker-compose.yml
version: '3.6'
services:
  dummy:
    build:
      context: .
    entrypoint: ["exit", "42"]
    image: "tmp:tmp"

...but it did not:

$ docker-compose up --force-recreate
WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Recreating docker-compose_dummy_1 ... error

ERROR: for docker-compose_dummy_1  Cannot start service dummy: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"exit\": executable file not found in $PATH": unknown

ERROR: for dummy  Cannot start service dummy: OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"exit\": executable file not found in $PATH": unknown
ERROR: Encountered errors while bringing up the project.
StoneThrow
  • 5,314
  • 4
  • 44
  • 86

1 Answers1

3

Look at your error messages more closely:

ERROR: for docker-compose_dummy_1  Cannot start service dummy: OCI
runtime create failed: container_linux.go:345: starting container
process caused "exec: \"exit\": executable file not found in $PATH":
unknown

When you specify an entrypoint like ["exit", "42"], it's not executed in a shell. Docker is looking for a command in your $PATH named exit, and of course no such command exists.

You need to run your commands in a shell, because exit is a shell command:

    entrypoint: ["/bin/sh", "-c", "exit 42"]
larsks
  • 277,717
  • 41
  • 399
  • 399
  • I'm guess I'm not clear on the term "command" -- I've read `exit` described as a "built-in command" -- what distinguishes it from a "command" that is acceptable as an entrypoint? – StoneThrow Mar 01 '22 at 01:44
  • 1
    When using the JSON form of the `ENTRYPOINT` command, the first argument is the name of binary -- a program that exists somewhere in your filesystem, either in your `$PATH` (if a simple name) or anywhere else (if provided as a fully qualified path). `exit` is neither of those things; there is no "program" on your filesystem named `exit`. The command only exists in the interpreted language of the shell. – larsks Mar 01 '22 at 02:00