Skip to content

Commit 468a74e

Browse files
committed
All environment variables for the dockerized application are set and passed from the backend/docker-compose.yaml
1 parent 30bda35 commit 468a74e

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

.env

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL="http"
2-
NEXT_PUBLIC_BACKEND_SERVICE_PORT=4000
3-
NEXT_PUBLIC_BACKEND_SERVICE_HOST="localhost"
4-
NEXT_PUBLIC_BACKEND_API_VERSION="0.0.1"
1+
# Use this file to set these environment variables when developing/running
2+
# the application locally outside of a Docker container environment. This
3+
# will have no effect on the values passed into the containers as those are
4+
# all set in the backend's .env file.
55

6-
FRONTEND_SERVICE_PORT=3000
7-
FRONTEND_SERVICE_INTERFACE=0.0.0.0
6+
# NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL="http"
7+
# NEXT_PUBLIC_BACKEND_SERVICE_PORT=4000
8+
# NEXT_PUBLIC_BACKEND_SERVICE_HOST="localhost"
9+
# NEXT_PUBLIC_BACKEND_API_VERSION="0.0.1"
10+
11+
# FRONTEND_SERVICE_INTERFACE=0.0.0.0
12+
# FRONTEND_SERVICE_PORT=3000

Dockerfile

+25-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,28 @@ FROM base AS builder
1919
WORKDIR /app
2020
COPY --from=deps /app/node_modules ./node_modules
2121
COPY . .
22-
# This will do the trick, use the corresponding env file for each environment.
23-
COPY .env .env
22+
23+
# Receive the build args from docker-compose.yaml
24+
ARG NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL
25+
ARG NEXT_PUBLIC_BACKEND_SERVICE_HOST
26+
ARG NEXT_PUBLIC_BACKEND_SERVICE_PORT
27+
ARG NEXT_PUBLIC_BACKEND_API_VERSION
28+
ARG FRONTEND_SERVICE_INTERFACE
29+
ARG FRONTEND_SERVICE_PORT
30+
31+
# And convert them to environment variables for runtime
32+
ENV NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL=$NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL
33+
ENV NEXT_PUBLIC_BACKEND_SERVICE_HOST=$NEXT_PUBLIC_BACKEND_SERVICE_HOST
34+
ENV NEXT_PUBLIC_BACKEND_SERVICE_PORT=$NEXT_PUBLIC_BACKEND_SERVICE_PORT
35+
ENV NEXT_PUBLIC_BACKEND_API_VERSION=$NEXT_PUBLIC_BACKEND_API_VERSION
36+
37+
RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL: ${NEXT_PUBLIC_BACKEND_SERVICE_PROTOCOL}"
38+
RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${NEXT_PUBLIC_BACKEND_SERVICE_HOST}"
39+
RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${NEXT_PUBLIC_BACKEND_SERVICE_PORT}"
40+
RUN echo "NEXT_PUBLIC_BACKEND_SERVICE_HOST: ${NEXT_PUBLIC_BACKEND_API_VERSION}"
41+
RUN echo "FRONTEND_SERVICE_INTERFACE: ${FRONTEND_SERVICE_INTERFACE}}"
42+
RUN echo "FRONTEND_SERVICE_PORT: ${FRONTEND_SERVICE_PORT}}"
43+
2444
RUN npm run build
2545

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

4666
# Expose the port Next.js runs on
47-
EXPOSE 3000
67+
EXPOSE $FRONTEND_SERVICE_PORT
4868

49-
ENV PORT=3000
50-
ENV HOSTNAME=0.0.0.0
69+
ENV HOSTNAME=$FRONTEND_SERVICE_INTERFACE
70+
ENV PORT=$FRONTEND_SERVICE_PORT
5171

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

0 commit comments

Comments
 (0)