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

Mieubrisse/docker image with dynamic envvars #2191

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions packages/frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
**/.git
**/node_modules
.gitignore
dist
.cache

# Docker will read these during run anyways, so no need to invalidate the cache if we change these
Dockerfile
.dockerignore
28 changes: 19 additions & 9 deletions packages/frontend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

FROM phusion/baseimage:0.11

ENTRYPOINT ["/sbin/my_init", "--"]

RUN curl -o /tmp/node_setup.sh "https://deb.nodesource.com/setup_11.x"
RUN bash /tmp/node_setup.sh
RUN curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
Expand All @@ -13,16 +11,28 @@ RUN apt-get update -qq && apt-get install -y \
nginx \
rsync

# near-wallet
# Set up
RUN mkdir /near-wallet
COPY . /near-wallet/
WORKDIR /near-wallet
# We'll replace the sites-enabled down below
RUN rm /etc/nginx/sites-enabled/default

# 'npm install' takes ~3 minutes, so copy just the package.json and do an install before we copy the rest of the code
# so that Docker will cache these instructions so long as package.json isn't changed
COPY package.json .
RUN npm install
RUN npm run build
RUN mkdir -p /var/www/html/wallet
RUN rsync -ar /near-wallet/dist/ /var/www/html/wallet

# nginx
RUN rm /etc/nginx/sites-enabled/default
# Now, copy the rest of the code for the build
COPY . .
RUN mkdir -p /var/www/html/wallet
COPY /scripts/wallet.nginx /etc/nginx/sites-enabled/wallet
COPY /scripts/init_nginx.sh /etc/my_init.d/

# NOTE1: Because the Wallet uses Parcel for its build and environment variables in Parcel can only be set at build time, we run an 'npm build'
# as part of container startup so that the Wallet will respond to environment variables set on container startup
# NOTE2: The last command, /sbin/my_init, is the command to run the NginX server bundled inside the phusion base image
# See https://hub.docker.com/layers/phusion/baseimage/0.11/images/sha256-14e6c478b00aa0a6f68391a53f7d510d9a062efcf509ea55ee9a125969592b79?context=explore for more details
CMD bash -c "./scripts/generate_env_file_for_build_inside_docker.sh && \
npm run build && \
rsync -ar /near-wallet/dist/ /var/www/html/wallet && \
/sbin/my_init"
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# The wallet app's environment variables can only be configured at build time due to Parcel being used to build the wallet, which means that passing
# environment variables to a Docker container running a wallet wouldn't do anything.
# Parcel *does* store environment variables in a '.env' file in the root of the project though, so when we launch a Wallet Docker image we a) write a .env
# file with the environment variables we want the wallet to have and b) rebuild the Wallet as part of the Docker container startup
# This script does the first part - taking environment variables set in the shell environment and writing them to a .env file for Parcel

set -euo pipefail # Bash "strict mode"
script_dirpath="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
project_root_dirpath="$(dirname "${script_dirpath}")"


# ==================================================================================================
# Constants
# ==================================================================================================
# Parcel, which is used to build the project, will read this file at build time and 'process.env.SOMEVAR' calls
# in the Javascript code will use the variables set in this file
OUTPUT_FILENAME=".env"

# Environment variables that will be set in the shell environment when the container is started, and which should be propagated
# to the .env file for Parcel to use
ENVVARS_TO_STORE=(
"REACT_APP_ACCOUNT_HELPER_URL"
"EXPLORER_URL"
"REACT_APP_NODE_URL"
"REACT_APP_IS_MAINNET"
"REACT_APP_NETWORK_ID"
"REACT_APP_ACCOUNT_ID_SUFFIX"
"REACT_APP_ACCESS_KEY_FUNDING_AMOUNT"
)

# ==================================================================================================
# Main Logic
# ==================================================================================================
output_abs_filepath="${project_root_dirpath}/${OUTPUT_FILENAME}"

for envvar in "${ENVVARS_TO_STORE[@]}"; do
value="${!envvar:-}"
if [ -z "${value}" ]; then
# Safety guard to make sure someone running from the Docker image sets all the expected envvars
echo "Error: Expected environment variable '${envvar}' to have a value, but none was passed in" >&2
exit 1
fi
echo "${envvar}=\"${value}\"" >> "${output_abs_filepath}"
done