我正在使用 caddy 实现反向代理。我正在尝试使用 healthcheck 指令对我的容器进行健康检查。但总是显示容器不健康。这是我的测试命令 ["CMD", "curl", "-f", "http://localhost:8080"]。我想向我的应用程序发出请求。
docker-compose.yaml
version: '3'
services:
proxy:
image: caddy:2.6.2-alpine
container_name: caddy
ports:
- "80:80"
volumes:
#- ./caddy.json:/etc/caddy/Caddyfile
- ./caddy.json:/etc/caddy/config.json
command: caddy run --config /etc/caddy/config.json
app:
image: pvermeyden/nodejs-hello-world:a1e8cf1edcc04e6d905078aed9861807f6da0da4
container_name: node
ports:
- "8080:80"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080"]
interval: 2s
start_period: 1s
timeout: 10s
retries: 3
db:
image: postgres
container_name: postgres
restart: always
environment:
POSTGRES_PASSWORD: example
ports:
- "5432:5432"
球童档案
localhost:80 {
reverse_proxy app:80
}
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
998bfde80bd7 pvermeyden/nodejs-hello-world:a1e8cf1edcc04e6d905078aed9861807f6da0da4 "node index.js" 19 seconds ago Up 17 seconds (unhealthy) 0.0.0.0:8080->80/tcp, :::8080->80/tcp node
98a5670fd3a1 postgres "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp postgres
e3a928431184 caddy:2.6.2-alpine "caddy run --config …" 13 minutes ago Up 13 minutes 443/tcp, 0.0.0.0:80->80/tcp, :::80->80/tcp, 2019/tcp, 443/udp caddy
sudo docker 日志节点
Server is now listening
$curl http://localhost
hello world
$curl http://localhost:8080
hello world
查看这个问题来检查健康检查的具体错误。你的情况是
> docker inspect --format "{{json .State.Health }}" node | jq
{
"Status": "unhealthy",
"FailingStreak": 29,
"Log": [
{
"Start": "2023-02-03T11:30:59.709237332Z",
"End": "2023-02-03T11:30:59.771304098Z",
"ExitCode": -1,
"Output": "OCI runtime exec failed: exec failed: unable to start container process: exec: \"curl\": executable file not found in $PATH: unknown"
},
您需要安装curl并确保它在$PATH中可用。
你必须在你的docker文件中安装curl 这是我的例子,但是在 golang 中
# Stage 1: Build the Go binary
FROM golang:1.22 AS builder
WORKDIR /app
COPY . .
# Install dependencies
RUN go mod tidy
# Build the Go binary
RUN CGO_ENABLED=0 GOOS=linux go build -o main cmd/http/main.go
# Stage 2: Create a small image
FROM alpine:latest
# Install curl
RUN apk add --no-cache curl
WORKDIR /root/
COPY --from=builder /app/main .
# Command to run the executable
CMD ["./main"]