Skip to content

Instantly share code, notes, and snippets.

@sibelius
Forked from armand1m/Dockerfile
Created April 21, 2019 16:40
Show Gist options
  • Save sibelius/1c34ac6b977e3440f4edb43e1372d550 to your computer and use it in GitHub Desktop.
Save sibelius/1c34ac6b977e3440f4edb43e1372d550 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