Dockerizing react app - node.js

The build folder is ready to be deployed.
You may serve it with a static server:
serve -s build
---> b252a9088991
Removing intermediate container cb5c1e2629c9
Step 16/16 : RUN serve -s build
---> Running in c27b54b31108
serve: Running on port 5000
created and dockerized react application using react-app and getting output like above on "docker-compose up" command
but nothing is showing on http://0.0.0.0:5000/ or http://localhost:5000/
version: '3'
services:
web:
build: .
image: react-cli
container_name: react-cli
volumes:
- .:/app
ports:
- '3000:3000'
above my docker-compose.yml file
FROM scratch
FROM mhart/alpine-node:6.12.0
RUN npm install -g npm --prefix=/usr/local
RUN ln -s -f /usr/local/bin/npm /usr/bin/npm
CMD [ "/bin/sh" ]
ENV NPM_CONFIG_LOGLEVEL warn
RUN npm install -g serve
CMD serve -s build
EXPOSE 3000
COPY package.json package.json
COPY semantic.json semantic.json
COPY npm-shrinkwrap.json npm-shrinkwrap.json
RUN npm install gulp-header --save-dev
RUN npm install --no-optional
COPY . .
RUN npm run build --production
RUN serve -s build
and this is my Dockerfile

Most likely you are not exposing container port on host. I don't see how your compose file looks, but you probably need to add
ports:
- "5000:5000"
To your container definition in compose file.

Related

Dev dependencies not getting installed in docker container

I am new to docker. I am trying to create a container for react and express and run both the containers on same network using docker compose.
Below is my dockerfile for frontend:
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm","run","start"]
Below is my dockerfile for backend
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN NODE_ENV=development npm install
COPY . .
EXPOSE 5000
CMD ["npm","run","server"]
Below is my docker-compose.yml
version: '3'
services:
client:
build:
context: './frontend'
dockerfile: Dockerfile
ports:
- 3000:3000
container_name: react_cont
environment:
- WATCHPACK_POLLING=true
networks:
- mern
volumes:
- ./frontend:/app
depends_on:
- server
server:
build:
context: './backend'
dockerfile: Dockerfile
ports:
- 5000:5000
container_name: express_cont
networks:
- mern
volumes:
- ./backend:/app
networks:
mern:
react container is getting is created and running successfully but the express container is not getting created with an error
sh: nodemon: not found
I had installed nodemon as my dev dependency.
Any help is appreciated. Thanks in advance.
Answering my own Question.
I had installed nodemon globally in my machine but forgot to install it as a dependency for my current project.
Since nodemon was installed globally in my machine i was not getting any errors while i was trying to run my server.js using nodemon. nodemon server.js in scripts did not throw any error while i was in developing my project locally prior moving it to docker container.
But since neither my package.json had nodemon as my dependency and i had not installed it separately in my container nodemon did not get installed and it gave me error.
You can try to delete node_modules folder in your source code and add flag --production=false explicitly to the npm install command. I think it's caching problem.
You may need to install nodemon package globally in your Docker:
RUN NODE_ENV=development npm install && npm --global install nodemon

Docker container with nestjs app exits without any error

I am trying to dockerize nestjs application. I have to use approach of our devops, so I can't give all details of configuration.
Scripts in package.json typical for any nestjs application.
I have Dockerfile.backend:
FROM some.registry.net/docker/node16 as builder
WORKDIR /opt/app
COPY --chown=app:app ./nestjs/nest-project .
RUN yarn install --non-interactive --production --frozen-lockfile
FROM some.registry.net/docker/node16 as serve
WORKDIR /opt/app
ENV NODE_ENV=production
ENV APP_CONFIG=/opt/app/config/config.yaml
COPY --chown=app:app ./build/Procfile /opt/startup/Procfile
COPY --chown=app:app ./build/config.yaml ./config/config.yaml
COPY --chown=app:app --from=builder /opt/app/ ./
COPY --chown=app:app --from=builder /opt/app/node_modules ./node_modules
USER root
##RUN npm install pm2 -g
##RUN npm install -g nodemon
RUN npm run build
CMD ["/opt/startup/entrypoint.sh"]
And I have docker-compose.yml file:
version: "2"
services:
backend:
build:
context: .
dockerfile: ./build/Dockerfile.backend
command: npm run start
##volumes:
##- ./nestjs/nest-project:/app
##- /app/node_modules
ports:
- 4001:4001
- 9229:9229
environment:
- NODE_ENV=development
- PORT=4001
- REACT_APP_PROD=0
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001
- FRONTEND_URL=http://localhost:4000
- APP_CONFIG=/opt/app/config/config.yaml
frontend:
build: ./frontend
command: npm start
##volumes:
##- ./frontend:/app
##- /app/node_modules
environment:
- NODE_ENV=development
- DISABLE_ESLINT_PLUGIN=true
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001/backend
- PORT=4000
- REACT_APP_PROD=0
ports:
- 4000:4000
So the most interesting point lies in backend command part. I am able to start container only with npm run start:dev command. When I enter there npm start or npm run start:prod
container executes (I see in logs that nestjs app starts, successfully connects to database) and exits without any errors. I tried node dist/main and got the same result. I tried nodemon, with nodemon dist/main nodemon, even with verbose flag shows red line something like app crashed... and gives no more information. I tried pm2 with this command pm2 --name nestjs start npm -- start pm2 successfully starts and container exits without any information.
So far I see problem lies somewhere in my configuration, but I have no clue where to seek. Thanks in advance.

docker-compose throws error with getaddrinfo EAI_AGAIN registry.npmjs.org

background:
I'm on Ubuntu 20.04.
I'm trying to build images using docker-compose.
This is my docker-compose file
version: "3.8"
services:
web:
build: ./frontend
network_mode: "host"
ports:
- 3000:3000
api:
build: ./backend
network_mode: "host"
ports:
- 3001:3001
environment:
DB_URL: mongodb://db/foo-app
db:
image: mongo:4.0-xenial
ports:
- 27017:27017
volumes:
- foo-app-data:/data/db
volumes:
foo-app-data:
And below are my two Dockerfile files:
# ./backend file
FROM node:16.3.0-alpine3.13
RUN addgroup app && adduser -S -G app app
WORKDIR /app
USER root
COPY package*.json ./
# --- debug try
RUN npm cache clean --force
RUN npm config set registry https://registry.npmjs.org/
RUN npm install -g #angular/cli
# ---
RUN npm install
COPY . .
RUN chown app:app /app
USER app
EXPOSE 3001
CMD ["npm", "start"]
# ./frontend file
FROM node:16.3.0-alpine3.13
RUN addgroup app && adduser -S -G app app
WORKDIR /app
USER root
COPY package*.json ./
# --- debug try
RUN npm cache clean --force
RUN npm config set registry https://registry.npmjs.org/
# ---
RUN npm install
COPY . .
RUN chown app:app /app
USER app
EXPOSE 3000
CMD ["npm", "start"]
error:
When I run docker-compose build, the below error is thrown:
Step 8/14 : RUN npm install -g #angular/cli
---> Running in ce221adb18f6
npm ERR! code EAI_AGAIN
npm ERR! syscall getaddrinfo
npm ERR! errno EAI_AGAIN
npm ERR! request to https://registry.npmjs.org/#angular%2fcli failed, reason: getaddrinfo EAI_AGAIN registry.npmjs.org
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-06-13T10_19_13_600Z-debug.log
The command '/bin/sh -c npm install -g #angular/cli' returned a non-zero code: 1
ERROR: Service 'web' failed to build : Build failed
what i have tried so far:
when I was building each docker file manually, I was getting the same error, until I used the --network=host. It means, when I build the images with docker build --network=host, it works fine. So I tried to add network_mode= "host" to my docker-compose file but it doesn't solve the issue.
and for God's sake, read this before marking this question as duplicate:
this post here propose a solution for docker and not docker compose. When I ping registry.npmjs.org, the connection works fine.
this post here propose to docker-compose up, which will throw the exact same error as I have here.
this post here doens't work, i have already restarted docker multiple times. And on top of that, I clean all docker images after the error is thrown to make sure the next time I build, nothing is used from the cache.
this post here doesn't work either. I tried to (1) remove the proxies from the npm config, and also add the additional lines npm cache clean --force and npm config set registry https://registry.npmjs.org/ in my Dockerfile. Nothing works.
this post here not only doesn't solve the problem, but also it doesn't really explain well the reason why the solution is being proposed.
and this post here i don't even know how this answer is allowed on StackOverflow.
If you use network_mode with host value, you can't mix it with port mapping: https://docs.docker.com/compose/compose-file/compose-file-v3/#ports
Then, or you change your network_mode to bridge or you drop your port mapping for each service.
Add following Variable to .zshrc file in mac
export NODE_OPTIONS=--openssl-legacy-provider

add dependencies to package.json docker

I am currently running a nodejs express app in docker.
docker-compose.yml
# networks and nginx ...
api:
build:
context: ./api
networks:
- back-tier
volumes:
- ./api:/usr/src/api
- /usr/src/api/node_modules
Dockerfile
FROM node:lts-alpine
RUN npm install -g nodemon
WORKDIR /usr/src/api
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "nodemon" ]
The problem here is the volume is mounted and I can't modify package.json from the host as the docker "owns" package.json.
However if I add a dependency inside the docker with docker run -it dockername_api /bin/sh then doing npm i package, the package.json does not update in the host side.
The solution now is to manually copy package.json from the container back to host after adding a new dependency? Another way of doing it is to stop the docker, do npm i package on the host, then rebuild the docker, which is very cumbersome.
Thanks

docker compose run with command error

From this documentation, it seems like that I can execute a single command from a service like this:
docker-compose run SERVICE CMD
But when I run
docker-compose up pwa npm test
I get the error
ERROR: No such service: npm
From my configurations, it will execute npm start, but I'd like to know how to execute other commands.
Files
Dockerfile:
From node:8
WORKDIR /app
copy package.json /app/
RUN npm install --quiet
CMD npm start
docker-compose.yml:
version: '3'
services:
pwa:
build: .
ports:
- '3000:3000'
volumes:
- ./src:/app/src
- ./public:/app/public
Versions
Docker version: 17.03
Docker compose version: 1.11.2
As docs say, the command is docker-compose run, not docker-compose up. The later expects all service names.
Do as this:
docker-compose run pwa npm test

Resources