expo build:web docker cannot find correct entry point - node.js

NOOB to building an expo web app. I’m trying to build a docker container with my expo web app. After I run “expo build:web” and the build successfully finishes, I get a run error when I try to execute.
I have a custom entry point defined in my app.json: “entryPoint”: “./index.js”,
When I run the app in the docker container and connect to http://localhost:19006 I get:
./node_modules/expo/AppEntry.js:3
Module not found: Can't resolve '../../App'
I can see the entry point defined in the “asset-manifest.json” file. But I don’t know the steps to call that as my starting point and why it’s trying to use “../../App” instead.
Here is my Dockerfile if it helps:
FROM node:12.20.2 as build
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
ARG PORT=19006
ENV PORT $PORT
EXPOSE $PORT 19001 19002
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV PATH /home/node/.npm-global/bin:$PATH
RUN npm i --unsafe-perm -g npm#latest expo-cli#latest sharp-cli
RUN mkdir /opt/web && chown node:node /opt/web
WORKDIR /opt/web
ENV PATH /opt/web/.bin:$PATH
USER node
COPY package.json ./
COPY .env.production ./.env
COPY ./private ./private
RUN yarn install --silent
RUN ls -al
WORKDIR /opt/web/app
COPY ./web-build .
RUN ls -al
ENTRYPOINT ["npm", "run"]
CMD ["web"]

I figured it out!
In my package.json file, I had the line:
"main": "node_modules/expo/AppEntry.js",
I changed it to:
"main": "index.js",
This worked in yarn start mode but not in production so I never gave it another look.
I also had to change the docker file a little by using "serve":
FROM node:12.20.2 as build
ARG NODE_ENV=production
ENV NODE_ENV $NODE_ENV
ARG PORT=19006
ENV PORT $PORT
EXPOSE $PORT 19001 19002
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV PATH /home/node/.npm-global/bin:$PATH
RUN npm i --unsafe-perm -g npm#latest expo-cli#latest serve
RUN mkdir /opt/web && chown node:node /opt/web
WORKDIR /opt/web
ENV PATH /opt/web/.bin:$PATH
USER node
COPY package.json ./
COPY .env.production ./.env
COPY ./private ./private
RUN yarn install --silent
RUN ls -al
WORKDIR /opt/web/app
COPY ./web-build .
RUN ls -al
CMD ["serve","--no-port-switching","-p","19006"]

Related

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

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

npm ERR! missing script: serve

Getting error while starting the docker container. I am using nodemon to listen to the file changes.
DockerFile
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm","run","serve"]
Package.json
{
"dependencies": {
"express": "*",
"nodemon": "*"
},
"scripts": {
"serve": "nodemon index.js",
"start": "node index.js"
}
}
build command
docker build -f Dockerfile.dev -t test/nodeapp1 .
cmdLine docker cmd ->
docker run -p 3000:8080 -v /app/node_modules -v pwd:/app test/nodeapp1.
Iam new to docker, and not able to figure out the cause.
Make this changes in your dockerfile
FROM node:alpine
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
ENV HOME=/home/node/app
ENV PATH="/home/node/.npm-global/bin:${PATH}"
USER node
RUN npm install -g nodemon
RUN mkdir -p ${HOME}
WORKDIR ${HOME}
ADD package.json ${HOME}
RUN cd ${HOME} && npm install
CMD [ "npm" ,"run", "serve" ]
Build the docker container
docker build -f Dockerfile -t prac/nodeApp .
Run the docker container
docker run -p 3000:8080 -v /app/node_modules -v pwd:/app prac/nodeApp
Changing the WORKDIR to a new value worked.
FROM node:alpine
WORKDIR '/dir'
COPY package.json .
RUN npm install
COPY . .
CMD [ "npm" ,"run", "serve" ]
Your docker run -v options are wrong. You probably actually meant to write
docker run ... -v $PWD:/app ...
docker run ... -v $(pwd):/app ...
to use the current directory (from the PWD environment variable or from the pwd command, respectively) as a bind mount.
I tend to not recommend this pattern, especially for Node applications where the host dependencies are minimal and you're not interacting much with other containers. It's probably easier to just install Node locally (if you don't already have it) and do live development against that; when you want to use Docker to deploy your application, use the version you've COPYed into the image, and don't separately use a -v option to inject your code over it.

Running gulp in Docker compose - does not create files

I have issues where gulp is not making any files. It says finished, but no file is being created.
If I log in to my docker instance using:
docker exec -t -i myservice-service /bin/bash
and if I run the gulp command, then it creates it properly
Then all the files defined in the gulpfile.js are created. In other words, public/dist/ is populated with the main.js and other css files.
This is my Dockerfile.
FROM node:9
RUN mkdir -p /usr/src/app
RUN mkdir -p /usr/src/logs
WORKDIR /usr/src/app
# GULP Installation
RUN npm install -g gulp
RUN npm install gulp
COPY package*.json /usr/src/app/
COPY .npmrc /usr/src/app/
RUN cd /usr/src/app/ && npm install && npm install -g nodemon
COPY . /usr/src/app
RUN chown -R node:node /usr/src/app && chown -R node:node /usr/src/logs
USER node
EXPOSE 3000
RUN gulp
CMD ["npm", "run-script", "start" ]
And this is my composer file (development):
version: "3"
services:
myservice-service:
build: .
image: myservice-service
container_name: myservice-service
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
environment:
- NODE_ENV=dev
ports:
- 3000:3000
command: nodemon --delay 2 ./bin/www
I run it as:
docker-compose -f docker-compose.development.yml up --build
When I run it like that, it does not create any files. I get the same output on the screen, when I run the command manually.
I have spent hours trying to make it work, I tried with setting permissions and what not, but it just does not work.
My expectation was to have public/dist/ populated with files.
Any help is appreciated.
UPDATE. It works, but I have doubts:
I manage to make it work by using command inside the composerfile itself.
So in my case:
command: bash -c "gulp && nodemon --delay 2 ./bin/www"
In my reasoning, gulp should be done inside the Dockerfile itself, not on the composer files. But then again, it is out of my scope of knowledge.
The Dockerfile is run at build time and will COPY all the files in your local directory into the container, then run gulp and create any files.
You then mount the local folder over the docker containers file system, pretty much overwriting what was done in the docker file with the original files, as gulp ran on the files in the container, it did not effect the original files so you are undoing the changes.
The solutions are either to do as as you have mentioned in your question (add it to the command in docker-compose.yml or run it via docker-compose exec) or write a custom entrypoint script that will run gulp and then the command, something like:
bin/entrypoint.sh
#!/bin/sh
gulp
exec "$#"
Dockerfile
FROM node:9
COPY bin/entrypoint.sh /entrypoint.sh
RUN chmod 755 /entrypoint.sh
RUN mkdir -p /usr/src/app
RUN mkdir -p /usr/src/logs
WORKDIR /usr/src/app
# GULP Installation
RUN npm install -g gulp
RUN npm install gulp
COPY package*.json /usr/src/app/
COPY .npmrc /usr/src/app/
RUN cd /usr/src/app/ && npm install && npm install -g nodemon
COPY . /usr/src/app
RUN chown -R node:node /usr/src/app && chown -R node:node /usr/src/logs
USER node
EXPOSE 3000
ENTRYPOINT ["/entrypoint.sh"]
CMD ["npm", "run-script", "start" ]
This will make your build a little less predictable though as it will run gulp each time the container starts (e.g. after every deployment) if you use the same Dockerfile in dev and production.

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