Dockerfile을 빌드시에 --build-arg를 이용하여 ARG를 넘길 수 있다.
docker build --build-arg APP_NAME=notify-server -t notify-server .
넘겨진 ARG는 ARG APP_NAME으로 선언하여 받아오고 ${APP_NAME}으로 사용한다.
# ------------------------------------------
# 1. Build Image
# ------------------------------------------
FROM golang:1.20.5-bullseye AS builder
ENV GO111MODULE=on
ARG APP_NAME
WORKDIR /go/src/app
COPY . .
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -a -ldflags="-s -w" -o ${APP_NAME} .
# ------------------------------------------
# 2. Production Image
# ------------------------------------------
FROM debian:bullseye
ARG APP_NAME
COPY docker-entrypoint.sh /docker-entrypoint/
RUN chmod +x /docker-entrypoint/docker-entrypoint.sh
WORKDIR /docker-entrypoint/dist
COPY --from=builder /go/src/app/${APP_NAME} .
RUN ln -s /usr/local/app/${APP_NAME} /docker-entrypoint/dist/appctl
WORKDIR /usr/local/app
ENTRYPOINT ["/docker-entrypoint/docker-entrypoint.sh"]
CMD ["/docker-entrypoint/dist/appctl"]