I am creating an application in nodejs, with typescript, postgres and typeorm, to help in my development I put this all to start with docker. I'm using the network "bridge" and the connections between the containers are ok. The problem is when I want to run a migration using the TypeORM CLI. The CLI simply does not recognize my host that is set in my ormconfig.json as "postgres". The funny thing is that when I change the host on ormconfig.json to "localhost" the CLI works perfectly, but I lose the connection to the database, and when I put the host as "postgres" the connection to the database works but the CLI to run the migrations does not.
This is the TypeORM CLI error when trying to run migrations:
yarn typeorm migration:run
yarn run v1.22.0
$ ts-node-dev ./node_modules/typeorm/cli.js migration:run
Using ts-node version 8.10.2, typescript version 3.9.7
Error during migration run:
Error: getaddrinfo ENOTFOUND postgres
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'postgres'
}
error Command failed with exit code 1.
docker-compose.yml:
version: '3.7'
services:
api_cadunico:
container_name: api_cadunico
build:
context: .
target: development
command: yarn start:dev
restart: unless-stopped
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
env_file:
- .env
ports:
- 3333:3333
networks:
- connection_cadunico
depends_on:
- postgres
postgres:
container_name: postgres
image: postgres:12
volumes:
- pgdata:/var/lib/postgresql/data
environment:
TZ: America/Fortaleza
POSTGRES_DB: ${DATABASE_NAME}
POSTGRES_USER: ${DATABASE_USER}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
PG_DATA: /var/lib/postgresql/data
ports:
- 5432:5432
networks:
- connection_cadunico
networks:
connection_cadunico:
driver: bridge
volumes:
pgdata:
ormconfig.json:
[
{
"name": "default",
"type": "postgres",
"host": "postgres",
"port": 5432,
"username": "postgres",
"password": "cadunico",
"database": "cadunico_atendimento",
"entities": [
"./src/entities/*.ts"
],
"migrations": [
"./src/database/migrations/*.ts"
],
"cli": {
"migrationsDir": "./src/database/migrations"
}
}
]
What do I need to do to run the migrations without having to change the host to localhost and normally use the docker's network host?