Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dezren39
Forked from armand1m/Dockerfile
Created September 5, 2019 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dezren39/c558f0db61134c8b9b7d3aeef22c65f5 to your computer and use it in GitHub Desktop.
Save dezren39/c558f0db61134c8b9b7d3aeef22c65f5 to your computer and use it in GitHub Desktop.
Yarn cache compatible Dockerfile

Yarn Dockerfile

An yarn cache compatible Dockerfile, for building node.js images faster.

Usage

Clone this gist into your project root, and add it to your source control. Change the image service-name:latest tag to your project name in the Dockerfile and build.sh files. Then, always build your image using the build.sh script.

$ chmod +x ./build.sh
$ ./build.sh
#!/bin/bash
if [ ! -f .yarn-cache.tgz ]; then
echo "+ build: Init empty .yarn-cache.tgz"
tar cvzf .yarn-cache.tgz --files-from /dev/null
fi
docker build -t service-name:latest .
docker run \
--rm \
--entrypoint cat \
service-name:latest \
/tmp/yarn.lock > /tmp/yarn.lock
if ! diff -q yarn.lock /tmp/yarn.lock > /dev/null 2>&1; then
echo "+ build: Saving Yarn cache"
docker run \
--rm \
--entrypoint tar \
service-name:latest \
czf - /root/.yarn-cache/ > .yarn-cache.tgz
echo "+ build: Saving yarn.lock"
cp /tmp/yarn.lock yarn.lock
fi
FROM alpine
RUN apk add --update --no-cache nodejs
RUN npm i -g yarn
ADD package.json yarn.lock /tmp/
ADD .yarn-cache.tgz /
RUN cd /tmp && yarn
RUN mkdir -p /service && cd /service && ln -s /tmp/node_modules
COPY . /service
WORKDIR /service
ENV FORCE_COLOR=1
ENTRYPOINT ["npm"]
CMD ["start"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment