I’m migrating a VueJS application from "classic" Yarn 1.x to Yarn 2. Following the install documentation is straightforward and works without problems.
The tricky part comes when packaging the application in a Docker image.
Current Dockerfile
FROM node:14-alpine AS build-stage
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY . ./
RUN yarn build --modern
&& find dist -type f -exec gzip -k "{}" ;
FROM nginx:mainline-alpine as production-stage
RUN apk add --no-cache curl
HEALTHCHECK CMD curl -f http://localhost || exit 1
COPY docker/entrypoint.sh /
RUN chmod +x /entrypoint.sh
COPY docker/app.nginx /etc/nginx/conf.d/default.conf
COPY --from=build-stage /app/dist /usr/share/nginx/html
ENTRYPOINT [ "/entrypoint.sh" ]
Maybe I looked in the wrong places but I couldn’t find any information how a Yarn 2 Zero-Install setup would look like for a Docker image.
Do you have any recommendation on how to use the Yarn 2 approach in a Dockerfile
?
2
Answers
Due to a weird catch-22 with yarn 2’s package install process, I’ve found this to be the most effective method of installing yarn@berry with docker. There’s likely a better method of doing it, but I’m not aware of one.
However, I will note that yarn is intended to run from the local
.yarn/releases
folder, so the best method may simply be to install yarn2 in local and add it to the repo as yarn recommends. Then as a preliminary step with pulling in thepackage.json
file, pull the necessary.yarn
files with it as shown above. This should work under most circumstances, however it gave me difficulty sometimes, hence the above example.@Ethan’s answer makes sense, and it should work. But for me I was getting this strange error during a build:
Even though I definitely copied
.yarn
to the image, how well.I had to actually install yarn v2 within the build:
UPDATE
Turns out Docker doesn’t copy entire directories the way I thought it did.
I had to add an explict
COPY
for the.yarn
:Solved it for me.