I am using mkcert to generate a self-signed certificate and authority. When I use these files locally with ListenAndServeTLS, I can successfully connect with cURL. My host O/S is MacOS.
However, when trying to run this Go code in a docker container, I get the following error:
x509: certificate signed by unknown authority
Many other posts suggest that the problem is ca-certificates is not installed and that one should run: apk add ca-certificates. I have done this, and I still have the problem.
Generate certificates
mkcert -cert-file ./cert.pem -key-file ./key.pem localhost
This means the cert will be valid for the domain "localhost", accessible at https://localhost.
Load the cert and authority in Go
// Load cert + key.
cert, _ := tls.LoadX509KeyPair("cert.pem", "key.pem")
// Load our CA. (Mkcert also generates this btw, check the docs).
caCert, _ := ioutil.ReadFile("rootCA.pem")
// Add our CA so it's considered an acceptable rootCA.
rootCAs, _ := x509.SystemCertPool()
rootCAs.AppendCertsFromPEM(caCert)
server := http.Server{
Addr: ":1234",
Handler: router,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: rootCAs,
},
}
_ = server.ListenAndServeTLS("", "")
This runs fine and works locally.
Put it in a docker container
ARG GO_VERSION=1.14
FROM golang:${GO_VERSION}-alpine AS builder
RUN apk add --no-cache ca-certificates git curl
ENV CGO_ENABLED=0
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . /app
RUN go build -o ./bin/app .
FROM alpine AS final
WORKDIR /app
COPY --from=builder /user/group /user/passwd /etc/
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /app/bin/app /app
ENTRYPOINT ["./app"]
docker-compose file
my-app:
build:
context: ./
dockerfile: Dockerfile
ports:
- 1234:1234
When running the above container and connecting over TLS, I get the unknown authority error described above.
What am I missing?