# -------------------------------------------------------------------------------------------------- # 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 RUN mkdir /var/autoplex && chown node:node -R /var/autoplex # 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 RUN mkdir /var/autoplex && chown node:node -R /var/autoplex RUN apt-get update RUN apt-get full-upgrade -y RUN apt-get install -y libasound2 iputils-ping curl gnupg1 apt-transport-https dirmngr RUN apt-get autoremove --purge -y RUN rm -rf /var/lib/apt/lists/* # 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-base WORKDIR /app RUN yarn global add lerna typescript rimraf COPY docker/scripts docker/scripts COPY lerna.json package.json tsconfig.json ./ RUN yarn install COPY packages packages # Copy service package.json and patch files if they exist FROM builder-base AS builder ARG SERVICE COPY services/${SERVICE}/package.json ./services/${SERVICE}/ COPY services/${SERVICE}/patches* ./services/${SERVICE}/patches # 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 CMD [ "yarn", "run", "start" ]