Skip to content

Instantly share code, notes, and snippets.

@mightyhorst
Last active October 20, 2019 08:55
Show Gist options
  • Select an option

  • Save mightyhorst/42f644f4a994c85461dc895da497668e to your computer and use it in GitHub Desktop.

Select an option

Save mightyhorst/42f644f4a994c85461dc895da497668e to your computer and use it in GitHub Desktop.
Docker multi build for NodeJs
# deploy-to-heroku.sh
#!/usr/bin/env bash
set -eu
curl -n -X PATCH https://api.heroku.com/apps/$HEROKU_APP_NAME/formation \
-d '{
"updates": [
{
"type": "web",
"docker_image": "'"$HEROKU_DOCKER_IMAGE_ID"'"
}
]
}' \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3.docker-releases" \
-H "Authorization: Bearer $HEROKU_TOKEN"
#############################
#
# Build
#
FROM node:12-alpine as builder
WORKDIR /usr/src/app
COPY package* ./
RUN ["yarn", "install"]
COPY . /usr/src/app
#############################
#
# Build for Prod
# with Environment variables
#
FROM node:12-alpine as build4prod
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app ./
ENV NODE_ENV production
ENV MICROSERVICE_STORAGE_URL https://kitset-ms-storage.herokuapp.com/
ENV MICROSERVICE_WEBSSH_URL https://kitset-ms-webssh.herokuapp.com/
ENV MICROSERVICE_FOLDER_WATCHER_URL https://kitset-ms-folder-watcher.herokuapp.com/
ENV IDP https://kitset-idp.herokuapp.com/
RUN ["yarn", "run", "build"]
# Gets Sonarqube Scanner from Dockerhub and runs it
# FROM newmitch/sonar-scanner:latest as sonarqube
# COPY --from=builder /usr/src/app/src /root/src
#############################
#
# Tests
#
FROM node:12-alpine as tests
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app/ .
RUN ["npm", "test"]
#############################
#
# Dev Environment
#
FROM node:12-alpine as dev
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app ./
VOLUME /usr/src/app
# usage cli: docker -v $PWD/my_computer:/usr/src/app
# usage compose:
# service_name:
# volume:
# - "my_computer:/usr/src/app"
ENV NODE_ENV development
ENV MICROSERVICE_STORAGE_URL https://kitset-ms-storage.herokuapp.com
ENV MICROSERVICE_WEBSSH_URL https://kitset-ms-webssh.herokuapp.com
ENV MICROSERVICE_FOLDER_WATCHER_URL https://kitset-ms-folder-watcher.herokuapp.com
EXPOSE 8081
CMD ["yarn", "run", "start:dev"]
#############################
#
# Prod Environment
#
# Docker multi build does not support selective running yet. So both Dev and Prod would run
#############################
#
# Build
#
FROM node:12-alpine as builder
WORKDIR /usr/src/app
COPY package* ./
RUN ["yarn", "install"]
COPY . /usr/src/app
#############################
#
# Build for Prod
# with Environment variables
#
FROM node:12-alpine as build4prod
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app ./
ENV NODE_ENV production
ENV MICROSERVICE_STORAGE_URL https://kitset-ms-storage.herokuapp.com/
ENV MICROSERVICE_WEBSSH_URL https://kitset-ms-webssh.herokuapp.com/
ENV MICROSERVICE_FOLDER_WATCHER_URL https://kitset-ms-folder-watcher.herokuapp.com/
ENV IDP https://kitset-idp.herokuapp.com/
RUN ["yarn", "run", "build"]
# Gets Sonarqube Scanner from Dockerhub and runs it
# FROM newmitch/sonar-scanner:latest as sonarqube
# COPY --from=builder /usr/src/app/src /root/src
#############################
#
# Tests
#
FROM node:12-alpine as tests
WORKDIR /usr/src/app
COPY --from=builder /usr/src/app/ .
RUN ["npm", "test"]
#############################
#
# Dev Environment
#
# Docker multi build does not support selective running yet. So both Dev and Prod would run
#############################
#
# Prod Environment
#
FROM nginx:1.17.4 as prod
COPY --from=build4prod /usr/src/app/dist /usr/share/nginx/html
COPY ./deployment/nginx/default.conf.template /etc/nginx/conf.d/default.conf.template
COPY ./deployment/nginx/nginx.conf /etc/nginx/nginx.conf
CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment