Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yarn install inside docker image with yarn workspaces

Tags:

I am using yarn workspaces and I have this packages in my package.json:

  "workspaces": ["packages/*"] 

I am trying to create a docker image to deploy and I have the following Dockerfile:

# production dockerfile FROM node:9.2  # add code COPY ./packages/website/dist /cutting  WORKDIR /cutting  COPY package.json /cutting/ RUN yarn install --pure-lockfile && yarn cache clean --production  CMD npm run serve 

But I get the following error:

error An unexpected error occurred: "https://registry.yarnpkg.com/@cutting%2futil: Not found"

@cutting/util is the name of one of my workspace packages.

So the problem is that there is no source code in the docker image so it is trying to install it from yarnpkg.

what is the best way to handle workspaces when deploying to a docker image.

like image 262
dagda1 Avatar asked Feb 24 '18 23:02

dagda1


People also ask

Does Docker support yarn?

Running Docker containers on YARN works very similar to running existing containers. Containers have access to files that are localized for the container as well as logging. You can deploy a load balanced web server pair on a single node HDP cluster using the YARN Services API .

How do yarn workspaces work?

Yarn Workspaces is a feature that allows users to install dependencies from multiple package. json files in subfolders of a single root package. json file, all in one go. Yarn can also create symlinks between Workspaces that depend on each other, and will ensure the consistency and correctness of all directories.

Does node Docker image have yarn?

All of the images contain pre-installed versions of node , npm , and yarn .


2 Answers

This code won't work outside of the docker vm, so it will refuse in the docker, too.

The problem is you have built a code, and copy the bundled code. The yarn workspaces is looking for a package.json that you don't have in the dist folder. The workspaces is just creating a link in a common node_modules folder to the other workspace that you are using. The source code is needed there. (BTW why don't you build code inside the docker vm? That way source code and dist would also be available.)

like image 112
androbin Avatar answered Oct 13 '22 08:10

androbin


Here is my dockerfile. I use yarn workspaces and lerna, but without lerna should be similar. You want to build your shared libraries and then test the build works locally by running your code in your dist folder.

############################################################################### # Step 1 : Builder image FROM node:11 AS builder WORKDIR /usr/src/app ENV NODE_ENV production RUN npm i -g yarn RUN npm i -g lerna COPY ./lerna.json . COPY ./package* ./ COPY ./yarn* ./ COPY ./.env . COPY ./packages/shared/ ./packages/shared COPY ./packages/api/ ./packages/api # Install dependencies and build whatever you have to build  RUN yarn install --production RUN lerna bootstrap RUN cd /usr/src/app/packages/shared && yarn build RUN cd /usr/src/app/packages/api && yarn build ############################################################################### # Step 2 : Run image FROM node:11 LABEL maintainer="Richard T" LABEL version="1.0" LABEL description="This is our dist docker image" RUN npm i -g yarn RUN npm i -g lerna ENV NODE_ENV production ENV NPM_CONFIG_LOGLEVEL error ARG PORT=3001 ENV PORT $PORT  WORKDIR /usr/src/app COPY ./package* ./ COPY ./lerna.json ./ COPY ./.env ./ COPY ./yarn* ./ COPY --from=builder /usr/src/app/packages/shared ./packages/shared COPY ./packages/api/package* ./packages/api/ COPY ./packages/api/.env* ./packages/api/ COPY --from=builder /usr/src/app/packages/api ./packages/api RUN yarn install  CMD cd ./packages/api && yarn start-production EXPOSE $PORT ############################################################################### 
like image 42
Richard Torcato Avatar answered Oct 13 '22 08:10

Richard Torcato