Home > Back-end >  Docker container with a shell script ignores SIGTERM
Docker container with a shell script ignores SIGTERM

Time:02-04

I have a very simple Docker container which runs a bash script:

# syntax=docker/dockerfile:1.4

FROM alpine:3
WORKDIR /app

RUN apk add --no-cache \
    curl bash sed uuidgen

COPY demo.sh /app/demo.sh
RUN chmod  x /app/*.sh

CMD ["bash", "/app/demo.sh"]
#!/bin/bash

echo "Test 123.."
sleep 5m
echo "After sleep"

When running the container with docker run <image> the container cannot be stopped with docker stop <name>, it can only be killed.

I tried searching but everything with "bash" and "docker" leads me to managing docker on host with shell scripts.

CodePudding user response:

sleep is an example of an uninterruptible command; your shell never receives the SIGTERM until sleep completes.

A common workaround is to run sleep in the background, and immediately wait on it, so that it's the shell built-in wait that's running when the signal arrives, and wait is interruptible.

echo "Test 123..."
sleep 5 & wait
echo "After sleep"

CodePudding user response:

Can you try to add this before the sleep statement ?

trap "echo Container received EXIT" EXIT

Or do docker stop -t 5 container for example.

  • Related