|
| 1 | +# Step 1: Initial build using the `yarn build` command |
| 2 | +#################################################### |
| 3 | +FROM node:latest as build |
| 4 | + |
| 5 | +# Prepare the build directory |
| 6 | +RUN mkdir -p /opt/build; |
| 7 | + |
| 8 | +WORKDIR /opt/build |
| 9 | + |
| 10 | +# If your build step requires environment variables too, add them here |
| 11 | + |
| 12 | +# Copy required files |
| 13 | +# Note: I specify each file directly here to avoid copying over |
| 14 | +# existing /dist folder or other dev files like .env |
| 15 | +COPY ./src ./src |
| 16 | +COPY [ "package.json", "yarn.lock", "tsconfig.json", "./" ] |
| 17 | + |
| 18 | +RUN yarn install --no-progress && yarn build |
| 19 | + |
| 20 | + |
| 21 | +# Step 2: Fetch production-only dependencies |
| 22 | +#################################################### |
| 23 | +FROM node:latest as dependencies |
| 24 | + |
| 25 | +# Set environment to production |
| 26 | +ENV NODE_ENV='production' |
| 27 | + |
| 28 | +RUN mkdir -p /opt/build; |
| 29 | + |
| 30 | +WORKDIR /opt/build |
| 31 | + |
| 32 | +COPY --from=build [ "/opt/build/package.json", "/opt/build/yarn.lock", "./" ] |
| 33 | + |
| 34 | +RUN yarn install --production=true --no-progress |
| 35 | + |
| 36 | + |
| 37 | +# Step 3: Build done, create the deployable/runnable image step |
| 38 | +#################################################### |
| 39 | +FROM node:slim as release |
| 40 | + |
| 41 | +# Set environment to production |
| 42 | +ENV NODE_ENV='production' |
| 43 | + |
| 44 | +# Prepare the app directory |
| 45 | +RUN mkdir -p /opt/app; |
| 46 | + |
| 47 | +WORKDIR /opt/app |
| 48 | + |
| 49 | +# Add any and all environment variables your application needs here: |
| 50 | +ENV JWT_SECRET= \ |
| 51 | + PORT=80 |
| 52 | + |
| 53 | +# Copy dependencies and compiled application from previous steps |
| 54 | +COPY --from=dependencies /opt/build/node_modules /opt/app/node_modules |
| 55 | +COPY --from=build /opt/build/dist /opt/app/dist |
| 56 | + |
| 57 | +WORKDIR /opt/app |
| 58 | + |
| 59 | +# Run the application using node |
| 60 | +ENTRYPOINT [ "node", "dist/index.js" ] |
| 61 | + |
| 62 | +# Alternatively: Run your application using Forever (ensure this is installed as a dependency first) |
| 63 | +# See: https://github.com/foreversd/forever |
| 64 | +# ENTRYPOINT [ "./node_modules/.bin/forever", "./dist/index.js" ] |
0 commit comments