-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
14 deletions.
There are no files selected for viewing
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
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"] |
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
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
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) | ||
} | ||
} |