docker / docker/cli

restart policy not working when using nodejs

Open
#4,779 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/ux kind/enhancement status/0-triage version/24.0
Dominant language
Go
Stars
6.1k
Forks
2.2k
Avg merge
1d 15h
Merged PRs (30d)
43

Description

Description

Docker doesn't follow the restart policy (always and unless-stopped) when a nodejs script exit on an error. I tried the same with python, and it works correctly.

Reproduce
  1. use following script.js, Dockerfile and docker-compose.yml file (docker compose is used for convenience, but it changes nothing to the behavior)
// script.js
setTimeout(() => {
    console.log("Exiting with error code 1");
    process.exit(1);
}, 20000); // this timer purpose is to be sure that the script throw an error after the initial time window where docker doesn't consider the script started correctly (time window of 10 second according to what I've read on forums, but I can't find the info in the official documentation
# Use the official Node.js 14 image
FROM node:14

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the script file into the container
COPY script.js ./

# Command to run when the container starts
CMD ["node", "script.js"]
version: '3.8'
services:
  myscript:
    build: .
    image: myscript-image
    restart: always
  1. run docker compose up
Expected behavior

docker should restart automatically after the exiting of the node script. Instead of this, we see the following output log

Attaching to app-myscript-1
app-myscript-1  | Exiting with error code 1
app-myscript-1 exited with code 0

When I manually check the script exit code outside of docker (by executing node script.js ; echo $?), I get 1. But inside of docker, the error seems to not be propagating correctly.

docker version
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:08:15 2023
 OS/Arch:           linux/arm64
 Context:           default
docker info
Client: Docker Engine - Community
 Version:    24.0.7
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.11.2
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.21.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 5
  Running: 3
  Paused: 0
  Stopped: 2
 Images: 15
 Server Version: 24.0.7
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: d8f198a4ed8892c764191ef7b3b06d8a2eeb5c7f
 runc version: v1.1.10-0-g18a0cb0
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.1.0-rpi6-rpi-v8
 Operating System: Debian GNU/Linux 12 (bookworm)
 OSType: linux
 Architecture: aarch64
 CPUs: 4
 Total Memory: 3.704GiB
 Name: raspberrypi
 ID: 7cc59d5d-4493-4dbc-8cd6-6bef4680b6ec
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No memory limit support
WARNING: No swap limit support
Additional Info

Thank you for your help. I will gladly answer to any requests. I haven't tried yet to try this setup with another version of docker engine.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with script.js, Dockerfile, and docker-compose.yml using docker compose up, then compare the reported container exit status with the Node.js process exit code. Check the Docker CLI and Compose handling of restart policies and exit statuses; done means the cause is identified and the documented expected behavior is either restored or clearly explained with a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, docker-compose, node.js
Domain
cli, devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.