I have error with docker-compose (exited with code 243) - node.js

I'm have dockers error(exited with code 243), after run my docker-compose file
My docker file:
FROM node:16
ARG stage
RUN mkdir -p /app
WORKDIR /app
COPY ./env/${stage}.env .
COPY ./env/test.env .
EXPOSE 3000
CMD npm run start --prefix ./

I change node version from node:16 to node:16.14.2
It's work for me
FROM node:16.14.2
ARG stage
RUN mkdir -p /app
WORKDIR /app
COPY ./env/${stage}.env .
COPY ./env/test.env .
EXPOSE 3000
CMD npm run start --prefix ./

Related

can't start node server from CMD

I am building a docker image with these Docker file:
FROM node:14.18.1-alpine as projectbuild
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY . .
ARG NODE_ENV
RUN yarn
RUN yarn build
# production environment
FROM nginx:stable-alpine
WORKDIR /app
COPY --from=projectbuild /app/build /usr/share/nginx/html
COPY --from=projectbuild /app/node_modules /app/node_modules
COPY --from=projectbuild /app/server.js /app
COPY --from=projectbuild /app/package.json /app
RUN rm -rf /etc/nginx/conf.d
RUN apk add --update nodejs npm libc6-compat libstdc++
ADD entrypoint.sh /app
RUN chmod +x /app/entrypoint.sh
COPY conf /etc/nginx
EXPOSE 80
CMD ["sh", "-c", "nginx -g \"daemon off;\" ; npm run server"]
Unfortunately when I run it , only nginx seems to come alive , as it ignore the npm run server command.
I would appreciate any insights into what I am doing wrong.

Running Node with dumb-init inside a Docker Container

I am trying to use dumb-init in my docker container but the container OS cannot find the executable. My file is
FROM node:16 AS builder
RUN apt update
RUN apt install dumb-init
WORKDIR /app
COPY package.json .
RUN yarn install
COPY . .
RUN yarn run build
FROM node:16 AS production
WORKDIR /app
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/yarn.lock ./yarn.lock
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["node", "dist/main"]
and when I run it
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/usr/bin/dumb-init": stat /usr/bin/dumb-init: no such file or directory: unknown.
You final nodejs:16 image is a debian based image, you however need to install dumb-init on it.
RUN apt-get install dumb-init
on debian / ubuntu based images
RUN apk add dumb-init
on Alpine based images
I think a better approach here is to just use the docker image for dumb-init.
For example..
FROM building5/dumb-init:1.2.1 as init
FROM node:16.15.0 as build
COPY package*.json ./
RUN npm install
COPY . .
FROM node:16.15.0 as prod
COPY --from=init /dumb-init /usr/local/bin/
COPY --from=build /usr/src/app/package*.json ./
COPY --from=build /usr/src/app/node_modules ./node_modules
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/dumb-init", "--"]
CMD ["node", "./server.js"]

RUN yarn doesn't create node_modules inside docker image

I have a dockerfile that should build an image of a node. In the build phase, it builds right, however, in the production phase, the node_modules folder does not appear inside the image.
I'm not mapping any volumes, just trying to build the image. Could anyone help me with this? I don't understand why this is happening.
FROM node:16.14-alpine3.15 as builder
ENV NODE_ENV=development
WORKDIR /home/node/app
COPY package*.json .
COPY tsconfig.json .
RUN yarn install
COPY . .
RUN yarn build
FROM node:16.14-alpine3.15 as production
ENV NODE_ENV=production
RUN mkdir -p /usr/src/app
VOLUME /usr/src/app
WORKDIR /usr/src/app
RUN mkdir logs
COPY package*.json .
COPY yarn.lock .
RUN yarn install --production
RUN ls -la
RUN ls -la node_modules
COPY --from=builder /home/node/app/dist /usr/src/app/dist
EXPOSE 3333
CMD ["yarn", "start"]

NPM install in docker container fails and returns "The command '/bin/sh -c npm install' returned a non-zero code: 1"

I'm trying to deploy my dockerized MERN stack web app to my vps with gitlab CI/CD. In my react app dockerfile, when I try to install npm packages, I get this error:
Service 'nowfront' failed to build: The command '/bin/sh -c npm install' returned a non-zero code: 1
This is my Dockerfile:
# pull official base image
FROM node as builder
# make directory
RUN mkdir -p /app
RUN chmod -R 777 /app
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json .
RUN npm install
# add app
COPY . .
COPY .env .
RUN ls -li
#RUN yarn build
#CMD ["npm","run","build"]
RUN ls
FROM nginx:stable-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
My Ubuntu version is 20.04.1 LTS.
Is there any solution to pass this?
I updated my Dockerfile and added line 14 and it works now.
# pull official base image
FROM node as builder
# make directory
RUN mkdir -p /app
RUN chmod -R 777 /app
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json .
COPY package-lock.json .
RUN npm install
# add app
COPY . .
COPY .env .
RUN ls -li
#RUN yarn build
#CMD ["npm","run","build"]
RUN ls
FROM nginx:stable-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

How to fix "Directory not empty" on move during docker build?

I have a working Dockerfile for a node application:
FROM node:8.8
ENV TERM=xterm-color NPM_CONFIG_LOGLEVEL=warn PATH="$PATH:/usr/src/app/node_modules/.bin/"
VOLUME ["/logs"]
WORKDIR /tmp/node
ADD package.json yarn.lock .npmrc ./
RUN yarn install --frozen-lockfile --ignore-platform --ignore-engines --quiet
WORKDIR /usr/src/app
ADD . /usr/src/app
RUN mv /tmp/node/* ./ && tsc && webpack
CMD ["node", "/usr/src/app/server"]
I wanted to re-created the caching behavior for the node_modules during build, hence I have updated the Dockerfile for another project to look very similar.
FROM node:9-alpine
WORKDIR /tmp/node
ADD package.json yarn.lock ./
RUN yarn install --frozen-lockfile --ignore-platform --ignore-engines --quiet
WORKDIR /app
ADD . /app/
RUN mv /tmp/node/* ./
EXPOSE 1337
CMD ["yarn", "start"]
Yet for that Dockerfile I get an unexpected error during:
$ docker build .
...
Step 7/9 : RUN mv /tmp/node/* ./
---> Running in 51543827cd89
mv: can't rename '/tmp/node/node_modules': Directory not empty
The command '/bin/sh -c mv /tmp/node/* ./' returned a non-zero code: 1
Why doesn't the mv command work here?
When you run docker build ., the current directory gets passed as its context. It's most likely the case that you have either run the yarn install command on your host already which is why you alrady have a
/app/node_modules
This is why it cannot be moved, as it already exists.
To avoid passing the folder along within the context, you can add:
node_modules/
in your .dockerignore file.

Resources