# -------------------------------------------------------------------------------------------------- # NOTE: This Dockerfile was designed to be run with BuildKit. # # Enable BuildKit using: # $ export DOCKER_BUILDKIT=1 # $ export COMPOSE_DOCKER_CLI_BUILD=1 # -------------------------------------------------------------------------------------------------- # The service to build ARG SERVICE # The base image to use ARG BASE=base-alpine # Base Definitions --------------------------------------------------------------------------------- # Alpine base definition FROM node:14-alpine AS base-alpine # Alpine dev definition FROM base-alpine AS base-alpine-dev RUN apk add build-base python3 # Slim base definition FROM node:14-slim AS base-slim # Slim dev definition FROM base-slim AS base-slim-dev # Development Image -------------------------------------------------------------------------------- # Define the base image FROM ${BASE}-dev AS dev ARG SERVICE WORKDIR /opt/app # Environment configuration ENV PATH "/app/node_modules/.bin:$PATH" RUN echo "#!/bin/sh" >> /bin/lerna && \ echo '( cd /opt/app && ./node_modules/.bin/lerna "$@" )' >> /bin/lerna && \ chmod +x /bin/lerna # Copy over Lerna stuff COPY lerna.json package.json tsconfig.json ./ RUN yarn install # Symlink the requested service to /app RUN ln -s /opt/app/services/${SERVICE} /app WORKDIR /app # Build Definitions -------------------------------------------------------------------------------- # Create the builder image with all dependencies FROM ${BASE}-dev AS builder ARG SERVICE WORKDIR /app RUN yarn global add lerna typescript rimraf COPY scripts scripts COPY lerna.json package.json tsconfig.json yarn.lock ./ RUN yarn install COPY packages packages COPY services/${SERVICE}/package.json ./services/${SERVICE}/ # Cache an image containing only the prod-dependencies FROM builder AS prod-deps RUN lerna bootstrap -- --production --no-optional RUN yarn run export:deps # Build the service FROM builder AS build ARG SERVICE COPY services/${SERVICE} ./services/${SERVICE} RUN lerna clean --yes && lerna bootstrap && lerna run clean && lerna run build RUN yarn run export:builds # Return to base image with the compiled files and production dependencies FROM ${BASE} AS prod ARG SERVICE WORKDIR /app COPY --from=prod-deps /app/build /opt COPY --from=build /app/build /opt RUN for link in $(find /opt/services -type l); do dir=$(readlink -f $link); rm $link; ln -s $dir $link; done RUN mv /opt/services/${SERVICE}/* /app && rm -rf /opt/services # TODO: Should remove unnecessary packages that were included # Default entrypoint ENTRYPOINT "/bin/sh"