Skip to content

Commit 997a433

Browse files
committed
Use ENTRYPOINT for Nginx runtime substitution
1 parent ce82600 commit 997a433

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

Dockerfile.production

+10-7
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
142142
# nginx stage to serve static assets and proxy requests to api container
143143
FROM nginx:$NGINX_VERSION AS web
144144

145-
ENV INSTALL_PATH=/usr/src/app
145+
ENV INSTALL_PATH=/usr/src/app \
146+
API_SERVICE_HOST=api \
147+
API_SERVICE_PORT=3000
146148

147149
# Create non-root user
148150
RUN addgroup -g 3000 -S dmpgroup && \
@@ -163,13 +165,14 @@ COPY nginx.conf /etc/nginx/nginx.conf
163165
RUN chown dmpuser:dmpgroup /etc/nginx/nginx.conf && \
164166
chmod 664 /etc/nginx/nginx.conf
165167

168+
# Set up an entrypoint script to replace variables dynamically
169+
COPY nginx-entrypoint.sh /usr/local/bin/entrypoint.sh
170+
RUN chmod +x /usr/local/bin/entrypoint.sh
171+
166172
USER dmpuser
167173

168174
EXPOSE 80
169175

170-
# Replace environment variables in nginx.conf and overwrite the file
171-
CMD envsubst '${API_URL}' < /etc/nginx/nginx.conf | tee /etc/nginx/nginx.conf > /dev/null \
172-
# Set to read-only after substitution
173-
&& chmod 644 /etc/nginx/nginx.conf \
174-
# Start Nginx in the foreground to keep the container running
175-
&& exec nginx -g 'daemon off;'
176+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
177+
178+
CMD ["nginx", "-g", "daemon off;"]

nginx-entrypoint.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# Replace environment variables in nginx.conf and overwrite the file
4+
envsubst '${API_SERVICE_HOST} ${API_SERVICE_PORT}' < /etc/nginx/nginx.conf | tee /etc/nginx/nginx.conf > /dev/null
5+
6+
# Set to read-only after substitution
7+
chmod 644 /etc/nginx/nginx.conf
8+
9+
# Execute the main process
10+
exec "$@"

nginx.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ http {
6767
}
6868
# Proxy configuration for API
6969
location / {
70-
proxy_pass ${API_URL};
70+
proxy_pass http://${API_SERVICE_HOST}:${API_SERVICE_PORT};
7171

7272
# Standard proxy headers
7373
proxy_set_header Host $host;

0 commit comments

Comments
 (0)