Skip to content

Instantly share code, notes, and snippets.

@gquittet
Last active February 3, 2020 23:57
Show Gist options
  • Save gquittet/efa0ef21540deb870dbfe65c155704d8 to your computer and use it in GitHub Desktop.
Save gquittet/efa0ef21540deb870dbfe65c155704d8 to your computer and use it in GitHub Desktop.
Article Medium - Créer un bon Dockerfile NodeJS
.git
assets/readme
build
documentation
node_modules
.dockerignore
.env
.env.example
.gitignore
.gitlab-ci.yml
.prettierrc
deployment.yaml
docker-compose.yml
Dockerfile
jsconfig.json
README.md
.cache
FROM node:12-slim AS base
ENV NODE_ENV=production
ENV TINI_VERSION=v0.18.0
ENV PORT=3000
EXPOSE 3000
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini && \
mkdir -p /node_app/app && \
chown -R node:node /node_app
WORKDIR /node_app
USER node
COPY --chown=node:node package.json package-lock*.json ./
RUN npm ci && \
npm cache clean --force
WORKDIR /node_app/app
FROM base AS source
COPY --chown=node:node . .
FROM source AS dev
ENV NODE_ENV=development
ENV PATH=/node_app/node_modules/.bin:$PATH
RUN npm install --only=development --prefix /node_app
CMD ["nodemon", "--inspect=0.0.0.0:9229"]
FROM source AS test
ENV NODE_ENV=test
ENV PATH=/node_app/node_modules/.bin:$PATH
COPY --from=dev /node_app/node_modules /node_app/node_modules
RUN eslint --ext js src/ && \
jest
CMD ["jest"]
FROM test AS audit
RUN npm audit --audit-level critical
USER root
RUN apt-get update && \
apt-get -y install ca-certificates
ADD https://get.aquasec.com/microscanner /
RUN chmod +x /microscanner && \
/microscanner $MICROSCANNER_TOKEN --continue-on-failure
FROM source AS buildProd
ENV PATH=/node_app/node_modules/.bin:$PATH
COPY --from=dev /node_app/node_modules /node_app/node_modules
RUN npm run build
FROM source AS prod
COPY --from=buildProd --chown=node:node /node_app/app/build ./build
ENTRYPOINT ["/tini", "--"]
CMD ["node", "./build/main.js"]
HEALTHCHECK --interval=10s CMD curl --fail http://127.0.0.1:$PORT/ping || exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment