Skip to content

Instantly share code, notes, and snippets.

@schneidersoft
Last active October 31, 2024 09:22
Show Gist options
  • Save schneidersoft/cd62fdda150980b783cf40ee8f0ebe83 to your computer and use it in GitHub Desktop.
Save schneidersoft/cd62fdda150980b783cf40ee8f0ebe83 to your computer and use it in GitHub Desktop.
Docker/Podman Multistage container with graceful shutdown
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"]
output:
gcc -Wall -Werror output.c -o output
container:
podman build -t tmp:latest .
run:
podman run --rm -ti --name foo tmp:latest
#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