ARG CADDY_VERSION=2.6
ARG BUILD_ENV=prod

FROM node:20.19.0 as base
WORKDIR /app
COPY . .
RUN npm install -g pnpm
RUN pnpm install
RUN pnpm build


# The base for mode ENVIRONMENT=prod
FROM caddy:${CADDY_VERSION}-alpine as prod

# Workaround for https://github.com/alpinelinux/docker-alpine/issues/98#issuecomment-679278499
RUN sed -i 's/https/http/' /etc/apk/repositories \
 && apk add --no-cache bash

COPY docker/Caddyfile /etc/caddy/
COPY --from=base /app/dist /frontend

# Run stage
FROM ${BUILD_ENV}

EXPOSE 80 443
VOLUME ["/data", "/etc/caddy"]
CMD ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
