Skip to content

Commit

Permalink
[ENH] Move to Docker multistage builds (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmanaem authored Jan 29, 2025
1 parent c66da0c commit 4a7f8cc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
26 changes: 23 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
FROM node:20
FROM node:20-alpine AS build

WORKDIR /app

COPY . .

ENV NB_API_QUERY_URL="REPLACE_ME_NB_API_QUERY_URL_REPLACE_ME"
ENV NB_ENABLE_AUTH="REPLACE_ME_NB_ENABLE_AUTH_REPLACE_ME"

Check warning on line 8 in Dockerfile

View workflow job for this annotation

GitHub Actions / build

Sensitive data should not be used in the ARG or ENV commands

SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "NB_ENABLE_AUTH") More info: https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/

Check warning on line 8 in Dockerfile

View workflow job for this annotation

GitHub Actions / build

Sensitive data should not be used in the ARG or ENV commands

SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "NB_ENABLE_AUTH") More info: https://docs.docker.com/go/dockerfile/rule/secrets-used-in-arg-or-env/
ENV NB_QUERY_CLIENT_ID="REPLACE_ME_NB_QUERY_CLIENT_ID_REPLACE_ME"
ENV NB_ENABLE_CHATBOT="REPLACE_ME_NB_ENABLE_CHATBOT_REPLACE_ME"
ENV NB_QUERY_APP_BASE_PATH="REPLACE_ME_NB_QUERY_APP_BASE_PATH_REPLACE_ME"

RUN npm ci

EXPOSE 5173
RUN npm run build


FROM nginx:alpine

COPY --from=build /app/dist /usr/share/nginx/html

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

WORKDIR /usr/share/nginx/html

EXPOSE 80

ENTRYPOINT [ "/entrypoint.sh" ]

ENTRYPOINT npm run build && npm run preview
CMD ["nginx", "-g", "daemon off;"]
27 changes: 27 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

# This script is used to replace environment variables in the JS files
# with the values provided in the environment variables. If no value is
# provided, the default value is used.
#
# To add a new environment variable, add a new sed command to replace
# the placeholder in the JS files with the value of the environment
# variable and define a default value

# Check that the required environment variables are set
if [ -z "$NB_API_QUERY_URL" ]; then
echo "NB_API_QUERY_URL is not set. Exiting."
exit 1
fi

# Find all .js and .html files and loop through them
for file in $(find /usr/share/nginx/html -type f \( -name "*.js" -o -name "*.html" \)); do
sed -i "s|REPLACE_ME_NB_API_QUERY_URL_REPLACE_ME|${NB_API_QUERY_URL}|g" "$file"
sed -i "s|REPLACE_ME_NB_ENABLE_AUTH_REPLACE_ME|${NB_ENABLE_AUTH:-false}|g" "$file"
sed -i "s|REPLACE_ME_NB_QUERY_CLIENT_ID_REPLACE_ME|${NB_QUERY_CLIENT_ID:-testclient1234}|g" "$file"
sed -i "s|REPLACE_ME_NB_ENABLE_CHATBOT_REPLACE_ME|${NB_ENABLE_CHATBOT:-false}|g" "$file"
sed -i "s|REPLACE_ME_NB_QUERY_APP_BASE_PATH_REPLACE_ME|${NB_QUERY_APP_BASE_PATH:-/}|g" "$file"
sed -i "s|<!-- REPLACE_ME_NB_QUERY_HEADER_SCRIPT_REPLACE_ME -->|${NB_QUERY_HEADER_SCRIPT}|g" "$file"
done

exec "$@"
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<!-- Additional script tags -->
<!-- REPLACE_ME_NB_QUERY_HEADER_SCRIPT_REPLACE_ME -->
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-query-tool",
"private": true,
"version": "v0.8.1",
"version": "v0.8.2",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down

0 comments on commit 4a7f8cc

Please sign in to comment.