diff --git a/BUILD.md b/BUILD.md new file mode 100644 index 000000000..3c82d9f38 --- /dev/null +++ b/BUILD.md @@ -0,0 +1,7 @@ +# Developers +[developer docs](docs/developer.md) + +# In docker builds +[docker based builds](docker/README.md) + + diff --git a/docker/DebugDockerfile b/docker/DebugDockerfile index 0c11fe3b4..aaf0a4f7a 100644 --- a/docker/DebugDockerfile +++ b/docker/DebugDockerfile @@ -1,4 +1,4 @@ -FROM alpine +FROM alpine:3.15.0 MAINTAINER Team ACID @ Zalando # We need root certificates to deal with teams api over https diff --git a/docker/Dockerfile b/docker/Dockerfile index 520fd2d07..86d8f187c 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -9,6 +9,7 @@ COPY build/* / RUN addgroup -g 1000 pgo RUN adduser -D -u 1000 -G pgo -g 'Postgres Operator' pgo -USER 1000:1000 +USER pgo:pgo ENTRYPOINT ["/postgres-operator"] + diff --git a/docker/NotFromScrachDockerfile b/docker/NotFromScrachDockerfile new file mode 100644 index 000000000..9fc9c0798 --- /dev/null +++ b/docker/NotFromScrachDockerfile @@ -0,0 +1,30 @@ +########################################################### +### builder image ############################### +########################################################### +FROM golang:1.17.6-alpine3.15 AS builder + +RUN apk --no-cache add ca-certificates make git && update-ca-certificates +WORKDIR /go/src/github.com/zalando/postgres-operator/ +COPY . . +RUN make linux + +########################################################### +### operator image -> the version with full alpine image ## +########################################################### +## This is optional, only as example. Only last image is used +FROM alpine:3.15.0 as postgres-operator-alpine +MAINTAINER Team ACID @ Zalando +LABEL org.opencontainers.image.authors "Team ACID @ Zalando " +LABEL org.opencontainers.image.url "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.documentation "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.source "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.title "zalando/posgress-operator" +LABEL org.opencontainers.image.description "posgress-operator-alpine - full alpine image" +# We need root certificates to deal with teams api over https +RUN apk --no-cache add ca-certificates && update-ca-certificates +COPY --from=builder /go/src/github.com/zalando/postgres-operator/build/* / +RUN addgroup -g 1000 pgo +RUN adduser -D -u 1000 -G pgo -g 'Postgres Operator' pgo +USER pgo:pgo +ENTRYPOINT ["/postgres-operator"] + diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 000000000..0004313d1 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,32 @@ +# Developers +[developer docs](docs/developer.md) + +[build docs](../BUILD.md) + +# Build both code and image using Dockerfile +Most Dockerfiles both build the code as well as the image. + +# Choose the desired build flavour: +- WithBuildDockerfile -> builds code as well as more secure a version of the image (from scratch instead of basing on alpine) +- WithBuildDebugDockerfile -> builds a debug version of the image, based on alpine and uses "github.com/derekparker/delve/cmd/dlv". exposes port :7777 +- NotFromScrachDockerfile -> alpine based image (notFromScratch like in Dockerfile ) +- Dockerfile -> alpine based image, without the build, it expects binary is build outside. This is used by Makefile (& Travis) +- DebugDockerfile -> alpine based image, without the build, it expects binary is build outside. This is used by Makefile (& Travis) to make debug image. + +# Command example: +```shell +docker build -f docker/WithBuildDockerfile . +``` +OR, using proxy and eventually also internal go mirrors +```shell +docker build -f docker/WithBuildDockerfile . --build-arg HTTP_PROXY=http://proxy.corp.example.com:1234 --build-arg HTTPS_PROXY=http://proxy.corp.example.com:1234 --build-arg NO_PROXY=.corp.example.com --build-arg GOPROXY=http://golang-intranet-proxy.corp.example.com:8081/repository/goproxy/ --build-arg GOSUMDB=off +``` + +# docker.io +This solution works also when you want to build your fork using docker hub (docker.io) (and share/test your image directly from there). + +For automating docker build. Make sure you pass the context to root of it project. +Same for for docker hub (docker.io) builds: +set the **Dockerfile** column to `docker/WithBuildDockerfile` and **context** column to `/` + + diff --git a/docker/WithBuildDebugDockerfile b/docker/WithBuildDebugDockerfile new file mode 100644 index 000000000..681c7684d --- /dev/null +++ b/docker/WithBuildDebugDockerfile @@ -0,0 +1,45 @@ +########################################################### +### builder image ############################### +########################################################### +ARG GOPROXY=https://proxy.golang.org +#ARG GONOPROXY +#ARG GOPRIVATE +ARG GOSUMDB=sum.golang.org +#ARG GONOSUMDB +FROM golang:1.17.6-alpine3.15 AS builder +RUN apk --no-cache add ca-certificates make git && update-ca-certificates +WORKDIR /go/src/github.com/zalando/postgres-operator/ +COPY . . +RUN make linux + +########################################################### +### operator image -> debug version ################## +########################################################### +FROM golang:1.17.6-alpine3.15 AS postgres-operator-debug +MAINTAINER Team ACID @ Zalando +LABEL org.opencontainers.image.authors "Team ACID @ Zalando " +LABEL org.opencontainers.image.url "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.documentation "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.source "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.title "zalando/posgress-operator" +LABEL org.opencontainers.image.description "posgress-operator-debug" + +# We need root certificates to deal with teams api over https +COPY --from=builder /go/src/github.com/zalando/postgres-operator/build/* / + +# We need root certificates to deal with teams api over https +RUN apk --no-cache add ca-certificates git musl-dev + +RUN addgroup -g 1000 pgo +RUN adduser -D -u 1000 -G pgo -g 'Postgres Operator' pgo + +RUN go get github.com/derekparker/delve/cmd/dlv +RUN cp /go/bin/dlv /dlv +RUN chown -R pgo:pgo /dlv + +USER pgo:pgo +EXPOSE 7777 +RUN ls -l / + +CMD ["/dlv", "--listen=:7777", "--headless=true", "--api-version=2", "exec", "/postgres-operator"] + diff --git a/docker/WithBuildDockerfile b/docker/WithBuildDockerfile new file mode 100644 index 000000000..08ed59093 --- /dev/null +++ b/docker/WithBuildDockerfile @@ -0,0 +1,36 @@ +########################################################### +### builder image ############################### +########################################################### +ARG GOPROXY=https://proxy.golang.org +#ARG GONOPROXY +#ARG GOPRIVATE +ARG GOSUMDB=sum.golang.org +#ARG GONOSUMDB +FROM golang:1.17.6-alpine3.15 AS builder +RUN apk --no-cache add ca-certificates make git && update-ca-certificates +WORKDIR /go/src/github.com/zalando/postgres-operator/ +COPY . . +RUN make linux + +################################################################ +### operator image -> the more secure version (from scratch) ### +################################################################ +FROM scratch AS postgres-operator-secure +MAINTAINER Team ACID @ Zalando +LABEL org.opencontainers.image.authors "Team ACID @ Zalando " +LABEL org.opencontainers.image.url "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.documentation "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.source "https://github.com/zalando/postgres-operator/" +LABEL org.opencontainers.image.title "zalando/posgress-operator" +LABEL org.opencontainers.image.description "posgress-operator-secure from scratch image" + +COPY --from=builder /etc/passwd /etc/group /etc/ + +# We need root certificates to deal with teams api over https +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ +COPY --from=builder /go/src/github.com/zalando/postgres-operator/build/* / + +USER nobody +ENTRYPOINT ["/postgres-operator"] + + diff --git a/docs/developer.md b/docs/developer.md index 6e0fc33c8..42641ddbe 100644 --- a/docs/developer.md +++ b/docs/developer.md @@ -3,6 +3,13 @@ Read this guide if you want to debug the operator, fix bugs or contribute new features and tests. +## Simple build + +To simply build the code and create an image, follow the instructions at: +[docker based code and image build](../docker/README.md) + +For setting a full development environment, follow the steps below. + ## Setting up Go Postgres Operator is written in Go. Use the [installation instructions](https://golang.org/doc/install#install)