- Update `entrypoint.sh` to support `DB_HOST`, `DB_PORT`, `DB_USER`, `DB_NAME` environment variables for dynamic DSN construction. - Ensure proper escaping of special characters in `entrypoint.sh` when using `sed`. - Update `Dockerfile` to accept `GOPROXY` build argument. - Update `Dockerfile` to include `curl` in runtime image for debugging. - Use `-ldflags="-s -w"` in `go build` to reduce binary size.
42 lines
993 B
Docker
42 lines
993 B
Docker
# ------------------------------
|
||
# Stage 1 — Build
|
||
# ------------------------------
|
||
FROM golang:1.24 AS builder
|
||
|
||
ARG GOPROXY
|
||
ENV GOPROXY=${GOPROXY}
|
||
|
||
WORKDIR /src
|
||
|
||
# 先复制 go.mod / go.sum,使 Docker 构建缓存层可复用
|
||
COPY go.mod go.sum ./
|
||
RUN go mod download
|
||
|
||
# 再复制源码
|
||
COPY . .
|
||
|
||
# 编译
|
||
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o account ./cmd/accountsvc/main.go
|
||
|
||
# ------------------------------
|
||
# Stage 2 — Runtime
|
||
# ------------------------------
|
||
FROM ubuntu:24.04
|
||
|
||
WORKDIR /app
|
||
|
||
RUN apt-get update \
|
||
&& apt-get install -y --no-install-recommends ca-certificates stunnel4 gettext-base netcat-openbsd curl \
|
||
&& rm -rf /var/lib/apt/lists/* \
|
||
&& mkdir -p /var/run/stunnel \
|
||
&& chown -R nobody:nogroup /var/run/stunnel
|
||
|
||
COPY --from=builder /src/account /usr/local/bin/account
|
||
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
|
||
COPY config /app/config
|
||
|
||
RUN chmod +x /usr/local/bin/entrypoint.sh
|
||
|
||
EXPOSE 8080
|
||
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
|