version: '3'
services:
postgres:
image: postgres
environment:
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_DATABASE}
ports:
- "5674:5432"
volumes:
- ./data/db_data:/var/lib/postgresql/data
redis_cache:
image: bitnami/redis
ports:
- "6379:6379"
environment:
- ALLOW_EMPTY_PASSWORD=yes
app:
image: asobooks-api
ports:
- 3000:3000
environment:
PORT: 3000
NODE_ENV: prod
DB_CONNECTION: postgres
DB_HOST: postgres
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
DB_DATABASE: ${DB_DATABASE}
DB_PORT: 5432
REDIS_PORT: 6379
depends_on:
- postgres
- redis_cache
command:
[
'node',
'dist/src/main.js'
]
I have been trying this for about 2 days now and unfortunately, the app container fails to connect to Redis.
Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
Here is the connection.
BullModule.registerQueue({
name: Queues.EMAIL,
redis: {
port: 6379,
host: 'redis_cache',
},
})
Please help me with this thing it works without using docker. But fails when I run in containers. Thanks.
Related
mongoose couldn't authenticate with docker mongodb container.
Note: mongo is in docker and my API app out of the docker.
docker-compose:
version: "3.7"
services:
db:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
MONGO_INITDB_DB: "${MONGO_DB}"
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
volumes:
mongo_data:
.env:
MONGO_USERNAME=root
MONGO_PASSWORD=123456
MONGO_DB=nodeApp
db.js (database connection file):
(async () => {
try {
const uri = `mongodb://127.0.0.1:27017/${process.env.MONGO_DB}`;
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
user: process.env.MONGO_USERNAME,
pass: process.env.MONGO_PASSWORD,
});
console.log("Database connection completed successfully");
} catch (error) {
console.error(error);
}
})();
and finally this is what I got from console:
I can connect to mongo with mongoose with username and password.
Docker only supports using default environment variables to be used like what you did in your code.
https://docs.docker.com/compose/env-file/
Docker added support to env filed since version 1.3 (as I remember). The soloution is to use env_file key in your yml file.
services:
db:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
MONGO_INITDB_DB: "${MONGO_DB}"
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
env_file:
- mongo_variables.env
Instead of localhost use your ip on database configuration.
Replace this DBurl=mongodb://localhost:27017/
to this one DBurl=mongodb://X.X.X.X:27017/ ( X.X.X.X means your ip address )on your database configuration file.
On your docker-compose.yml file
Inside of services of api add CONNECTIONSTRING.
version: "3.7"
services:
api:
build: node-server ## as i am using node
ports:
- 3000:3000
environment:
- CONNECTIONSTRING=mongodb://X.X.X.X:27017/ ##X.X.X.X means your ip address
db:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: "${MONGO_USERNAME}"
MONGO_INITDB_ROOT_PASSWORD: "${MONGO_PASSWORD}"
MONGO_INITDB_DB: "${MONGO_DB}"
ports:
- 27017:27017
volumes:
- mongo_data:/data/db
volumes:
mongo_data:
you see that in the inspector docker -> port of your container -> (0.0.0.0:27017)
mongodb://0.0.0.0:27017/
;)
I have a NodeJS Application that connects to a MongoDB server.
Both the node application and MongoDB server are served in a docker container (with docker-compose)
docker-compose.yml:
version: '3'
services:
redis:
image: "redis:alpine"
ports:
- "6379:6379"
expose:
- 6379
restart:
always
container_name: redis-server
mongo:
image: "mongo"
command: mongod --bind_ip_all --replSet rs8
volumes:
- c:\mongo\data:/data/db
ports:
- "27017:27017"
expose:
- 27017
restart:
always
container_name: mongo-server
accounts-service:
depends_on:
- redis
- mongo
build:
context: .
dockerfile: GenericNodeJSDockerfile
container_name: accounts-service
ports:
- "8001:8001"
In the node app, the connection in mongo looks like this:
const m = require('mongoose')
const connectionConfig = {
useUnifiedTopology: true,
useNewUrlParser: true,
};
let connectionString = 'mongodb://mongo:27017/mydb/replicaSet=rs8';
m.connect(connectionString , connectionConfig).then(_ => {
console.log("Connected to MongoDB")
}).catch(err => {
console.log("Failed connecting to MongoDB: " + err);
});
And the error that is thrown is:
Failed connecting to MongoDB: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
But when I'm commenting the row contains useUnifiedTopology option, the connection succeeds.
Does anyone have an idea how to fix it?
It turns out that I had a wrong hosts file configuration that made this issue.
I'm setting up a app with docker, mongodb and nodejs (mongoose), but for some reason I'm getting a authentication error.
UserNotFound: Could not find user "testuserdb" for db "admin"
Any suggestion?
Thanks!
.env
MONGO_USERNAME=testuserdb
MONGO_PASSWORD=testuserpass
MONGO_PORT=27017
MONGO_DB=testdb
db.js (mongoose connection)
const mongoose = require('mongoose');
const {
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_HOSTNAME,
MONGO_PORT,
MONGO_DB
} = process.env;
const options = {
useNewUrlParser: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
connectTimeoutMS: 10000,
};
const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}#${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;
mongoose.connect(url, options).then( function() {
console.log('MongoDB is connected');
})
.catch( function(err) {
console.log(err);
});
docker compose
I use a docker environment variables to mongodb and nodejs to define the username, password, hostname db, and databasename
version: '3'
services:
nodejs:
build:
context: .
dockerfile: Dockerfile.dev
depends_on:
- db
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
- node_modules:/home/node/app/node_modules
networks:
- app-network
#command: ./wait-for.sh db:27017 -- /home/node/app/node_modules/.bin/nodemon app.js
command: /home/node/app/node_modules/.bin/nodemon app.js
db:
image: mongo:4.2.9-bionic
container_name: db
restart: unless-stopped
env_file:
- .env
environment:
- MONGO_INITDB_ROOT_USERNAME=$MONGO_USERNAME
- MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASSWORD
- MONGO_INITDB_DATABASE=$MONGO_DB
volumes:
- dbdata:/data/db
networks:
- app-network
networks:
app-network:
driver: bridge
volumes:
dbdata:
node_modules:
As per the information available. I think the user testuserdb exist on testdb and not on admin database.
And while defining connection url you have mentioned authSource=admin
Change one of these as per your needs.
I'm building an app running on NodeJS using postgresql. I'm using SequelizeJS as ORM. I have been trying to dockerize my app but get this error message:
SequelizeConnectionRefusedError: connect ECONNREFUSED 172.21.0.2:5432
My docker-compose:
version: '3'
services:
db:
image: postgres
restart: always
volumes:
- ./pgdata:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: sAjs21312ls
POSTGRES_DB: dev-player
ports:
- "5432:5432"
app:
build: .
environment:
DB_USER: postgres
DB_PASSWORD: sAjs21312ls
DB_NAME: dev-player
depends_on:
- db
ports:
- "8090:8090"
My sequelize connection:
const connection = new sequelize(
'dev-player',
'postgres',
'sAjs21312ls',
{
host: 'db',
port: 5432,
dialect: 'postgres'
}
);
I have a node app which connects to NATS server. Below is my docker-compose.yml for both the servers.
version: "3"
services:
app:
image: node:12.13.1
volumes:
- ./:/app
working_dir: /app
depends_on:
- mongo
- nats
environment:
NODE_ENV: development
ports:
- 3000:3000
command: npm run dev
mongo:
image: mongo
expose:
- 27017
volumes:
- ./data/db:/data/db
nats:
image: 'nats:2.1.2'
# entrypoint: "/gnatsd -DV"
# entrypoint: "/nats-server -p 4222 -m 8888 "
expose:
- "4222"
ports:
- "8222:8222"
hostname: nats-server
app.post('/pets', function(req, res, next) {
let nats = NATS.connect('nats://nats-server:4222');
nats.publish('foo', 'Hello World!');
res.json({ stats: 'success' });
});
The above nodejs snippet gives me:
NatsError: Could not connect to server: Error: getaddrinfo ENOTFOUND nats-server
If I use let nats = NATS.connect() it gives me:
NatsError: Could not connect to server: Error: connect ECONNREFUSED 127.0.0.1:4222
Kindly throw me some ideas on how to resolve this issue.
Thanks.
let nats = NATS.connect('nats://nats:8222');
you need to use the name of the container, and attach the internal port