Building Docker image for Node application using Yarn dependency

22,413

Solution 1

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

Solution 2

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

Solution 3

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" ]
Share:
22,413
Kendo Nguimi
Author by

Kendo Nguimi

Updated on July 09, 2022

Comments

  • Kendo Nguimi
    Kendo Nguimi almost 2 years

    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.

    • JBone
      JBone over 6 years
      this looks more like issue with the packages that you are using. Try installing without docker. Just do a regular install on your machine (npm install) and see what happens. look for that particular module (and the version number) on npmjs.org.
    • Jinna Balu
      Jinna Balu over 6 years
      how areyou running your application without docker, npm run develop? if so change your CMD line like this "CMD ["npm", "run", "develop"] "
  • Kendo Nguimi
    Kendo Nguimi over 6 years
    Thanks for the help. Now the image builds successfully but I'm unable to run it. When I do a docker run, it shows my site is running at localhost:8000 but when I go to the browser, nothing is seen. It says Safari can't connect to server mean while when I do a normal run with npm run develop everything works fine and I can see my site. What can be the problem here?
  • Clive Makamara
    Clive Makamara over 6 years
    You need to make sure of two things. 1) That you are exposing 8000 with for example docker run -p 8000:8000 myimage 2) The dev server is running on 0.0.0.0 instead of localhost instead of localhost just to make sure it will work as expected
  • Jinna Balu
    Jinna Balu over 6 years
    to reduce the image size use alpine, you need to clean yar. check my answer
  • Clive Makamara
    Clive Makamara over 6 years
    Why do you need docker compose for a single image?
  • Jinna Balu
    Jinna Balu over 6 years
    i don't think this will be a big mess, to easy our joob, run command is reduced to "docker-compose up" and "docker-compose down"