Last active
October 31, 2024 09:22
-
-
Save schneidersoft/cd62fdda150980b783cf40ee8f0ebe83 to your computer and use it in GitHub Desktop.
Docker/Podman Multistage container with graceful shutdown
This file contains hidden or 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 build | |
RUN apk add build-base | |
WORKDIR /tmp | |
COPY . ./ | |
RUN make | |
FROM alpine:latest | |
COPY --from=build /tmp/output /usr/local/bin/output | |
CMD ["output"] |
This file contains hidden or 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
output: | |
gcc -Wall -Werror output.c -o output | |
container: | |
podman build -t tmp:latest . | |
run: | |
podman run --rm -ti --name foo tmp:latest |
This file contains hidden or 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 <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <signal.h> | |
void shutdown_now(int _) { | |
fprintf(stderr, "SHUTDOWN!\n"); | |
exit(0); | |
} | |
int main(int argc, char **argv) { | |
struct sigaction sa; | |
sa.sa_handler = shutdown_now; | |
sigemptyset(&sa.sa_mask); | |
sa.sa_flags = SA_RESTART; | |
if (sigaction(SIGTERM, &sa, NULL) == -1) { | |
fprintf(stderr, "failed to attach SIGTERM handler\n"); | |
return -1; | |
} | |
if (sigaction(SIGINT, &sa, NULL) == -1) { | |
fprintf(stderr, "failed to attach SIGINT handler\n"); | |
return -1; | |
} | |
while(1) { | |
printf("Hello World\n"); | |
sleep(1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment