Skip to content

Instantly share code, notes, and snippets.

@armand1m
Last active March 10, 2024 14:54
Show Gist options
  • Save armand1m/b8061bcc9e8e9a5c1303854290c7d61e to your computer and use it in GitHub Desktop.
Save armand1m/b8061bcc9e8e9a5c1303854290c7d61e 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"]
@etgrieco
Copy link

@etgrieco The provided code works. On the initial container start up the dependencies need to be downloaded and then are stored into the volume. When the container it is stoped and started again, dependencies does not need to re-installed but yarn take some time to "Link the dependencies", and this happens on every container restart. Any idea about that ? It take a good amount of time in my end (~20 seconds to link the dependencies).

I haven't used this docker-based workflow in a while, but hopefully others who ran into your issue can chime in!

@ManolisPap
Copy link

@etgrieco Ah, okay. I am developing a Next.js app, and its dependency installation is quite annoying. Without such a "trick", when I change something in package.json, it takes over 150 + seconds just for the yarn install step when a rebuild the Docker Image. So, any trick/optimization technique would be greatly appreciated!

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