-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(docker): Improve Docker image build (#37)
Changes: - Use dockerize for templating environment variables in build assets - Upgrade nodejs base image to 10 - Create docker-compose.yml - Improve DX by optimizing build layers (faster local builds on changes) - Use docker cache on travis (generally faster builds)
- Loading branch information
Showing
5 changed files
with
51 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
deploy/ | ||
node_modules/ | ||
tests/ | ||
build/ | ||
* | ||
!/src | ||
!/public | ||
!package.json | ||
!yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,38 @@ | ||
FROM node:9-alpine | ||
FROM node:10-alpine | ||
|
||
ARG PUBLIC_URL=/admin | ||
ARG REACT_APP_API_URL=http://api.knit.pk.edu.pl | ||
ENV DOCKERIZE_VERSION v0.6.1 | ||
RUN wget https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/dockerize-alpine-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \ | ||
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-${DOCKERIZE_VERSION}.tar.gz \ | ||
&& rm dockerize-alpine-linux-amd64-${DOCKERIZE_VERSION}.tar.gz | ||
|
||
ENV NODE_ENV production | ||
ENV PUBLIC_URL ${PUBLIC_URL} | ||
ENV REACT_APP_API_URL ${REACT_APP_API_URL} | ||
ENV NODE_ENV='production' \ | ||
PUBLIC_URL='{{{ .Env.PUBLIC_URL }}}' \ | ||
REACT_APP_API_URL='{{{ .Env.REACT_APP_API_URL }}}' | ||
|
||
WORKDIR /usr/src/app | ||
COPY package.json yarn.lock ./ | ||
RUN yarn install | ||
|
||
COPY . /usr/src/app | ||
|
||
RUN yarn install | ||
RUN yarn build | ||
# Build and create template files for dockerize | ||
RUN yarn build && \ | ||
ASSETS_VERSION=$(cat build/asset-manifest.json | grep "\"main.js\"" | sed -E "s/\s+\"main.js\": \"static\/js\/main\.(.*)\.js\",/\1/") && \ | ||
cp build/index.html build/index.html.tmpl && \ | ||
cp build/service-worker.js build/service-worker.js.tmpl && \ | ||
cp build/static/js/main.$ASSETS_VERSION.js build/static/js/main.$ASSETS_VERSION.js.tmpl && \ | ||
cp build/static/js/main.$ASSETS_VERSION.js.map build/static/js/main.$ASSETS_VERSION.js.map.tmpl | ||
|
||
# Set environment variables in build files provided via --build-args using dockerize | ||
# Remarks: You can also use bellow code in your derrived Dockerfile to change app's environment variables | ||
ARG ADMIN_PUBLIC_URL='/admin' | ||
ARG ADMIN_API_URL='http://api.knit.pk.edu.pl' | ||
ENV PUBLIC_URL=${ADMIN_PUBLIC_URL} \ | ||
REACT_APP_API_URL=${ADMIN_API_URL} | ||
|
||
# Note: Image is used only for building | ||
RUN ASSETS_VERSION=$(cat build/asset-manifest.json | grep "\"main.js\"" | sed -E "s/\s+\"main.js\": \"static\/js\/main\.(.*)\.js\",/\1/") && \ | ||
dockerize -delims "{{{:}}}" \ | ||
-template build/index.html.tmpl:build/index.html \ | ||
-template build/service-worker.js.tmpl:build/service-worker.js \ | ||
-template build/static/js/main.$ASSETS_VERSION.js.tmpl:build/static/js/main.$ASSETS_VERSION.js \ | ||
-template build/static/js/main.$ASSETS_VERSION.js.map.tmpl:build/static/js/main.$ASSETS_VERSION.js.map |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: '3.6' | ||
services: | ||
api-admin: | ||
image: knitpk/api-admin:latest | ||
build: | ||
context: . | ||
cache_from: | ||
- knitpk/api-admin:latest | ||
args: | ||
- APP_PUBLIC_URL='/admin' | ||
- APP_API_URL='localhost' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters