Reputation: 437
I am a beginner in node, nest, and docker but somehow I got assigned a job to dockerized all the existing node js applications.
I followed one of the youtube tutorial and successfully deployed the basic hello world via docker but in the next youtube tutorial when I am trying to add Postgres to the docker I am facing some issues in connecting to Postgres.
I am using docker desktop on mac.
Here is my docker-compose.yml
file code snippet
version: "3.9" # optional since v1.27.0
services:
api:
build:
dockerfile: Dockerfile
context: .
depends_on:
- postgres
environment:
DATABASE_URL: postgres://user:password@postgres:5432/db
NODE_ENV: developement
PORT: 3000
ports:
- "8080:3000"
postgres:
image: postgres:14.0
ports:
- "35000:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
Here is the entire error log
Github Repository of this project
Thank you for helping in advance :)
Upvotes: 1
Views: 12592
Reputation: 616
Ok, After some investigation I found out the problem is with host
. You should use container_name
as a host
value
docker-compose.yml is:
version: "3.8"
services:
db:
image: postgres:16.1-alpine
container_name: my-app-db
restart: unless-stopped
ports:
- "${POSTGRES_PORT}:${POSTGRES_PORT}"
env_file:
- .env
volumes:
- db:/var/lib/postgresql/data
volumes:
db:
config db is:
const dbConfig = {
port: Number(process.env.POSTGRES_PORT),
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB
};
export const ormConfig: PostgresConnectionOptions = {
type: "postgres",
host: "my-app-db",
port: dbConfig.port,
database: dbConfig.database,
username: dbConfig.user,
password: dbConfig.password
};
@Module({
imports: [
TypeOrmModule.forRoot(ormConfig),
...
],
providers: [AppService]
})
Upvotes: 0
Reputation: 701
In the normal case, without Docker, i. e. you are using node and postgresql on you development or production machine, you just want to start postgres service and enable it if you want.
In order to start your postgres service, type the following command:
sudo systemctl start postgresql
To enable :
sudo systemctl enable postgresql
Note:
I hope this can help someone else!
Upvotes: -1
Reputation: 3346
Make sure your connection string is correct in your docker-compose.yml
. Just pass the host, port, user and pass seperated and let TypeOrm
handle the connection.
// app.module.ts
TypeOrmModule.forRoot({
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
})
And your docker-compose.yml:
# docker-compose.yml
version: '3.9'
services:
api:
build:
dockerfile: Dockerfile
context: .
depends_on:
- postgres
environment:
- POSTGRES_HOST=postgres
- POSTGRES_PASSWORD=promo-pass
- POSTGRES_USER=promo-user
- POSTGRES_DB=promo-api-db
- POSTGRES_PORT=5432
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: promo-user
POSTGRES_PASSWORD: promo-pass
POSTGRES_DB: promo-api-db
Upvotes: 0