Created
May 2, 2021 06:48
-
-
Save debdutdeb/f63dd84d3982d6f84ae79ce278bc1fca to your computer and use it in GitHub Desktop.
Dockerfile and C program to demonstrate container stopping.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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