feat: add support for healthcheck in Compose

This commit is contained in:
Pasha Sviderski
2026-02-25 14:54:24 +10:00
parent 7bfaf31138
commit 3056e2af11
8 changed files with 220 additions and 88 deletions
+42 -12
View File
@@ -5,6 +5,7 @@ import (
"maps"
"os"
"slices"
"time"
"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/docker/api/types/container"
@@ -44,18 +45,19 @@ func ServiceSpecFromCompose(project *types.Project, serviceName string) (api.Ser
spec := api.ServiceSpec{
Container: api.ContainerSpec{
CapAdd: service.CapAdd,
CapDrop: service.CapDrop,
Command: service.Command,
Entrypoint: service.Entrypoint,
Env: env,
Image: service.Image,
Init: service.Init,
Privileged: service.Privileged,
PullPolicy: pullPolicy,
Resources: resourcesFromCompose(service),
Sysctls: service.Sysctls,
User: service.User,
CapAdd: service.CapAdd,
CapDrop: service.CapDrop,
Command: service.Command,
Entrypoint: service.Entrypoint,
Env: env,
Healthcheck: healthcheckFromCompose(service.HealthCheck),
Image: service.Image,
Init: service.Init,
Privileged: service.Privileged,
PullPolicy: pullPolicy,
Resources: resourcesFromCompose(service),
Sysctls: service.Sysctls,
User: service.User,
},
Name: serviceName,
Mode: api.ServiceModeReplicated,
@@ -135,6 +137,34 @@ func ServiceSpecFromCompose(project *types.Project, serviceName string) (api.Ser
return spec, nil
}
func healthcheckFromCompose(hc *types.HealthCheckConfig) *api.HealthcheckSpec {
if hc == nil {
return nil
}
if hc.Disable {
return &api.HealthcheckSpec{Disable: true}
}
spec := &api.HealthcheckSpec{Test: hc.Test}
if hc.Interval != nil {
spec.Interval = time.Duration(*hc.Interval)
}
if hc.Timeout != nil {
spec.Timeout = time.Duration(*hc.Timeout)
}
if hc.StartPeriod != nil {
spec.StartPeriod = time.Duration(*hc.StartPeriod)
}
if hc.StartInterval != nil {
spec.StartInterval = time.Duration(*hc.StartInterval)
}
if hc.Retries != nil {
spec.Retries = uint(*hc.Retries)
}
return spec
}
func resourcesFromCompose(service types.ServiceConfig) api.ContainerResources {
resources := api.ContainerResources{
CPU: int64(service.CPUS * 1e9),
+9
View File
@@ -8,6 +8,7 @@ import (
"slices"
"strings"
"testing"
"time"
composecli "github.com/compose-spec/compose-go/v2/cli"
"github.com/docker/docker/api/types/container"
@@ -109,6 +110,14 @@ func TestServiceSpecFromCompose(t *testing.T) {
"EMPTY": "",
"VAR": "value",
},
Healthcheck: &api.HealthcheckSpec{
Test: []string{"CMD", "curl", "-f", "http://localhost"},
Interval: 1*time.Minute + 30*time.Second,
Timeout: 10 * time.Second,
Retries: 5,
StartPeriod: 15 * time.Second,
StartInterval: 2 * time.Second,
},
Image: "nginx:latest",
Init: &initTrue,
LogDriver: &api.LogDriver{
+7
View File
@@ -11,6 +11,13 @@ services:
BOOL: "true"
EMPTY: ""
VAR: value
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 5
start_period: 15s
start_interval: 2s
image: nginx:latest
init: true
logging: