Issue
I'm currently building a system in Node with a Postgres db, and I've created a container using docker-compose for both. By running docker-compose up
I manage to initialize all container (Node app, Postgres db and PGAdmin) and a connection is successfully established.
server | yarn run v1.22.5
server | $ ts-node-dev --respawn --ignore-watch node_modules src/index.ts
server | [INFO] 14:35:39 ts-node-dev ver. 1.1.8 (using ts-node ver. 9.1.1, typescript ver. 4.4.3)
server | Atempting to connect to database...
server | SUCCESS: Connection achieved.
server | Running migrations...
server | 🏃 Running Server on port 3000
However when I try to generate migrations using yarn typeorm migration:generate -n [something]
I get a ENOTFOUND
error.
yarn run v1.22.15
$ node --require ts-node/register ./node_modules/typeorm/cli.js migration:generate -n UserRefac
Error during migration generation:
Error: getaddrinfo ENOTFOUND postgres
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:69:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'postgres'
}
error Command failed with exit code 1.
This is my docker-compose
file:
version: '3.7'
services:
server:
image: server
build:
context: .
target: build-node
volumes:
- ./:/src/app
- src/app/node_modules
container_name: server
ports:
- 3000:3000
depends_on:
- postgres
entrypoint: ['yarn', 'start']
networks:
- servernet
postgres:
image: postgres
environment:
POSTGRES_PASSWORD: inesc2021
POSTGRES_USER: inesc
POSTGRES_DB: db_inesc
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- 5432:5432
networks:
- servernet
pgadmin:
image: dpage/pgadmin4
restart: always
ports:
- 5050:80
environment:
- PGADMIN_DEFAULT_EMAIL=inesc@inesc.pt
- PGADMIN_DEFAULT_PASSWORD=inesc2021
depends_on:
- postgres
networks:
- servernet
networks:
servernet:
driver: bridge
This is my Dockerfile
:
FROM node as build-node
WORKDIR /src/app
COPY . .
RUN ["yarn"]
RUN ["yarn", "build"]
EXPOSE 3000
ENTRYPOINT ["yarn"]
This is my ormconfig.json
:
{
"type": "postgres",
"host": "postgres",
"port": 5432,
"username": "inesc",
"password": "inesc2021",
"database": "db_inesc",
"synchronize": false,
"logging": false,
"entities": ["src/classes/**/*.ts"],
"migrations": ["src/migrations/**/*.ts"],
"cli": {
"entitiesDir": "src/classes",
"migrationsDir": "src/migrations"
}
}
And finally, this is my package.json
, so you can see how I define my scripts:
{
"name": "ihanduapp_v2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "tsc -w",
"dev": "nodemon dist/index.js",
"start": "ts-node-dev --respawn --ignore-watch node_modules src/index.ts",
"build": "tsc",
"start2": "yarn build && node dist/index.js",
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^16.10.2",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^4.32.0",
"@typescript-eslint/parser": "^4.32.0",
"eslint": "^7.32.0",
"nodemon": "^2.0.13",
"prettier": "^2.4.1",
"ts-node": "^10.2.1",
"ts-node-dev": "^1.1.8",
"typescript": "^4.4.3"
},
"dependencies": {
"class-validator": "^0.13.1",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-openapi-validator": "^4.13.1",
"pg": "^8.7.1",
"reflect-metadata": "^0.1.13",
"swagger-jsdoc": "^6.1.0",
"swagger-ui-express": "^4.1.6",
"typeorm": "^0.2.37"
}
}
Of course, when I change "host": "postgres"
to "host": "localhost"
, I'm able to generate and run migrations using the above-metioned yarn script, but I naturally loose the connections between my containers, the server app failing the retry logic.
I don't know where to problem lies, as I'm new both to docker-compose and TypeORM, so any help would be more than welcome.
As a final NOTE: I'm running all this in MacOS.
Thanks in advance.
Solution
It looks like you're attempting to run the migrate command on your host OS, which does not know about a host called postgres
.
You will need to run the migrate command within the app container (which is in the virtual network that has a host called postgres
):
docker-compose exec server node --require ts-node/register ./node_modules/typeorm/cli.js migration:generate -n UserRefac
Answered By - AKX
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.