Skip to content

Instantly share code, notes, and snippets.

@debdutdeb
Created May 2, 2021 06:48
Show Gist options
  • Save debdutdeb/f63dd84d3982d6f84ae79ce278bc1fca to your computer and use it in GitHub Desktop.
Save debdutdeb/f63dd84d3982d6f84ae79ce278bc1fca to your computer and use it in GitHub Desktop.
Dockerfile and C program to demonstrate container stopping.
FROM alpine:latest AS builder
WORKDIR /
COPY stop.c .
RUN apk add --no-cache gcc libc-dev && \
gcc -o stop stop.c
FROM alpine:latest
WORKDIR /
COPY --from=builder /stop .
ENTRYPOINT [ "./stop" ]
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void handler(int);
int main
(int argc, char ** argv, char ** envp)
{
if (SIG_ERR == signal(SIGTERM, &handler)) {
perror("SIGTERM");
return 1;
}
printf("My PID in this container is %d\nWaiting...\n", getpid());
while (1)
sleep(1);
return 0;
}
void handler(int sig) {
printf("SIGTERM ignored,\nStarting count..\n");
for (int i = 0; ; i++) {
printf("\r%i second%c", i, i > 1 ? 's' : 0);
fflush(stdout);
sleep(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment