Jonathan
Jonathan

Reputation: 479

Unable to install yarn packages via docker build

I'm trying to speed up spinning up docker by having all current packages in yarn.lock be installed on the image already. I think I'm doing yarn install incorrectly, that it is working somewhere else?

relevant part of dockerfile:

# Create a dir
WORKDIR /(WORKDIR)
# Time to install all our dependencies
COPY package.json /(WORKDIR)/package.json
COPY yarn.lock /(WORKDIR)/yarn.lock

# Need the executables to be in the path
ENV PATH /(WORKDIR)/node_modules/.bin:$PATH
RUN yarn check --verify-tree || yarn install --frozen-lockfile

I think my last line is incorrect. It is installing somewhere, but not on the package itself? Either that or caching might be an issue. If I start the image I find the output of yarn check --verify-tree is still the current state of the image.

Upvotes: 1

Views: 9828

Answers (2)

Jason
Jason

Reputation: 2820

I was unable to yarn install during my Docker build. I was getting networking errors (getaddrinfo EAI_AGAIN):

Step 6/8 : COPY yarn.lock /app/yarn.lock
 ---> Using cache
 ---> e55488a3d051
Step 7/8 : RUN yarn
 ---> Running in 5bb8d663d00b
yarn install v1.22.15
[1/4] Resolving packages...
[2/4] Fetching packages...
error An unexpected error occurred: "https://registry.yarnpkg.com/app-root-path/-/app-root-path-3.0.0.tgz: getaddrinfo EAI_AGAIN registry.yarnpkg.com".
info If you think this is a bug, please open a bug report with the information provided in "/app/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
ERROR: Service 'web-svc' failed to build: The command '/bin/sh -c yarn' returned a non-zero code: 1

I recently updated my Ubuntu Linux system, and figured that might be the cause. So, I restarted the docker.service and that resolved the issue.

sudo systemctl restart docker.service

Upvotes: 3

RammusXu
RammusXu

Reputation: 1260

Just RUN yarn and make sure COPY code base after yarn.

FROM        node:12.14.0-alpine3.11

ENV         NODE_ENV=production
WORKDIR     /app

COPY        package.json ./
COPY        yarn.lock ./
RUN         yarn

COPY        src ./

I test it in my machine, you can see if I change yarn.lock. And if I don't change my yarn.lock

$ docker build -t demo .
Step 1/6 : FROM        node:12.14.0-alpine3.11
 ---> 1cbcaddb8074
Step 2/6 : ENV         NODE_ENV=production
 ---> Using cache
 ---> dc7f1a2f7d90
Step 3/6 : WORKDIR     /app
 ---> Using cache
 ---> eec9363713a5
Step 4/6 : COPY        package.json ./
 ---> Using cache
 ---> fde6cf7bb577
Step 5/6 : COPY        yarn.lock ./
 ---> 6a1369622d79
Step 6/6 : RUN         yarn
 ---> Running in ff6433969bea
yarn install v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
warning [email protected]: Invalid bin entry for "sha.js" (in "sha.js").
warning [email protected]: Invalid bin field for "url-loader".
info [email protected]: The platform "linux" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > [email protected]" has unmet peer dependency "react-is@>= 16.8.0".
[4/4] Building fresh packages...
Done in 35.97s.
Removing intermediate container ff6433969bea
 ---> 8dcd2124289d
Successfully built 8dcd2124289d

$docker build -t demo .
Step 1/6 : FROM        node:12.14.0-alpine3.11
 ---> 1cbcaddb8074
Step 2/6 : ENV         NODE_ENV=production
 ---> Using cache
 ---> dc7f1a2f7d90
Step 3/6 : WORKDIR     /app
 ---> Using cache
 ---> eec9363713a5
Step 4/6 : COPY        package.json ./
 ---> Using cache
 ---> fde6cf7bb577
Step 5/6 : COPY        yarn.lock ./
 ---> Using cache
 ---> 6a1369622d79
Step 6/6 : RUN         yarn
 ---> Using cache
 ---> 8dcd2124289d
Step 7/7 : COPY        src ./
 ---> 13474b882e11

Upvotes: 1

Related Questions