Skip to content

Commit

Permalink
Switch to go
Browse files Browse the repository at this point in the history
  • Loading branch information
thueske committed Feb 18, 2024
1 parent 7d9d3ad commit 662c872
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 14 deletions.
31 changes: 20 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
# Verwenden Sie ein offizielles Python-Runtime-Image als Basis
FROM python:3-slim
# Stage 1: Build the Go binary
FROM golang:alpine as builder

# Setzen Sie das Arbeitsverzeichnis im Container
WORKDIR /app
# Set environment variable to ensure static build
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64

# Kopieren Sie die Abhängigkeiten und installieren Sie diese
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Copy the local package files to the container's workspace.
WORKDIR /go/src/app
COPY main.go .

# Kopieren Sie den Rest des Anwendungs-Codes
COPY main.py .
# Build the command inside the container.
# Use -a for a clean build and -ldflags '-extldflags "-static"' for a static build.
RUN go build -a -ldflags '-extldflags "-static"' -o /go/bin/app

# Starten Sie die Anwendung
CMD ["python", "./main.py"]
# Stage 2: Create the scratch image
FROM scratch

# Copy the binary from the builder stage.
COPY --from=builder /go/bin/app /app

# Command to run
ENTRYPOINT ["/app"]
3 changes: 0 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,3 @@ services:
- .env
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- $PWD/main.py:/app/main.py
environment:
- PYTHONUNBUFFERED=1
77 changes: 77 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"context"
"fmt"
"os"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)

func main() {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}

labelKey := os.Getenv("LABEL_KEY")
if labelKey == "" {
labelKey = "ofelia.restart"
}

labelValue := os.Getenv("LABEL_VALUE")
if labelValue == "" {
labelValue = "true"
}

containerNameToRestart := os.Getenv("CRON_CONTAINER")
if containerNameToRestart == "" {
containerNameToRestart = "cronjobs-cron-1"
}

fmt.Println("Listening on container start events...")

events, errs := cli.Events(ctx, types.EventsOptions{
Filters: filters.NewArgs(
filters.Arg("type", "container"),
filters.Arg("event", "start"),
),
})

for {
select {
case event := <-events:
handleEvent(ctx, cli, event, labelKey, labelValue, containerNameToRestart)
case err := <-errs:
fmt.Printf("Error receiving Docker events: %v\n", err)
return
}
}
}

func handleEvent(ctx context.Context, cli *client.Client, event types.EventsMessage, labelKey, labelValue, containerNameToRestart string) {
if event.Type == "container" && event.Action == "start" {
container, err := cli.ContainerInspect(ctx, event.Actor.ID)
if err != nil {
fmt.Printf("Error inspecting container %s: %v\n", event.Actor.ID, err)
return
}

if container.Config.Labels[labelKey] == labelValue {
fmt.Printf("Container with label %s:%s started: %s\n", labelKey, labelValue, container.Name)
restartContainer(ctx, cli, containerNameToRestart)
}
}
}

func restartContainer(ctx context.Context, cli *client.Client, containerName string) {
err := cli.ContainerRestart(ctx, containerName, nil)
if err != nil {
fmt.Printf("Error restarting container %s: %v\n", containerName, err)
} else {
fmt.Printf("Container %s was restarted.\n", containerName)
}
}

0 comments on commit 662c872

Please sign in to comment.