Skip to content

Instantly share code, notes, and snippets.

@xriu
Forked from ankitpopli1891/Dockerfile
Created December 27, 2017 10:47
Show Gist options
  • Save xriu/33969011742d44c6d68e1453b956e6d7 to your computer and use it in GitHub Desktop.
Save xriu/33969011742d44c6d68e1453b956e6d7 to your computer and use it in GitHub Desktop.
Dockerizing a web app using Nginx
# https://docs.docker.com/engine/userguide/eng-image/multistage-build/
# The following example shows a way to bundle a node package & serve it using Nginx,
# while keeping the image size small & the image build time shorter, using
# the Multi-Stage Builds
########### Stage 0
# the node 8 alpine image is ~63MB, while the node image is around ~670MB
# the alpine image is resource efficient, smaller in bundle size
# saves downloading time & precious bandwidth
FROM node:8.0.0-alpine
# (optional) required if you have packages directly
# being installed from a github repo
RUN apk add --update git
# docker will again download the dependencies
# during the `npm i` instead of using cache even
# if there is no change in package.json
# copy ONLY the package.json
# to make sure cache only bursts if we update package.json
WORKDIR /usr/src/app
COPY package.json /usr/src/app/package.json
RUN npm i
# now copying the source is fine,
# just before the build step,
# since the dependencies have been cached
COPY . /usr/src/app
RUN npm run build
########### Stage 1
FROM nginx:1.13.0
# instead of copying the build folder from the local system,
# we copy it from the previous step
COPY --from=0 /usr/src/app/build /usr/share/nginx/html
RUN rm /etc/nginx/nginx.conf
RUN rm /etc/nginx/conf.d/*
COPY config/nginx/nginx.conf /etc/nginx/nginx.conf
COPY config/nginx/conf.d/client.conf /etc/nginx/conf.d/client.conf
VOLUME /etc/nginx/conf.d/
EXPOSE 80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment