package.json not found in node:13-alpine docker container - javascript

I made 2 docker images from 2 folder in this repo. The dockerfile was:
FROM node:13-alpine
EXPOSE 5000
RUN mkdir /app
RUN mkdir -p /home/app
COPY . /home/app
RUN npm install
CMD ["npm","run","start"]
But the docker logs showed that the package.json file was not found and hence the containers closed, I can't even enter the terminal of the containers to see the available files.
The error output is in the below image.
To reproduce the error, clone the repo and run sudo docker-compose -f docker-compose.yaml up

You didn't specify the working directory in your dockerfile and that's why the package.json cannot be found.
Ref: https://docs.docker.com/engine/reference/builder/#workdir
add WORKDIR before RUN npm install
WORKDIR /home/app
RUN npm install

Related

Docker build cannot find javascript modules (Docker/Nodejs/Express)

I'm building Express using Docker.
Previously, I uploaded it to the Ubuntu server using the Dockerfile.
But suddenly there was an error.
When building Docker Image, there was an error that could not read the file.
When I build with Docker and run, It can't find the javascript module I made.
An error occurred even if dockerfile2 was used instead of dockerfile1.
I don't know what the problem is.
I updated Docker, too.
Docker version 20.10.8,
Error Msg.
(when use dockerfile1)
Error msg : Cannot find module
Error Msg.
(when use dockerfile2)
> socket_example#1.0.0 start /usr/src/app
> node app.js
internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module './AgoraApi'
Require stack:
- /usr/src/app/src/audioChat/audioChannelHandler.js
- /usr/src/app/src/multiplay/rooms/GameRoom.js
- /usr/src/app/app.js
docker file1
FROM node:14
WORKDIR /
COPY package.json ./
RUN npm install
COPY ./ ./
EXPOSE 4000
CMD [ "npm", "start" ]
docker file2
FROM node:14
WORKDIR /usr/src/app
COPY package.json /usr/src/app
RUN npm install
COPY . /usr/src/app
EXPOSE 4000
CMD [ "npm", "start" ]
dockerignore file
.git
.gitignore
README.md
webpack.config.js
NAF-Gallery-Test.git
package-lock.json
backup
logs
node_modules
config/sql/*
!src
!util
Folder Structure
app.js
config.js
src
ㄴaudioChat
ㄴagoraApi.js
util
ㄴvipRoomManager.js
...

run nuxt.js project with docker-compose

I am trying to deploy my nuxt project with docker so i create Dockerfile and docker-compose.yml and also the .dockerignore file but it giving me errors it's my first time to work with docker i follow the process from the official documentation but i do not really a strong understanding about docker. If i miss something or anymore understand why there is an error while i'm trying to build docker-compose please let me know.
Dockerfile
FROM node:14.15.4-alpine
#create destination directory
RUN mkdir -p /twin_login/app
WORKDIR /app
COPY package*.json ./app
#update and install dependency
RUN apk update && apk upgrade
RUN apk add git
RUN apk add python3
#copy the app, note .dockerignore
COPY . .
COPY . /app
RUN npm install
#build necessary, even if no static files are needed,
RUN node -v
RUN npm -v
#set app serving to permissive / assigned
ENV HOST 0.0.0.0
#expose 8000 on container
EXPOSE 8000
CMD ["npm", "start"]
Docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- 8000:3000
volumes:
- ./twin_login:/app
stdin_open: true
tty: true
networks:
- twin_login
networks:
twin_login:
external: true
.dockerignore
node_modules
.gitignore
.nuxt
Error this is the error i am getting
WARN[0000] Found orphan containers ([twin_management_screen_nuxt_1 twin_management_screen_nuxt_run_2d8fca86d2ca twin_management_screen_nuxt_run_6d5ee3c9b0ba twin_management_screen_nuxt_run_881e46ae1ad1 twin_management_screen_nuxt_run_5089871620b5]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
The error you found is not an error, just a warning (thus WARN[0000]) that you have orphaned containers that you should clean up with --remove-orphans (because they take up a lot of space on your system).
Check the syntax of your yml file and possibly also the Dockerfile, indentation is strict.
It can look as though you forgot the "run" part of your start CMD.
A tip is to do as little as possible in the Dockerfile and most of the stuff in docker-compose. This is the minimum stuff you need in your Dockerfile:
FROM node:xxx.xxx
ENV NODE_ENV=dev
WORKDIR /
COPY ./package*.json ./
RUN npm install
COPY . .
EXPOSE 8080 #or other
CMD ["npm", "run", "start"]

Query about adding user in dockerfile for dockerizing the simple react app

I am stuck this issue: when I add the user in Dockerfile at the end and build an image from it and after that when I start a container from this image, I have permission denied errors. I start the container using this command:
docker run react-app npm start
It gives the permission denied error because I created build as a root user and defined the new "app" user at the end. When I moved the new "app" user at the top then the permission denied error happens while building the image. Here is the my Dockerfile before and after I make this change:
Before
FROM node:14.16.1-alpine3.13
WORKDIR /app/
COPY package*.json .
RUN npm install
COPY . .
ENV API_URL=https://api.myapp.com
EXPOSE 3000
RUN addgroup app && adduser -S -G app app
USER app
Creating image from it, image tagged react-app. The error after running the image is:
$ docker run react-app npm start
:information_source: 「wds」: Project is running at http://172.17.0.2/
:information_source: 「wds」: webpack output is served from
:information_source: 「wds」: Content not from webpack is served from /app/public
:information_source: 「wds」: 404s will fallback to /
Starting the development server…
Failed to compile.
EACCES: permission denied, mkdir ‘/app/node_modules/.cache’
After
FROM node:14.16.1-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app/
COPY package*.json .
RUN npm install
COPY . .
ENV API_URL=https://api.myapp.com
EXPOSE 3000
error while building the image after running the image:
$ docker build -t react-app
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /app
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access ‘/app’
npm ERR! [Error: EACCES: permission denied, access ‘/app’] {
npm ERR! errno: -13,
npm ERR! code: ‘EACCES’,
npm ERR! syscall: ‘access’,
npm ERR! path: ‘/app’
npm ERR! }
How can I resolve this issue?
I have once had a similar problem. I found a solution that I believe is also appliable to this scenario. I don't know if they are against any best practices,but the solution worked for me. Basicaly I've changed the ownership of the unaccessible files to belong to the user that was executing the processes in the container.
In your case, the dockerfiles would as follows:
FROM node:14.16.1-alpine3.13
WORKDIR /app/
COPY package*.json .
RUN npm install
COPY . .
RUN chown -R app /app/node_modules
ENV API_URL=https://api.myapp.com
EXPOSE 3000
RUN addgroup app && adduser -S -G app app
USER app
As for building the app inside the container, I believe you will have to make all the files from folder /app/ accessible to user app, it would then be as follows:
FROM node:14.16.1-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app/
COPY package*.json .
RUN npm install
COPY . .
RUN chown -R app /app
ENV API_URL=https://api.myapp.com
EXPOSE 3000

Unable to COPY config file in nginix /etc/nginx/conf.d/default.conf

Unable to Copy config file in my project dir to /etc/nginx/conf.d/default.conf
source file location:
/app/nginix.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
destination : /etc/nginx/conf.d/default.conf
Steps in docker file :
Tried the multi stage build:
- FROM node:8.9.0 as buid
- WORKDIR /app
- COPY package.json package-lock.json ./
- RUN npm install
- COPY . ./
- RUN npm run build
- FROM nginx:alpine
- RUN mkdir -p /var/www/html/client
- COPY --from=buid /app/nginix.conf /etc/nginx/conf.d/default.conf
- COPY --from=buid /app/build/ /var/www/html/client
Tried commenting the first copy command, and it was able to copy the build and it was good.
when it is able to find the build in the app dir why is it not able to find the nginix.conf file which is also in the same dir, did a ls -la and saw the nginix.conf file.
TIA
If the source file path is /app/nginix.conf then dockefile should contain:
COPY /app/nginx.conf /etc/nginx/conf.d/default.conf
If you're running docker build command from /app directory on your host then your above dockerfile should work.
Update:
If you're expecting /app/nginx.conf file of node docker image to present in nginx:alpine image then you need to use multi-stage docker builds.
Change your dockerfile to
FROM node as build
WORKDIR /app
COPY package json files
RUN npm build
FROM nginx:alpine
COPY --from=build /app/nginx.conf /etc/nginx/conf.d/default.conf
This will copy /app/nginx.conf file from node image to nginx:alpine image.
If somebody is still struggling with this....
changing the WORKDIR from app to builddir worked!
FROM node:alpine as builder
WORKDIR '/builddir'
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /builddir/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/nginx.conf /etc/nginx/conf.d
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

How to cache node_modules on Docker build?

I've been trying for some time to cache node_modules on a Docker build. I've tried several approaches including the one here, but without success.
My main reason to cache is because it takes 30+ minutes to build my image, which is way too much.
My Dockerfile:
# This image will be based on the oficial nodejs docker image
FROM node:4.2.1
RUN npm install -g jspm#0.17.0-beta.7 && \
npm install -g gulp && \
npm install -g tsd
# Use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /src/package.json
RUN cd /src && npm install
# Put all our code inside that directory that lives in the container
ADD . /src
# Set in what directory commands will run
WORKDIR /src
# Install dependencies
RUN cd /src && \
tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]
I do not have a .dockerignore file. I added one before but it still didn't cache my node_modules.
So, how to I cache my node_modules? Feel free to suggest modifications to the Dockerfile.
Thanks!
I'm not sure whether it is the root of the error, but try to specify the destination folder in the ADD command and not the destination file.
ADD package.json /src
Moreover, you can use COPY instead of ADD (ADD can work with url and archives but you don't need it here).
You can also specify your working directory earlier in the file.
Try with this code :
# This image will be based on the official nodejs docker image
FROM node:4.2.1
RUN npm install -g jspm#0.17.0-beta.7 && \
npm install -g gulp && \
npm install -g tsd
# Set in what directory commands will run
WORKDIR /src
# Use changes to package.json to force Docker not to use the cache
# when we change our application’s nodejs dependencies:
COPY package.json ./
RUN npm install
# Put all our code inside that directory that lives in the container
COPY . ./
# Install dependencies
RUN tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]

Categories