I connected with docker redis container.The redis working in the docker.If I execute the docker file with docker exec -it 96e199a8badf sh, I connected to redis server.
My node.js application like this.I use redis 4.1.0 version.
I don't know, what's going on.How can I fix this?
docker-compose.yml
version: '3'
services:
app:
container_name: delivery-app
build:
dockerfile: 'Dockerfile'
context: .
ports:
- "3000:3000"
volumes:
- .:/app,
- '/app/node_modules'
networks:
- redis
redis:
image: "redis"
ports:
- "6379:6379"
networks:
- redis
networks:
redis:
driver: bridge
Dockerfile
FROM node:16-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . ./
EXPOSE 3000
CMD ["node","server.js"]
code:
const redisClient = redis.createClient({
socket: {
port: 6379,
host: "redis"
}
});
await redisClient.connect();
redisClient.on('connect',function(){
return res.status(200).json({
success: true
})
}).on('error',function(error){
return res.status(400).json({
success: false
})
});
package.json
"redis": "^4.1.0"
I had the same issue and solved it by passing the host parameter as a socket object in createClient like this
{
socket: {
host: "redis"
}
}
This seems to be new because before you didn't have to use the socket object
Here's the documentation for node-redis createClient
https://github.com/redis/node-redis/blob/HEAD/docs/client-configuration.md
Related
I'm trying to start up my postgresql with typeorm on a node.js server and I'm getting this error: caught error # main DriverPackageNotInstalledError: Postgres package has not been found installed. Try to install it: npm install pg --save
I've checked pg is installed in my docker container
deps versions:
"pg": "^8.8.0",
"typeorm": "^0.2.34",
My docker-compose:
version: '3.7'
services:
api:
profiles: ['api', 'web']
container_name: slots-api
stdin_open: true
build:
context: ./
dockerfile: api/Dockerfile
target: dev
environment:
NODE_ENV: development
ports:
- 8000:8000
- 9229:9229 # for debugging
volumes:
- ./api:/app/api
- /app/node_modules/ # do not mount node_modules
- /app/api/node_modules/ # do not mount node_modules
depends_on:
- database
command: yarn dev:api
database:
container_name: slots-database
image: postgres:alpine
restart: unless-stopped
ports:
- 5432:5432
depends_on:
- adminer
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
POSTGRES_DB: dev
POSTGRES_HOST: 127.0.0.1
my dockerfile:
FROM node:16-alpine AS builder
WORKDIR /app
COPY package*.json ./
COPY yarn.lock ./
RUN yarn install
FROM node:16-alpine AS dev
WORKDIR /app
COPY --from=builder /app/ /app/
COPY . .
RUN yarn build:api
EXPOSE 8000
CMD ["yarn", "dev"]
My typeorm config:
import { Connection, createConnection, DatabaseType } from 'typeorm';
import * as entities from '#/entities';
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
export const shouldCache = (): boolean => {
return !['test', 'development'].includes(process.env.NODE_ENV ?? '');
};
export default async function postgresConnection(): Promise<Connection> {
const config = {
database: 'dev',
entities: Object.values(entities),
host: '127.0.0.1',
password: 'admin',
port: 5432,
type: 'postgres' as DatabaseType,
username: 'admin',
synchronize: false,
dropSchema:
process.env.NODE_ENV !== 'production' &&
process.env.POSTGRES_DROP_SCHEMA === 'true',
migrations: ['dist/migrations/*.js'],
migrationsRun: true,
cache: shouldCache(),
} as PostgresConnectionOptions;
return await createConnection(config);
}
my server.ts file:
...
await postgresConnection().then(async () => {
console.info('Database connected!');
});
...
It works without issues running the server locally, with the database and adminer running on docker
I am running a PERN application and currently trying to dockerize it. When I run the database as a container and the server and client locally I have no issues. However, when I containerize the server, client, and database respectively, I am unable to make requests. It results in 404 errors. This is the same behavior that occurs when I pass pool the wrong port or host. So I'm wondering if somehow I am giving the wrong host and/or port to pool or if I should change it when I containerize it.
This is the Pool Instance
const Pool = require('pg').Pool
const pool = new Pool({
user: 'docker',
password: 'docker',
host: "localhost",
port: 4000,
database: "docker"
})
This is part of the rest api in the server:
const express = require("express")
const router = express.Router()
const pool = require('../database/database.js')
router.get("/:login", async (req, res) => {
try {
let loginReq = JSON.parse(decodeURIComponent(req.params.login))
const user = await pool.query(
"SELECT user_id,first_name,last_name,email FROM \"user\" where email = $1 and password = $2",
[loginReq.email, loginReq.password]
)
if(user.rows.length) {
res.json(user.rows[0])
} else {
throw new Error('User Not Found')
}
} catch (err) {
res.status(404).json(err.message)
}
})
This is each of my dockerfiles for my client, server, and database
Client:
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN npm install --production
CMD ["npm","start"]
EXPOSE 3000
Server:
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN npm install --production
CMD ["node","index.js"]
EXPOSE 5000
Database;
FROM postgres:15.1-alpine
COPY init.sql /docker-entrypoint-initdb.d/
This is my docker-compose.yml
version: "3.8"
services:
client:
build: ./client
ports:
- "3000:3000"
restart: unless-stopped
server:
build: ./server
ports:
- "5000:5000"
restart: unless-stopped
database:
build: ./database
ports:
- "4000:4000"
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=docker
- POSTGRES_DB=docker
- PGPORT=4000
volumes:
- kurva:/var/lib/postgresql/data
restart: unless-stopped
volumes:
kurva:
I don't understand why the behavior would be different between containerizing the server and running it locally when they all use the same ports. I have tried messing with the host and changing it to 0.0.0.0 but that did not help. Any help would be appreciated!
I found that it was a host issue and I needed to change the host accessed in the pool to the service name. Additionally, I needed to add that the server depends on the database service.
This is the updated pg pool:
const pool = new Pool({
user: 'docker',
password: 'docker',
host: "database",
port: 4000,
database: "docker"
})
This is the updated docker-compose.yml
version: "3.8"
services:
client:
build: ./client
ports:
- "3000:3000"
restart: unless-stopped
server:
build: ./server
ports:
- "5000:5000"
depends_on:
- database
restart: unless-stopped
database:
build: ./database
ports:
- "4000:4000"
environment:
- POSTGRES_USER=docker
- POSTGRES_PASSWORD=docker
- POSTGRES_DB=docker
- PGPORT=4000
volumes:
- kurva:/var/lib/postgresql/data
restart: unless-stopped
volumes:
kurva:
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/
;)
Hello I cannot access the exposed port. It's a node server (no framework). Chrome sends ERR_EMPTY_RESPONSE. Each time I change the file and test it I run docker-compose build. How can I get this up and running so my browser can ping port 3000?
EDIT: I included my server.js file incase I'm binding the port wrong in node.
Dockerfile
FROM node:8.11.1-alpine
WORKDIR /usr/src/app
VOLUME [ "/usr/src/app" ]
RUN npm install -g nodemon
EXPOSE 3000
CMD [ "nodemon", "-L", "src/index.js" ]
Docker-compose.yml
version: '3'
services:
node:
build:
context: ./node
dockerfile: Dockerfile
working_dir: /usr/src/app
volumes:
- ./node:/usr/src/app
networks:
- app-network
env_file: ./.env
environment:
- MESSAGE_QUEUE=amqp://rabbitmq
ports:
- "3000:3000"
links:
- rabbitmq
python:
build:
context: ./python
dockerfile: Dockerfile
working_dir: /usr/src/app
volumes:
- ./python:/usr/src/app
networks:
- app-network
env_file: ./.env
links:
- rabbitmq
rabbitmq:
image: rabbitmq:3.7.4
networks:
- app-network
networks:
app-network:
driver: bridge
Server.js
const mongoose = require('mongoose')
const hostname = '127.0.0.1';
const port = 3000;
const server = require('./controllers/index');
server.listen(port, hostname, () => {
// Connect To Mongo
mongoose.connect(process.env.MONGO_URI, { keepAlive: true, keepAliveInitialDelay: 300000, useNewUrlParser: true });
mongoose.connection.on('disconnected', () => {
console.error('MongoDB Disconnected')
})
mongoose.connection.on('error', (err) => {
console.error(err)
console.error('MongoDB Error')
})
mongoose.connection.on('reconnected', () => {
console.error('MongoDB Reconnected')
})
mongoose.connection.on('connected', () => {
console.error('MongoDB Connected')
})
console.log(`Server running at http://${hostname}:${port}/`);
});
Try to bind your app to 0.0.0.0 like this
const hostname = '0.0.0.0';
it will listen on all network addresses.
How run rethinkdb with nodejs on server ?
Is docker-compose.yml:
web:
build: ./app
volumes:
- "./app:/src/app"
ports:
- "8000:8000"
- "8080:8080"
- "28015:28015"
- "29015:29015"
links:
- "db:redis"
- "rethink:rethinkdb"
command: nodemon -L app/bin/www
db:
image: redis
ports:
- "6379"
rethink:
image: rethinkdb
is a folder app with nodejs-project and Dockerfile
Dockerfile
FROM node:0.10.38
RUN mkdir /src
RUN npm install nodemon -g
WORKDIR /src/app
ADD package.json package.json
RUN npm install
ADD nodemon.json nodemon.json
In the nodejs-project connection to rethinkdb:
module.exports.setup = function() {
r.connect({host: dbConfig.host /* 127.0.0.1*/, port: dbConfig.port /*is 28015 or 29015 or 32783*/ }, function (err, connection) {
...
});
};
And when run a docker-compose up, have an error
Could not connect to 127.0.0.1:32783.
Try exposing the port in you docker-compose.yml:
web:
build: ./app
volumes:
- "./app:/src/app"
ports:
- "8000:8000"
depends_on:
- db
- rethink
command: nodemon -L app/bin/www
db:
image: redis
ports:
- "6379:6379"
rethink:
image: rethinkdb
ports:
- "8080:8080"
- "28015:28015"
- "29015:29015"
Then dbConfig.host = rethinkdb and dbConfig.port = 28015:
module.exports.setup = function() {
r.connect({host: dbConfig.host /* 127.0.0.1*/, port: dbConfig.port /*is 28015 or 29015 or 32783*/ }, function (err, connection) {
...
});
};
Hope you find this helpful.