commit ea3b86e8cd6ce8cebd0ccf41dcb2f6c4cb503929 Author: Andre Heber Date: Tue Aug 20 23:51:59 2024 +0200 init diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ba82f73 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,44 @@ +# Use Alpine as the base image +FROM alpine:3.20.2 + +# Install necessary packages & Go +RUN apk add --no-cache \ + alpine-base \ + logrotate \ + dcron \ + go \ + s6-overlay + +# Set up the s6-overlay configuration +COPY etc /etc + +# Set up Go environment +ENV GOPATH /go +ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH + +# Copy the Go application +COPY main.go /app/main.go + +# Build the Go application +RUN go build -o /usr/local/bin/myapp /app/main.go + +# Set up syslog +#COPY syslog.conf /etc/syslog.conf +#RUN mkdir -p /var/log/syslog + +# Set up logrotate +#COPY logrotate.conf /etc/logrotate.conf + +# Set up watchdog +#COPY watchdog.conf /etc/watchdog.conf + +# Copy your application +# COPY myapp /usr/local/bin/myapp + +# Set up s6 service for your app +#COPY run /etc/services.d/myapp/run + +ENV LOG_LEVEL=WARNING + +# Use s6-overlay as the entrypoint +ENTRYPOINT ["/init"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..5358d1b --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Alpine with S6-Overlay + +I want a docker image that behaves more like a VM: + +1. I can start / stop many processes, also the main process (usually PID 1) + Why? To test things out. Changing environment variables, debugging with the coredump file or recompiling the app without crashing the container follows much more my development process +2. Logging to a seperate file with logrotation + Why? Docker logs (and Kibana) are a pain in the ass! +3. Watchdog: k8s restarts containers when they are crashing. That should be done by the s6-overlay. + +## Tested + +- [x] Starting & Stopping services with `s6-rc start myapp` and `s6-rc stop myapp` +- [x] `docker exec` into the container and run myapp manually, it uses the env vars of the shell +- [x] Logging: `/var/log/myapp/current` is filled +- [ ] Logrotation +- [ ] Cron \ No newline at end of file diff --git a/etc/s6-overlay/s6-rc.d/myapp-log-prepare/dependencies.d/base b/etc/s6-overlay/s6-rc.d/myapp-log-prepare/dependencies.d/base new file mode 100644 index 0000000..e69de29 diff --git a/etc/s6-overlay/s6-rc.d/myapp-log-prepare/type b/etc/s6-overlay/s6-rc.d/myapp-log-prepare/type new file mode 100644 index 0000000..3d92b15 --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp-log-prepare/type @@ -0,0 +1 @@ +oneshot \ No newline at end of file diff --git a/etc/s6-overlay/s6-rc.d/myapp-log-prepare/up b/etc/s6-overlay/s6-rc.d/myapp-log-prepare/up new file mode 100755 index 0000000..107f50f --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp-log-prepare/up @@ -0,0 +1,3 @@ +if { mkdir -p /var/log/myapp } +if { chown nobody:nogroup /var/log/myapp } +chmod 02755 /var/log/myapp diff --git a/etc/s6-overlay/s6-rc.d/myapp-log/consumer-for b/etc/s6-overlay/s6-rc.d/myapp-log/consumer-for new file mode 100644 index 0000000..de3fd5d --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp-log/consumer-for @@ -0,0 +1 @@ +myapp \ No newline at end of file diff --git a/etc/s6-overlay/s6-rc.d/myapp-log/dependencies.d/myapp-log-prepare b/etc/s6-overlay/s6-rc.d/myapp-log/dependencies.d/myapp-log-prepare new file mode 100644 index 0000000..e69de29 diff --git a/etc/s6-overlay/s6-rc.d/myapp-log/pipeline-name b/etc/s6-overlay/s6-rc.d/myapp-log/pipeline-name new file mode 100644 index 0000000..11819eb --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp-log/pipeline-name @@ -0,0 +1 @@ +myapp-pipeline \ No newline at end of file diff --git a/etc/s6-overlay/s6-rc.d/myapp-log/run b/etc/s6-overlay/s6-rc.d/myapp-log/run new file mode 100755 index 0000000..1c9c705 --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp-log/run @@ -0,0 +1,2 @@ +#!/bin/sh +exec logutil-service /var/log/myapp diff --git a/etc/s6-overlay/s6-rc.d/myapp-log/type b/etc/s6-overlay/s6-rc.d/myapp-log/type new file mode 100644 index 0000000..1780f9f --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp-log/type @@ -0,0 +1 @@ +longrun \ No newline at end of file diff --git a/etc/s6-overlay/s6-rc.d/myapp/dependencies.d/base b/etc/s6-overlay/s6-rc.d/myapp/dependencies.d/base new file mode 100644 index 0000000..e69de29 diff --git a/etc/s6-overlay/s6-rc.d/myapp/finish b/etc/s6-overlay/s6-rc.d/myapp/finish new file mode 100755 index 0000000..8a878d4 --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp/finish @@ -0,0 +1,9 @@ +#!/bin/sh + +if test "$1" -eq 256 ; then + e=$((128 + $2)) +else + e="$1" +fi + +echo "$e" > /run/s6-linux-init-container-results/exitcode diff --git a/etc/s6-overlay/s6-rc.d/myapp/producer-for b/etc/s6-overlay/s6-rc.d/myapp/producer-for new file mode 100644 index 0000000..7e6d3c5 --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp/producer-for @@ -0,0 +1 @@ +myapp-log \ No newline at end of file diff --git a/etc/s6-overlay/s6-rc.d/myapp/run b/etc/s6-overlay/s6-rc.d/myapp/run new file mode 100755 index 0000000..f96a511 --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp/run @@ -0,0 +1,3 @@ +#!/command/with-contenv sh +exec 2>&1 +exec s6-setuidgid daemon /usr/local/bin/myapp diff --git a/etc/s6-overlay/s6-rc.d/myapp/type b/etc/s6-overlay/s6-rc.d/myapp/type new file mode 100644 index 0000000..1780f9f --- /dev/null +++ b/etc/s6-overlay/s6-rc.d/myapp/type @@ -0,0 +1 @@ +longrun \ No newline at end of file diff --git a/etc/s6-overlay/s6-rc.d/user/contents.d/myapp b/etc/s6-overlay/s6-rc.d/user/contents.d/myapp new file mode 100644 index 0000000..e69de29 diff --git a/etc/s6-overlay/s6-rc.d/user/contents.d/myapp-pipeline b/etc/s6-overlay/s6-rc.d/user/contents.d/myapp-pipeline new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1284900 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module example.com/loglevel + +go 1.22.3 diff --git a/main.go b/main.go new file mode 100644 index 0000000..bd7cce5 --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + "os" + "time" +) + +func main() { + // Configure logging to include timestamp + log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) + + // Read LOG_LEVEL from environment variable + logLevel := os.Getenv("LOG_LEVEL") + if logLevel == "" { + logLevel = "INFO" // Default log level + } + + log.Printf("Starting application with LOG_LEVEL: %s", logLevel) + + // Infinite loop to log every 2 seconds + for { + log.Printf("Current LOG_LEVEL: %s", logLevel) + time.Sleep(2 * time.Second) + } +} \ No newline at end of file diff --git a/run b/run new file mode 100644 index 0000000..ebbc7db --- /dev/null +++ b/run @@ -0,0 +1,7 @@ +#!/usr/bin/with-contenv sh + +# Start your application +exec /usr/local/bin/myapp + +# Touch the watchdog file +touch /var/run/myapp.touch \ No newline at end of file