Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes local container #616

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions frontend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
build:
docker build $(ARGS) -t react:latest -f docker/production/react/Dockerfile .

.PHONY: build-local
build-local:
docker build $(ARGS) -t react:latest -f docker/local/Dockerfile .

.PHONY: run
run:
docker run $(ARGS) -it --rm -p 3000:3000 react:latest

.PHONY: run-shell
run-shell:
docker run $(ARGS) -it --rm --entrypoint /bin/sh react:latest
.PHONY: shell
shell:
docker run $(ARGS) -it --rm -p 3000:3000 --entrypoint /bin/sh react:latest
5 changes: 5 additions & 0 deletions frontend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ services:
dockerfile: ./docker/local/Dockerfile
image: metagrid_local_react
container_name: react
environment:
- DEBUG=true
- RELEASE=dev
- HTML_PATH=/app/public
- ENV_FILE=/app/.envs/.react
env_file:
- .envs/.react
volumes:
Expand Down
11 changes: 11 additions & 0 deletions frontend/docker/local/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ RUN yarn install
# Add app
COPY . ./

# Determines which conf to use if app is/is not being served through subdirectory
COPY ./docker/production/react/entrypoint /entrypoint
# gettext-base is required for envsubst
RUN sed -i 's/\r$//g' /entrypoint && \
chmod +x /entrypoint && \
apt-get update && \
apt-get install -y gettext-base && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENTRYPOINT ["/entrypoint"]

# Start app
CMD ["yarn", "start:local"]
63 changes: 37 additions & 26 deletions frontend/docker/production/react/entrypoint
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
#!/bin/sh

[[ -n "${DEBUG}" ]] && set -x
[ -n "${DEBUG}" ] && set -x

runtime_path="/usr/share/nginx/html/static/js/runtime_env.js"
export RELEASE="${RELEASE:-production}"
export ENV_FILE="${ENV_FILE:-/env}"
export HTML_PATH="${HTML_PATH:-/usr/share/nginx/html}"
export STATIC_PATH="${STATIC_PATH:-/static/js}"
export STATIC_URL="${PUBLIC_URL}${STATIC_PATH}"
export RUNTIME_PATH="${HTML_PATH}${STATIC_PATH}/runtime_env.js"

rm -rf "${runtime_path}" && touch "${runtime_path}"
if [ ! -e "$(dirname ${RUNTIME_PATH})" ]
then
mkdir -p "$(dirname ${RUNTIME_PATH})"
fi

echo "window.ENV = {" >> "${runtime_path}"
if [ -e "${ENV_FILE}" ]; then
echo "window.ENV = {" > "${RUNTIME_PATH}"

while read -r line; do
[[ -z "${line}" || "${line}" =~ ^# ]] && continue
while read -r line; do
[ -z "$(echo ${line} | grep -vE '^# |^$')" ] && continue

varname=$(printf '%s\n' "${line}" | cut -d"=" -f1)
varvalue=$(printenv "${varname}")
varname=$(printf '%s\n' "${line}" | cut -d"=" -f1)
varvalue=$(printenv "${varname}")

if [[ -z "${varvalue}" ]]; then
varvalue=$(printf '%s\n' "${line}" | cut -d"=" -f2)
if [ -z "${varvalue}" ]; then
varvalue=$(printf '%s\n' "${line}" | cut -d"=" -f2)

export "${varname}"="${varvalue}"
fi
export "${varname}"="${varvalue}"
fi

echo " ${varname}: \"${varvalue}\"," >> "${runtime_path}"
done < ${ENV_FILE:-/env}
echo " ${varname}: \"${varvalue}\"," >> "${RUNTIME_PATH}"
done < ${ENV_FILE}

echo "};" >> "${runtime_path}"
echo "};" >> "${RUNTIME_PATH}"
fi

if [[ -z "${PUBLIC_URL}" ]]
if [ "${RELEASE}" = "production" ]
then
envsubst '${PREVIOUS_URL}' < /nginx.conf > /etc/nginx/conf.d/default.conf
export PUBLIC_URL=""
else
envsubst '${PREVIOUS_URL},${PUBLIC_URL}' < /nginx.subdir.conf > /etc/nginx/conf.d/default.conf
if [ -z "${PUBLIC_URL}" ]
then
envsubst '${PREVIOUS_URL}' < /nginx.conf > /etc/nginx/conf.d/default.conf
export PUBLIC_URL=""
else
envsubst '${PREVIOUS_URL},${PUBLIC_URL}' < /nginx.subdir.conf > /etc/nginx/conf.d/default.conf
fi
fi

# prepend any "/static/... with "${PUBLIC_URL}/static/...
sed -i"" "s/\"\/static/\"\${PUBLIC_URL}\/static/g" /usr/share/nginx/html/index.html

# move original index.html, envsubst cannot modify in place
mv /usr/share/nginx/html/index.html /index.html
if [ ! -e "/index.html" ]; then
cp "${HTML_PATH}/index.html" /index.html
fi

envsubst '${PUBLIC_URL},${$REACT_APP_GOOGLE_ANALYTICS_TRACKING_ID}' < /index.html > /usr/share/nginx/html/index.html
envsubst '$STATIC_URL,$PUBLIC_URL,$REACT_APP_GOOGLE_ANALYTICS_TRACKING_ID' < /index.html > "${HTML_PATH}/index.html"

exec "$@"
2 changes: 1 addition & 1 deletion frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
-->
<title>ESGF MetaGrid</title>

<script type="application/javascript" src="$PUBLIC_URL/static/js/runtime_env.js"></script>
<script type="application/javascript" src="$STATIC_URL/runtime_env.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=$REACT_APP_GOOGLE_ANALYTICS_TRACKING_ID"></script>
<script>
Expand Down
Loading