Building Docker image for Node application using Yarn dependency - node.js

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 glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-inline#1.0.5: use glamor/inline instead
warning gatsby-plugin-glamor > glamor-react > glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-server > glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby > babel-preset-es2015#6.24.1: 🙌 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.

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

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

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" ]

Related

Strapi Webpack stucks in Docker Container

I created a project using
npx create-strapi-app my-project --quickstart
created a Dockerfile:
FROM strapi/base
WORKDIR /srv/app
COPY ./package.json ./
COPY ./yarn.lock ./
RUN yarn install
COPY . .
ENV NODE_ENV production
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]
Now i'm trying to build the Docker Image, but it stucks always when building the webpack.
When I manually start yarn build in the container, then i can see that it's stuck at 90% processing chunk assets.
My container has 4GB Ram and 2 cpus attached. I can see that its using 100% SSD Read the whole time.
Do you have any suggestions?
Strapi Docker Github issue
Try this :
FROM strapi/base
WORKDIR /srv/app
COPY ./package.json ./
COPY ./yarn.lock ./
COPY . .
RUN yarn install
ENV NODE_ENV production
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]
I think COPY . . Before yarn install was the issue

Dockerizing React App but ERR_EMPTY_RESPONSE

I'm trying to turn this nodeJS video game into a docker container:
https://github.com/navignaw/TooManyChefs
But I'm getting "ERR_EMPTY_RESPONSE" when I try to access the started docker container:
http://localhost:3000/
I think the issue is my dockerfile:
# pull official base image
FROM node:13.12.0-alpine
# set working directory
WORKDIR /usr/src/app
ENV PATH /app/node_modules/.bin:$PATH
COPY package*.json ./
RUN npm install
COPY . ./
EXPOSE 3000
CMD ["npm", "start"]
Docker commands:
docker build -t hal/chefs .
docker run -p 3000:3000 -d hal/chefs
Docker logs:
docker logs 92928695b528a7ed4059bcc32af1d58a309f855294b48d49c60a2bb977755c4e
> TooManyChefs# start /usr/src/app
> watchify -o js/bundle.js -v -d js/main.js
Can someone please give advice how I can fix or troubleshoot? I really appreciate your help!
-Hal
change the following line:
ENV PATH /app/node_modules/.bin:$PATH
to :
ENV PATH /usr/src/app/node_modules/.bin:$PATH
Edit-01:
Also update your npm install command to following:
npm install --no-optional && npm cache clean --force

Nestjs exiting on Container Start

My container is exiting immediately upon running.
I am using Nestjs and Postgres.
Here is my Dockerfile:
FROM node:14.5.0 AS build
WORKDIR /src
RUN apt update && apt install python -y
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node:14.5.0-alpine
ENV PORT=3000
WORKDIR /src
COPY --from=build /src/node_modules ./node_modules
COPY --from=build /src/dist ./dist
EXPOSE 3000
CMD [ "node", "dist/src/main.js"]
Here is the console output:
Thanks in Advance
So i solved it using Node version 13
I do not know whether the issue is in node 14 or if the Nest is not compatible with Node 14 yet.
For someone reading in future, here is the issue you can track :
https://github.com/nestjs/nest/issues/5045

TSC error when compiling through Dockerfile

I have a Node / TypeScript application which I'm trying to run from Docker.
yarn tsc works fine locally, but does not work in the context of the Dockerfile. I think the issue is regarding which directory the command is being run from with the Dockerfile, but I'm not sure how to fix it.
How can I make sure tsc can see the tsconfig.json file?
Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN yarn
# Copy source files.
COPY . .
# Run tsc.
RUN yarn prepare
# Run app.
CMD [ "yarn", "start" ]
package.json
"scripts": {
"prepare": "yarn tsc",
"tsc": "tsc -p .",
"dev": "ts-node-dev --respawn --transpileOnly server.ts",
"start": "./node_modules/nodemon/bin/nodemon.js ./build/server.js",
},
Error output
docker build --tag app ./
Sending build context to Docker daemon 114.1MB
Step 1/7 : FROM node:10
---> e05cbde47b8f
Step 2/7 : WORKDIR /usr/src/app
---> Using cache
---> faaea91b16ae
Step 3/7 : COPY package*.json ./
---> 64310f50355d
Step 4/7 : RUN yarn
---> Running in be8aed305980
yarn install v1.16.0
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents#1.2.9: The platform "linux" is incompatible with this module.
info "fsevents#1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
$ yarn tsc
yarn run v1.16.0
$ tsc -p ./
error TS5057: Cannot find a tsconfig.json file at the specified directory: './'.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
The command '/bin/sh -c yarn' returned a non-zero code: 1
As David said,
Just running yarn is equivalent to yarn install, which runs a "prepare" script.
You could change your script to copy the application code to the given directory before running yarn command, if you do not need for yarn command to run first.
FROM node:10
WORKDIR /usr/src/app
# Copy source files.
COPY . .
RUN yarn
# Run app.
CMD [ "yarn", "start" ]
If you look carefully at your docker build output, you'll notice that the last Dockerfile instruction that gets run is the
Step 4/7 : RUN yarn
In particular, yarn treats a couple of script names as special. Just running yarn is equivalent to yarn install, which runs a "prepare" script. In your case the "prepare" script runs tsc; but, this is happening during the "install dependencies" phase of the Dockerfile, and your application code isn't there yet.
You can work around this by using yarn's --ignore-scripts option. A number of other options make sense here in the context of building a Docker image. (You could use a multi-stage build to build a final image with only --production dependencies and not the tsc compiler or other build-time tools, for example.)
FROM node:10
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn install --ignore-scripts --frozen-lockfile --non-interactive
COPY . .
RUN yarn prepare
CMD ["node", "./build/server.js"]

Docker does not identify npm install command and throws error

My Dockerfile is as shown below:
From ubuntu:14.04
WORKDIR /app
#COPY package.json /app/package.json
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["npm","start"]
Now, when I run command sudo docker -t my-app .. It gives me the following error:
Sending build context to Docker daemon 453.6 kB Sending build context
to Docker daemon Step 0 : FROM ubuntu:14.04 ---> 37a9c4a8276c Step 1
: WORKDIR /app ---> Using cache ---> a83d4ef27948 Step 2 : COPY .
/app ---> 1029f5d7d8a3 Removing intermediate container eb9e7ea7f7e6
Step 3 : RUN npm install ---> Running in 5d4f2c05d2d8 /bin/sh: 1:
npm: not found INFO[0000] The command [/bin/sh -c npm install]
returned a non-zero code: 127
Is there anything missing in my Dockerfile?
This is expected. Your image doesn't have node installed, since the base image is ubuntu. You should use the node image as a base image.
From node
WORKDIR /app
#COPY package.json /app/package.json
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["npm","start"]
Your Dockerfile is building vanilla ubuntu FROM ubuntu:14.04 so if you want to use npm/node in your container you'll need to setup node yourself by adding RUN commands to install node, following the install instructions for ubuntu.
Instead of this, you probably want to simply use the official node image found at:
https://hub.docker.com/_/node/
FROM node
or use a specific version/distro like
FROM node:8.4.0-wheezy
(other tags/versions/distros are listed on the docker hub page)

Resources