Skip to content

Instantly share code, notes, and snippets.

@gaieges
Created March 10, 2020 23:44
Show Gist options
  • Save gaieges/c21880c8327a09944b634f83d8da3adb to your computer and use it in GitHub Desktop.
Save gaieges/c21880c8327a09944b634f83d8da3adb to your computer and use it in GitHub Desktop.
Using docker env vars to set config file in image at run time
# this file is intended to be used in the local build environment
# to get started, run:
#
# docker-compose up
#
# REQUIRES docker-compose 1.16+
version: '2.3'
services:
signup:
build:
context: .
target: build
ports:
- '9000:9000'
environment:
- FUN_VAR_HERE
#!/bin/sh
set -xe
echo "window.CONFIG=`jq -n env`" > /dist/config.js
exec "$@"
##############
## BUILD IMAGE
##############
FROM node:8 AS build
LABEL builder=true
RUN apt-get update \
&& apt-get install -y gnupg nginx curl \
&& curl https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 -Lo /usr/bin/jq \
&& chmod +x /usr/bin/jq \
&& rm -rf /var/lib/apt/lists
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY . /client
WORKDIR /client
RUN yarn && npm run build
EXPOSE 9000
ENTRYPOINT ["/client/entrypoint.sh"]
CMD ["sh", "-c", "(npm run dev &) && nginx -g 'daemon off;'"]
##############
## DIST IMAGE
##############
FROM alpine AS dist
COPY --from=build /dist /dist
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY entrypoint.sh /entrypoint.sh
RUN apk update \
&& apk add jq nginx \
&& mkdir -p /run/nginx \
&& ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
&& rm -rf /var/lib/apt/lists
EXPOSE 9000
ENTRYPOINT ["/entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment