How should I configure Dockerfile for golang proj with a CA.pem file? Inside the container CA,pem can't be found
I have a server project. Handlers provides some data from a postgres DB. For connect I have a CA.pem. When I run main.go locally - it's working. CA.pem is in the proj folder. It's invoked like this:
pem, err := ioutil.ReadFile("CA.pem")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read the CA file:%s - %s", el, err)
return err
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
fmt.Fprint(os.Stderr, "Failed to append PEM")
}
Proj structure:
├──project/
├──main.go
├──Dockerfile
├──CA.pem
├──go.mod
├──...
├──folder1/
| ├─...
└──folder2/
└──...
Dockerfile looks like:
# syntax=docker/dockerfile:1
##
## Build
##
FROM golang:1.17 AS build
WORKDIR /app/proj
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o ./out/proj .
##
## Deploy
##
FROM gcr.io/distroless/base-debian10
WORKDIR /
COPY --from=build /app/proj/out/proj /app/proj
EXPOSE 8080
ENTRYPOINT ["/app/proj"]
So when I'm trying to run the container I receive this err:
Failed to read the CA file:spaceship - open CA.pem: no such file or directory Error during connection to databaseinteractions: open CA.pem: no such file or directory
*spaceship - db name *databaseinteractions - folder where I have the "connect_to_db.go" file.
The server is running. I've specially created a "status" handler, which provide me 201 response. It's ok. But the DB connection can't be established. It doesn't see the CA.pem file. But why? May be I should create the docker image differently? Or read the file differently?
Thank you for any help!
When you run locally Program finds CA.pem
file. But in docker you are keeping only executable build of the project in debian image.
You need to copy the CA.pem file in the project directory in order to run it successfully.
COPY --from=build /app/proj/CA.pem /app/proj