Skip to content

Instantly share code, notes, and snippets.

@msreekm
Created June 18, 2019 17:54
Show Gist options
  • Save msreekm/a141725bb8e98649b3718a1ed6a94d0e to your computer and use it in GitHub Desktop.
Save msreekm/a141725bb8e98649b3718a1ed6a94d0e to your computer and use it in GitHub Desktop.
# ---- Base Node ----
FROM mhart/alpine-node:10 AS base
# install node
RUN apk add --no-cache nodejs-current
# set working directory
WORKDIR /root/nextApp
# copy project file
COPY package.json .
COPY tsconfig.server.json .
COPY .npmrc .
COPY _build/staging.env ./.env
#
# ---- Dependencies ----
FROM base AS dependencies
# install node packages
RUN npm set progress=false && npm config set depth 0
RUN npm config set "@fortawesome:registry" https://npm.fontawesome.com/ && \
npm config set "//npm.fontawesome.com/:_authToken" XXXXXX yarn global add typescript
# install global packages
RUN npm install -g rimraf typescript
# install only production node_modules
RUN npm install --only=production
# copy production node_modules aside
RUN cp -R node_modules prod_node_modules
# install ALL node_modules, including 'devDependencies'
RUN npm install
#
# ---- Test ----
# run linters and tests
FROM dependencies AS test
COPY . .
RUN npm run lint
#
# ---- Release ----
FROM base AS release
# copy production node_modules
COPY --from=dependencies /root/nextApp/prod_node_modules ./node_modules
# copy app sources
COPY . .
RUN npm run ci-build
# expose port and define CMD
EXPOSE 1610
CMD npm run start
@derakhshanfar
Copy link

this is my Dockerfile. because I don't copy files between different stages, the build speed is more efficient.

FROM node:current-alpine as base
WORKDIR /opt
COPY package*.json ./
RUN npm ci --production && npm cache clean --force

# for dev mode
FROM base as dev
WORKDIR /opt
ENV PATH=/opt/node_modules/.bin:$PATH
RUN npm install --only=development
WORKDIR /opt/app
# I Don't Copy source code because on dev mode, I'll use mount

# for prodcution without dev dependencies
FROM base as prod
ENV PATH=/opt/node_modules/.bin:$PATH
WORKDIR /opt/app
COPY . .
RUN npm run build 
EXPOSE 3000
CMD npm run start
HEALTHCHECK CMD curl --fail http://localhost:300 


@mikecabana
Copy link

@derakhshanfar you're Dockerfile is really interesting. Can you provide a sample docker command to build as well as a sample oh how it might be used as part of a compose file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment