Skip to content

Commit

Permalink
Merge pull request #53 from refactor-group/pass_env_from_backend_for_…
Browse files Browse the repository at this point in the history
…container_build
  • Loading branch information
jhodapp authored Dec 16, 2024
2 parents 30bda35 + 468a74e commit 79646ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
17 changes: 11 additions & 6 deletions .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL="http"
NEXT_PUBLIC_BACKEND_SERVICE_PORT=4000
NEXT_PUBLIC_BACKEND_SERVICE_HOST="localhost"
NEXT_PUBLIC_BACKEND_API_VERSION="0.0.1"
# Use this file to set these environment variables when developing/running
# the application locally outside of a Docker container environment. This
# will have no effect on the values passed into the containers as those are
# all set in the backend's .env file.

FRONTEND_SERVICE_PORT=3000
FRONTEND_SERVICE_INTERFACE=0.0.0.0
# NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL="http"
# NEXT_PUBLIC_BACKEND_SERVICE_PORT=4000
# NEXT_PUBLIC_BACKEND_SERVICE_HOST="localhost"
# NEXT_PUBLIC_BACKEND_API_VERSION="0.0.1"

# FRONTEND_SERVICE_INTERFACE=0.0.0.0
# FRONTEND_SERVICE_PORT=3000
31 changes: 25 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,28 @@ FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# This will do the trick, use the corresponding env file for each environment.
COPY .env .env

# Receive the build args from docker-compose.yaml
ARG NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL
ARG NEXT_PUBLIC_BACKEND_SERVICE_HOST
ARG NEXT_PUBLIC_BACKEND_SERVICE_PORT
ARG NEXT_PUBLIC_BACKEND_API_VERSION
ARG FRONTEND_SERVICE_INTERFACE
ARG FRONTEND_SERVICE_PORT

# And convert them to environment variables for runtime
ENV NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL=$NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL
ENV NEXT_PUBLIC_BACKEND_SERVICE_HOST=$NEXT_PUBLIC_BACKEND_SERVICE_HOST
ENV NEXT_PUBLIC_BACKEND_SERVICE_PORT=$NEXT_PUBLIC_BACKEND_SERVICE_PORT
ENV NEXT_PUBLIC_BACKEND_API_VERSION=$NEXT_PUBLIC_BACKEND_API_VERSION

RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL: ${NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL}"
RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${NEXT_PUBLIC_BACKEND_SERVICE_HOST}"
RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${NEXT_PUBLIC_BACKEND_SERVICE_PORT}"
RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${NEXT_PUBLIC_BACKEND_API_VERSION}"
RUN echo "FRONTEND_SERVICE_INTERFACE: ${FRONTEND_SERVICE_INTERFACE}}"
RUN echo "FRONTEND_SERVICE_PORT: ${FRONTEND_SERVICE_PORT}}"

RUN npm run build

# 3. Production image, copy all the files and run next
Expand All @@ -44,11 +64,10 @@ COPY --from=builder /app/package.json ./
USER nextjs

# Expose the port Next.js runs on
EXPOSE 3000
EXPOSE $FRONTEND_SERVICE_PORT

ENV PORT=3000
ENV HOSTNAME=0.0.0.0
ENV HOSTNAME=$FRONTEND_SERVICE_INTERFACE
ENV PORT=$FRONTEND_SERVICE_PORT

# Run the app using JSON array notation
CMD ["node", "server.js"]
#CMD HOSTNAME="0.0.0.0" node server.js

0 comments on commit 79646ad

Please sign in to comment.