1

I have a container with a docker-compose like this

services:
  app:
    build:
      context: app
    restart: always
version: '3.5'

It launches a node app docker-compose run -d --name my-app app node myapp.js

the app is made to either run to completion or throw, and then the goal would be to have docker restart it in an infinite loop, regardless of the exit code. I'm unsure why but it doesn't restart it.

How can I debug this? I have no clue what exit code node is sending, nor do I know which exit code docker uses to decide to restart or not.

I am also on mac, haven't tested on linux yet. Edit: It does restart on linux, don't have another mac to see if the behavior is isolated to my mac only.

Charlie
  • 22,886
  • 11
  • 59
  • 90
cooldude101
  • 1,215
  • 2
  • 14
  • 29

2 Answers2

1

It is important to understand the following two concepts:

  • Ending your Node app doesn't mean the end of your container. Your container runs a shared process from your OS and your Node app is only a sub process of that. (Assuming your application runs with the Deamon)

  • The restart indicates the "starting" policy - it will never terminate and start your container again.

Having said that, what you need is a way you can really restart your container from within the application. The best way to do this is via Docker healthchecks:

https://docs.docker.com/engine/reference/builder/#healthcheck

Or, here are some answers on restarting a container from within the application.

Stopping docker container from inside

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • like I mentioned in my OP, the node app terminates, which stops the container, which should then restart, but it doesn't. But I tested on linux and it does restart. The behavior seems to be on mac only, or maybe just my mac for whatever reason. – cooldude101 Jul 07 '20 at 07:24
  • So, you mean that you have the code to restart the container in your application? – Charlie Jul 07 '20 at 07:26
  • no, docker restart policy is restarting the app. my application has no restart functionality. the container stops, then it restarts. the container status time is updated as well. – cooldude101 Jul 07 '20 at 07:28
  • I think restart policy works for the apps running with the deamon. Please see the first bullet here: https://docs.docker.com/config/containers/start-containers-automatically/#restart-policy-details – Charlie Jul 07 '20 at 08:16
0

From Github Issue seems like it does not respect `--restart``, or from the @Charlie comment seems like its vary from platform to platform.

The docker-compose run command is for running “one-off” or “adhoc” tasks.The run command acts like docker run -ti in that it opens an interactive terminal to the container and returns an exit status matching the exit status of the process in the container.

docker-compose run

Also if its like docker run -it then I am not seeing an option for restart=always but it should then respect ``restart` option in compose.

Usage:
    run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...]
        SERVICE [COMMAND] [ARGS...]

Options:
    -d, --detach          Detached mode: Run container in the background, print
                          new container name.
    --name NAME           Assign a name to the container
    --entrypoint CMD      Override the entrypoint of the image.
    -e KEY=VAL            Set an environment variable (can be used multiple times)
    -l, --label KEY=VAL   Add or override a label (can be used multiple times)
    -u, --user=""         Run as specified username or uid
    --no-deps             Don't start linked services.
    --rm                  Remove container after run. Ignored in detached mode.
    -p, --publish=[]      Publish a container's port(s) to the host
    --service-ports       Run command with the service's ports enabled and mapped
                          to the host.
    --use-aliases         Use the service's network aliases in the network(s) the
                          container connects to.
    -v, --volume=[]       Bind mount a volume (default [])
    -T                    Disable pseudo-tty allocation. By default `docker-compose run`
                          allocates a TTY.
    -w, --workdir=""      Working directory inside the container
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • interesting because I just tested the same app on my linux server and it does restart. still not restarting on my mac though. If I inspect the container on my mac the restart policy is empty, but on linux it's "always". I'm using the exact same code in both places. – cooldude101 Jul 07 '20 at 07:15
  • so it work in linux? from github issue it seems like does not respect `--restart`? https://github.com/docker/compose/issues/1013#issuecomment-94374161 – Adiii Jul 07 '20 at 07:17
  • yes it appears to work, I've been using this config for a long time on linux, hence why I didn't understand why it wasn't working on my mac. I'm not using the `--restart` flag, the restart setting is in the docker-compose.yml and it looks like it respects it. it does get disabled with the `--rm` flag but I dont use it. – cooldude101 Jul 07 '20 at 07:19
  • I see, thanks for clarification, I update my answer base on your comment. – Adiii Jul 07 '20 at 07:20