I have a Node.js application using Postgres and redis all inside docker-compose.
It runs perfectly when the volume is not specified in app container. I'm using fedora but when I try on Garuda Linux, there is no such error.
Here is the Dockerfile
FROM node:alpine
WORKDIR /app
RUN chown -R node:node /app
USER node
COPY package*.json ./
RUN yarn
COPY . .
CMD [ "yarn", "dev" ]
docker-compose.yml
version: "3.1"
services:
app:
build: .
container_name: app
ports:
- "8000:8000"
depends_on:
- postgres
- redis
links:
- postgres
- redis
volumes:
- .:/app
- /app/node_modules
postgres:
image: postgres:14.1-alpine
container_name: database
restart: always
ports:
- "5432:5432"
env_file:
- .env
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- db_data:/var/lib/postgresql/data
redis:
image: redis:alpine
container_name: redis
ports:
- "6379:6379"
volumes:
db_data:
Error:
app | Error: EACCES: permission denied, open '/app/package.json'
app | at Object.openSync (node:fs:585:3)
app | at Object.readFileSync (node:fs:453:35)
app | at onUnexpectedError (/opt/yarn-v1.22.19/lib/cli.js:88615:106)
app | at /opt/yarn-v1.22.19/lib/cli.js:88734:9
Related
When deploying a project, the web container crashes with the No module named 'api_yamdb.wsgi' error. Already spent a lot of time but could not figure out what exactly needs to be done to solve this problem. Please help!)
My Dockerfile:
FROM python:3.7-slim
WORKDIR /app
COPY ./api_yamdb/requirements.txt .
RUN pip3 install -r requirements.txt --no-cache-dir
COPY . .
CMD ["gunicorn", "api_yamdb.wsgi:application", "--bind", "0:8000" ]
My docker-compose:
version: '3.8'
services:
db:
image: postgres:13.0-alpine
volumes:
- /var/lib/postgresql/data/
env_file:
- ./.env
web:
image: porter4ch/yamdb_final:latest
restart: always
volumes:
- static_value:/app/static/
- media_value:/app/media/
depends_on:
- db
env_file:
- ./.env
nginx:
image: nginx:1.21.3-alpine
ports:
- "80:80"
volumes:
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
- static_value:/var/html/static/
- media_value:/var/html/media/
depends_on:
- web
volumes:
static_value:
media_value:
Here is my docker-compose.yml, when I comment the volumes code of "khaothi-manager" my services work correctly. But when uncomment it, my Node service throw an error that it can not connect to Mongo
version: "3.8"
services:
mongo:
image: mongo
restart: always
env_file: ./.env
ports:
- $MONGO_LOCAL_PORT:$DB_PORT
volumes:
- ./data:/data/db
networks:
- hm_khaothi
khaothi-manager:
container_name: khaothi-manager
image: khaothi-manager
restart: always
volumes:
- ./admin:/app
build: ./admin
env_file: ./.env
links:
- mongo
- khaothi-resource
ports:
- $MANAGER_PORT:$MANAGER_PORT
environment:
- MANAGER_HOST=$MANAGER_HOST
- MANAGER_PORT=$MANAGER_PORT
- RESOURCE_HOST=khaothi-resource
- RESOURCE_PORT:$RESOURCE_PORT
- DB_HOST=mongo
- DB_NAME=$DB_NAME
- DB_PORT=$DB_PORT
networks:
- hm_khaothi
My Dockerfile
# syntax=docker/dockerfile:1
FROM node:14-alpine
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
This is the error
(node:37) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connection timed out
at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:807:32)
at /app/node_modules/mongoose/lib/index.js:342:10
...
It worked correctly when I add another volume /app/node_modules
khaothi-manager:
container_name: khaothi-manager
image: khaothi-manager
restart: always
volumes:
- ./admin:/app
- /app/node_modules
I am getting a 'connection refused' error when trying to hit my NodeJS server running in a Docker container. If I try to cURL the server from the host machine, I get the error "curl: (56) Recv failure: Connection reset by peer".
When I run sudo docker-compose up -d it starts up my services and running sudo docker ps -a shows the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e56b30b1de9c mongo:4.1.8-xenial "docker-entrypoint.s…" About a minute ago Up About a minute 27017/tcp db
0d82b0a881e5 nodejs "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3000->3000/tcp nodejs
The containers are running on a server with IP '192.168.0.24' so I try to hit an endpoint in my 'nodejs' app via '192.168.0.24:3000/items' but this results in a connection refused error.
docker-compose.yml
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile
image: nodejs
container_name: nodejs
restart: unless-stopped
env_file: .env
environment:
- MONGO_USERNAME=$MONGO_USERNAME
- MONGO_PASSWORD=$MONGO_PASSWORD
- MONGO_HOSTNAME=db
- MONGO_PORT=$MONGO_PORT
- MONGO_DB=$MONGO_DB
ports:
- "3000:3000"
volumes:
- .:/home/node/app
- /home/node/app/node_modules
networks:
- app-network
command: ./wait-for.sh db:27017 -- /home/node/app/node_modules/.bin/nodemon app.js
db:
image: mongo:4.1.8-xenial
container_name: db
restart: unless-stopped
env_file: .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
volumes:
- dbdata:/data/db
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
dbdata:
node_modules:
Dockerfile
FROM node:10-alpine
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 3000
CMD [ "npm", "app.js" ]
I am having the connection error with the docker-composer when I try to access a postgres database through an API in Node.
I'm using Sequelize as ORM to acess the database. But I'dont know what happened.
docker-compose.yml:
version: '3.5'
services:
api-service:
build:
context: .
dockerfile: ./api-docker.dockerfile
image: api-service
container_name: api-service
restart: always
env_file: .env
environment:
- NODE_ENV=$NODE_ENV
ports:
- ${PORT}:3000
volumes:
- .:/home/node/api
- node_modules:/home/node/api/node_modules
depends_on:
- postgres-db
networks:
- api-network
command: npm run start:dev
postgres-db:
expose:
- ${PORT_SERVICE}
ports:
- ${PORT_SERVICE}:5432
restart: always
env_file: .env
volumes:
- pgReportData:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${USER_SERVICE}
POSTGRES_PASSWORD: ${PASSWORD_SERVICE}
POSTGRES_DB: ${DATABASE_SERVICE}
networks:
- api-network
container_name: postgres-db
image: postgres:10
networks:
api-network:
driver: bridge
volumes:
pgReportData:
driver: local
node_modules:
.env:
NODE_ENV=development
PORT=30780
HOST_SERVICE=postgres-db
DATABASE_SERVICE=base
USER_SERVICE=user
PASSWORD_SERVICE=password
DIALECT=postgres
PORT_SERVICE=5444
api-docker.dockerfile:
FROM node:12
WORKDIR /src
COPY . .
COPY --chown=node:node . .
USER node
RUN npm install
EXPOSE $PORT
ENTRYPOINT ["npm", "run", "start:dev"]
And when I run:
docker-compose up
I'm getting this error:
Any ideia?
Can someone help me ??
If node is the only app that connects to postgre-db you can remove the networks, and expose the postgredb running port (5432). To connect to the db you can simply use the container name as host.
Connection String: "postgres://YourUserName:YourPassword#postgres-db:5432/YourDatabase";
version: '3.5'
services:
api-service:
build:
context: .
dockerfile: ./api-docker.dockerfile
image: api-service
container_name: api-service
restart: always
env_file: .env
environment:
- NODE_ENV=$NODE_ENV
ports:
- ${PORT}:3000
volumes:
- .:/home/node/api
- node_modules:/home/node/api/node_modules
depends_on:
- postgres-db
command: npm run start:dev
postgres-db:
expose:
- 5432
restart: always
env_file: .env
volumes:
- pgReportData:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${USER_SERVICE}
POSTGRES_PASSWORD: ${PASSWORD_SERVICE}
POSTGRES_DB: ${DATABASE_SERVICE}
container_name: postgres-db
image: postgres:10
volumes:
pgReportData:
driver: local
node_modules:
-- Docker FIle
FROM node:4.4.2
EXPOSE 3000
ENV NODE_ENV test
WORKDIR /app
COPY ["package.json", "npm-shrinkwrap.json*", "./"]
RUN npm install --silent
COPY . /app
CMD node server.js
-- DOCKER-COMPOSE
version: "2"
services:
postgresql:
image: postgres:9.5.21
container_name: postgresql
volumes:
- '$HOME/DOCKER_VOLUME/db-data1:$HOME/DOCKER_VOLUME/var/lib/postgres'
environment:
POSTGRES_PASSWORD: <PWD>
POSTGRES_DB: <DB>
POSTGRES_USER: <UERS>
ports:
- '5432:5432'
redis:
image: redis
container_name: redis
ports:
- '6379:6379'
broker:
image: eclipse-mosquitto
container_name: mosquitto
volumes:
- $HOME/DOCKER_VOLUME/etc/mosquitto:$HOME/DOCKER_VOLUME/etc/mosquitto:ro
- $HOME/DOCKER_VOLUME/var/log/mosquitto:$HOME/DOCKER_VOLUME/var/log/mosquitto:rw
ports:
- '1883:1883'
app:
container_name: ms_user_manager
environment:
NODE_ENV: test
build: .
volumes:
- .:/app
- /app/node_modules
depends_on:
- postgresql
- redis
- broker
ports:
- '3000:3000'
I am able to access postgres, redis outside of container but nodejs app unable to connect to both & giving error
Redis - connection to 0.0.0.0:6379 failed - connect ECONNREFUSED 0.0.0.0:6379"
Postgres - Error: connect ECONNREFUSED 0.0.0.0:5432
So any idea, what I am doing wrong or what i missed.
Your Node.js app should try to connect to redis:6379. Change this in your application configuration. At the moment it looks like it is trying to connect to localhost (which works probably when you run your server locally on the host).