Skip to content

Commit 52b8a29

Browse files
authored
Fix ENV variables are not being set inside docker (#332)
* Fix ENV variables are not being set inside docker closes Visual-Regression-Tracker/Visual-Regression-Tracker#414 * Update Dockerfile
1 parent b7baea7 commit 52b8a29

File tree

5 files changed

+28
-52
lines changed

5 files changed

+28
-52
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
REACT_APP_API_URL=http://localhost:4200
22
PORT=8080
3-
VRT_VERSION=4.20.0
3+
VRT_VERSION=5.0.0

Dockerfile

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ FROM node:18-alpine3.18 AS builder
66
WORKDIR /app
77

88
# Copy all files from the repo to /app
9-
COPY . .
9+
COPY /public ./public
10+
COPY /src ./src
11+
COPY index.html .
12+
COPY package*.json .
13+
COPY tsconfig.json .
14+
COPY vite.config.ts .
1015

1116
# Install app dependencies
1217
RUN npm ci --verbose
1318

1419
# Build the ui
1520
RUN npm run build
1621

17-
# Create environment variable js file
18-
RUN node generate-env-browser.js
19-
2022
### STAGE 2: Run ###
2123
# This image is around 5 megabytes
2224
FROM nginx:1.25-alpine3.17-slim
@@ -26,7 +28,17 @@ COPY /nginx /etc/nginx/conf.d
2628
EXPOSE 8080
2729
EXPOSE 443
2830

29-
COPY --from=builder /app/build /usr/share/nginx/html
30-
COPY --from=builder /app/env-config.js /usr/share/nginx/html/
31+
WORKDIR /usr/share/nginx/html
32+
33+
# Copy built source
34+
COPY --from=builder /app/build .
35+
36+
# Copy override ENV script with default values
37+
COPY ./env.sh .
38+
COPY .env .
39+
40+
# Make our shell script executable
41+
RUN chmod +x env.sh
3142

32-
# Nginx server will now start automatically.
43+
# Start Nginx server with override ENV variables script
44+
CMD ["/bin/sh", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]

env.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
# (c) https://github.com/kunokdev/cra-runtime-environment-variables/blob/master/env.sh
3+
# line endings must be \n, not \r\n !
4+
5+
echo "window._env_ = {" > ./env-config.js
6+
awk -F '=' '{ print $1 ": \"" (ENVIRON[$1] ? ENVIRON[$1] : $2) "\"," }' ./.env >> ./env-config.js
7+
echo "}" >> ./env-config.js

generate-env-browser.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"uuid": "^9.0.0"
3939
},
4040
"scripts": {
41-
"start": "node generate-env-browser.js && cp env-config.js ./public/ && vite",
41+
"start": "chmod +x ./env.sh && sh ./env.sh && cp env-config.js ./public/ && vite",
4242
"test": "jest",
4343
"format": "prettier --write integration_tests src *.js *.ts *.md",
4444
"build": "vite build",

0 commit comments

Comments
 (0)