What I have, are multi similar and simple dockerfiles
But what I want is to have a single base dockerfile and my dockerfiles
pass their variables into it.
In my case the only difference between dockerfiles are
simply their EXPOSE, so I think it's better to keep a base dockerfile and other dockerfiles only inject that variables into base dockerfile like a template engine
A sample dockerfile:
FROM golang:1.17 AS builder
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o /bin/app ./cmd/root.go
FROM alpine:latest
WORKDIR /bin/
COPY --from=builder /bin/app .
EXPOSE 8080
LABEL org.opencontainers.image.source="https://github.com/mohammadne/bookman-auth"
ENTRYPOINT ["/bin/app"]
CMD ["server", "--env=dev"]
IMPORT directive will never be implementedA long time ago there was proposed IMPORT directive for Docker
Unfortunately, issues are closed while PR's are still open:
But for your case, all you need - is just a bit of sed
E.g.:
# Case1: inplace templating
EXPOSED_PORT=8081 sed -i "s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/" Dockerfile
# Case2: generating Dockerfile from template
sed "s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/" Dockerfile.template > Dockerfile
Explanation:
EXPOSED_PORT=8081 declares local bash variablesed is a tool for text manipulationsed -i "s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/" Dockerfile replaces EXPOSE 8080 to EXPOSE 8081sed "s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/" Dockerfile.template > Dockerfile generates the new Dockerfile from Dockerfile.templateIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With