How to solve ERR_EMPTY_RESPONSE in docker image? - javascript

my docker file:
# Use the official Node.js 10 image.
# https://hub.docker.com/_/node
FROM node:13.12.0-alpine
# 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
# RUN npm install react-scripts#3.4.3 -g
# add app
COPY . /app
CMD ["npm", "start"]
I buiild:
$ docker build . -t myapp
Sending build context to Docker daemon 2.051MB
Step 1/7 : FROM node:13.12.0-alpine
---> 483343d6c5f5
Step 2/7 : WORKDIR /app
---> Using cache
---> c731f744759f
Step 3/7 : ENV PATH /app/node_modules/.bin:$PATH
---> Using cache
---> e1f5f853c8c6
Step 4/7 : COPY package.json ./
---> Using cache
---> f831911acf6c
Step 5/7 : RUN npm install
---> Using cache
---> c18449857dd2
Step 6/7 : COPY . /app
---> Using cache
---> 4b99412d4725
Step 7/7 : CMD ["npm", "start"]
---> Using cache
---> e565a34838d5
Successfully built e565a34838d5
Successfully tagged myapp:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Win dows Docker host. All files and directories added to build context will have '-r wxr-xr-x' permissions. It is recommended to double check and reset permissions f or sensitive files and directories.
Run:
docker run -p 3000 myapp
> myapp-fe#0.1.0 start /app
> react-scripts start
ℹ 「wds」: Project is running at http://172.17.0.3/
ℹ 「wds」: webpack output is served from
ℹ 「wds」: Content not from webpack is served from /app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...
Compiled successfully!
You can now view rugvista-fe in the browser.
Local: http://localhost:3000
On Your Network: http://172.17.0.3:3000
Note that the development build is not optimized.
To create a production build, use npm run build.
Visiting: http://localhost:3000/ gives ERR_EMPTY_RESPONSE
What am I doing wrong?

Add this in your docker file before npm start
EXPOSE 3000/tcp
Modify your COPY . /app to COPY . .

Related

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

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

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

Cannot find module error for a node js app running in a docker environment

This application is written in ES6 and compile to ES5 using babel. The build files stored in the following order inside the build folder. the dockerfile reside in the outside the build folder.
-build
---public
---server
---shared
---package.json
-Dockerfile
This is my docker file.
FROM node:8.9-alpine
ENV NODE_ENV production
WORKDIR /usr/src/app
ADD ./build/ /usr/src/app
RUN npm install --production --silent
RUN ls /usr/src/app/
RUN ls /usr/src/app/server/
EXPOSE 3000
CMD ["node", "/usr/src/app/server/index.js", "--config", "/usr/src/app/config.json"]
This is the output I get when running the image build command.
Sending build context to Docker daemon 16.08MB
Step 1/9 : FROM node:8.9-alpine
---> 406f227b21f5
Step 2/9 : ENV NODE_ENV production
---> Using cache
---> 090d3e742281
Step 3/9 : WORKDIR /usr/src/app
---> Using cache
---> fa2a344bd8ee
Step 4/9 : ADD ./build/ /usr/src/app
---> 93559e34a45a
Step 5/9 : RUN npm install --production --silent
---> Running in ebc2b6b2ba1a
added 212 packages in 15.212s
Removing intermediate container ebc2b6b2ba1a
---> b49180988a07
Step 6/9 : RUN ls /usr/src/app/
---> Running in b140530cb8ca
build-info.json
node_modules
package-lock.json
package.json
public
server
shared
Removing intermediate container b140530cb8ca
---> 0712e1b84e1a
Step 7/9 : RUN ls /usr/src/app/server/
---> Running in b6570fbc8d2c
auth
configuration
database
files
index.js
sessions
users
utils
Removing intermediate container b6570fbc8d2c
---> c49775551756
Step 8/9 : EXPOSE 3000
---> Running in 9318dfc0f183
Removing intermediate container 9318dfc0f183
---> ad20aec92973
Step 9/9 : CMD ["node", "/usr/src/app/server/index.js", "--config", "/usr/src/app/config.json"]
---> Running in fa6c2e388906
Removing intermediate container fa6c2e388906
---> ba1ced3be150
Successfully built ba1ced3be150
Successfully tagged tiqri:myclaim
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
After image created, i start the image using the following command.
"docker run -d -v C:\portal\environments\development:/usr/src/app/ -p 3000:80 f057e4bc8191"
There is volume map for config files.
But the container, not running. when I check the logs, it shows the following error.
C:\portal>docker logs c819d39fea61
module.js:540
throw err;
^
Error: Cannot find module '/usr/src/app/server/index.js'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
I couldn't figure out why giving this error even though all folders are in place in the docker container(RUN ls- shows the folder structure in the container).
still, I couldn't start the container because of this error.
UPDATE
Based on the answer given by #Marcos Casagrande, made changes to the docker file and container run command as follows. Now I'm getting a new error.
After made changes in the Dockerfile.
FROM node:8.9-alpine
ENV NODE_ENV production
WORKDIR /usr/src/app
ADD ./build/ /usr/src/app
RUN npm install --production --silent && mv node_modules ../
RUN mkdir /usr/src/app/environments
RUN mkdir /usr/src/app/environments/development
RUN ls /usr/src/app/
RUN ls /usr/src/app/server/
EXPOSE 3000
CMD ["node", "/usr/src/app/server/index.js", "--config", "/usr/src/app/environments/development/config.json"]
container run command changed as follows,
"docker run -d -v C:\portal\environments\development:/usr/src/app/environments/development -p 3000:80 ca290d67ff34"
Then I get the following error when checking the logs. The container does not start yet.
/usr/src/app/environments/development/config.json
/usr/src/app/environments/development/config.json
fs.js:646
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open '/usr/src/app/environments/development/config.json'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at loadJSON (/usr/src/node_modules/convict/lib/convict.js:364:25)
at /usr/src/node_modules/convict/lib/convict.js:501:17
at Array.forEach (<anonymous>)
at Object.loadFile (/usr/src/node_modules/convict/lib/convict.js:500:13)
at Object.<anonymous> (/usr/src/app/server/configuration/configuration.js:156:19)
at Module._compile (module.js:643:30)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
Without knowing the contents of C:\portal\environments\development I will assume that there isn't server/index.js in it.
I couldn't figure out why giving this error even though all folders
are in place in the docker container(RUN ls- shows the folder
structure in the container). still i couldnt start the container
because of this error.
RUN ls is executed after you copy the build dir, when building the docker image, if you run ls when you start the container you will see that there is no server/index.js
While you're copying the build folder to /usr/src/app/, you're overriding that files when mounting development folder on /usr/src/app.
The easiest way, is to mount C:\portal\environments\development on /usr/src/app/development.
docker run -d -v C:\portal\environments\development:/usr/src/app/development/ -p 3000:80 f057e4bc8191
Also you can mount development on /usr/src/app and then use the build/server that was build on the image:
-v C:\portal\environments\development:/usr/src/app/ -v /usr/src/app/server
You will have to do that for every folder that you need from the built image.
-v /usr/src/app/server -v /usr/src/app/public -v ...
I had a similar problem running a docker container. I figured out I might I have created the image wrongly and I resolved this by deleting the image and rebuilding. but first I had to delete the existing containers of the image like below
docker rm containerId
then I deleted the image like below
docker rmi imageId
then I rebuilt the image and ran to create a new container.

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