How can I include a local package dependency in my docker build?

Viewed 800

I have a turborepo monorepo, using yarn workspaces which looks like this

├── packages

│ ├── server-common

├── services

│ ├── auth

Services/auth is dependant on packages/server-common and using yarn workspaces, imports it like so

  "dependencies": {
    "server-common": "*",
  }

I want to build a docker image out of services/auth, however packages/server-common is not hosted on npm, it is a local package.

Hence, when building the following dockerfile

FROM node:16-alpine

WORKDIR /app
COPY package.json .
RUN yarn install --production
COPY . .

CMD ["npm", "start"]

It fails on the yarn install stage, because docker is trying to download a package from npm, when it is a local package.

#9 8.441 error Received malformed response from registry for "server-common". The registry may be down.

Is there a way that this local package can be included in the docker build?

1 Answers

A solution would be to copy your local package into the container.

FROM node:16-alpine

COPY path/to/server-common .
WORKDIR /app
COPY package.json .
RUN yarn install --production
COPY . .

CMD ["npm", "start"]
"dependencies": {
    "server-common": "file:../server-common"
}
Related