Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Docker image for Node application using Yarn dependency

I am trying to build a docker image for a node application that uses yarn to install dependencies. My Dockerfile looks like this:

 FROM node:7
WORKDIR /app
COPY package.json /app
RUN yarn install
COPY . /app
CMD npm run develop
EXPOSE 8000

Every thing runs well when I run yarn install on my local machine but when I do a docker build, I get this error that blocks for ever.

**docker build -t  rs  .**
Sending build context to Docker daemon  219.1MB
Step 1/7 : FROM node:7
 ---> d9aed20b68a4
Step 2/7 : WORKDIR /reason
 ---> Using cache
 ---> fe51a1860989
Step 3/7 : COPY package.json /reason
 ---> Using cache
 ---> b0e136ee6eeb
Step 4/7 : RUN yarn install
 ---> Running in e273f8cf1f3e
yarn install v0.24.4
info No lockfile found.
[1/4] Resolving packages...
Couldn't find any versions for "glamor" that matches "next"
? Please choose a version of "glamor" from this list: (Use arrow keys)
❯ 2.20.40
  2.20.39
  2.20.38
  2.20.37
  2.20.36
  2.20.35
  2.20.34
(Move up and down to reveal more choices)warning [email protected]: abandoned, please use v2 instead
warning gatsby-plugin-glamor > [email protected]: use glamor/inline instead
warning gatsby-plugin-glamor > glamor-react > [email protected]: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-server > [email protected]: abandoned, please use v2 instead
warning gatsby > [email protected]: 🙌  Thanks for using Babel: we recommend using babel-preset-env now: 

please read babeljs.io/env to update!

The console remains in this stage for ever. How can I fix this please.

like image 822
Kendo Nguimi Avatar asked Oct 14 '17 11:10

Kendo Nguimi


3 Answers

You should first run yarn install to generate a yarn lockfile (yarn.lock) before building the image. Then make sure to copy it along with the package.json. Your dockerfile should look like this :

FROM node:7 
WORKDIR /app 
COPY package.json /app 
COPY yarn.lock /app
RUN yarn install 
COPY . /app 
CMD npm run develop 
EXPOSE 8000

With this all dependencies should install successfully when building your image

like image 125
Clive Makamara Avatar answered Sep 29 '22 22:09

Clive Makamara


Dockerfile

FROM node:6.9.5-alpine
RUN mkdir -p /code
WORKDIR /code
ADD . /code
RUN npm install -g -s --no-progress yarn && \
    yarn && \
    yarn run build && \
    yarn cache clean
CMD [ "npm", "start" ]
EXPOSE 8080

docker-compose.yml

version: '2'
services:
  sample-app:
    image: sample-node-yarn-app
    ports:
      - "8080:8080"

Create docker image

docker build -t sample-node-app .

RUN

docker-compose up -d
like image 20
Jinna Balu Avatar answered Sep 29 '22 23:09

Jinna Balu


You can simplify the above answers by using a predefined yarn docker image. We are assuming here this image is only for development purpose. For production mode, you should only consider the minimum binaries, such as node.

FROM gmolaire/yarn:1.22.4_12.18.3-alpine3.12

WORKDIR /usr/local/app
ADD . .

RUN yarn install && \
    yarn build

EXPOSE 8080

CMD [ "yarn", "run", "develop" ]
like image 31
gmolaire Avatar answered Sep 29 '22 22:09

gmolaire