Reputation: 99
I need to read in my application from a file "cert.pem" whose path is a) provided as an argument or b) it is retrieved from the directory of the main application file main.py.
I have created the following dockerfile, but after building and running my application using the image the "cert.pem" file cannot be accessed. Is there a way to read from this file?
FROM golang:1.17-alpine AS builder
RUN apk add --no-cache git
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o ./bin .
#final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /app/bin /app/bin
EXPOSE 3000
CMD ["/app/bin"]
The instruction to load the file is the following:
....
var (
cert_file = flag.String("cert", "./cert.pem", "File name of x509 certificate")
)
...
func main() {
...
_, err := ioutil.ReadFile(*cert_file)
...
Upvotes: 1
Views: 358
Reputation: 99
The solution is to define an ENTRYPOINT instead CMD and add a WORKDIR instruction:
#build stage
FROM golang:1.17-alpine AS builder
RUN apk add --no-cache git
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o ./bin .
#final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /app/bin /app/bin
COPY --from=builder /app/cert.pem /app/cert.pem
EXPOSE 3000
WORKDIR /app
ENTRYPOINT ["/app/bin"]
Upvotes: 1
Reputation: 155
you just have to copy your cert.pem file to the location of the binary in the final stage in docker. Assuming you have the cert file in your docker build context, you run a copy command.
FROM golang:1.17-alpine AS builder
RUN apk add --no-cache git
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o ./bin .
#final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /app/bin /app/bin
COPY cert.pem /app/
EXPOSE 3000
CMD ["/app/bin"]
Make sure you set the cert.pem location in your go program as ./cert.pem
Upvotes: 1