node.js容器无法自动访问postgres容器

wrrgggsh  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(260)

所以,我一直在尝试为我的“项目”创建一个docker,它使用的是一个没有阶段和docker compose的简单docker文件。但是,现在我尝试使用阶段,我认为我的更改不应该像这样影响项目。
当我用 docker-compose -f ./docker-compose.dev.yml up --build -d ,所有3个服务都会启动,但包含节点内容的web服务会尝试执行我的 yarn migration:run 命令,它只在第一次尝试时起作用,然后我得到的是:

  1. PS C:\Users\joaov\Documents\gittin> docker logs web-dev
  2. [INFO wait] --------------------------------------------------------
  3. [INFO wait] docker-compose-wait 2.9.0
  4. [INFO wait] ---------------------------
  5. [DEBUG wait] Starting with configuration:
  6. [DEBUG wait] - Hosts to be waiting for: []
  7. [DEBUG wait] - Paths to be waiting for: []
  8. [DEBUG wait] - Timeout before failure: 30 seconds
  9. [DEBUG wait] - TCP connection timeout before retry: 5 seconds
  10. [DEBUG wait] - Sleeping time before checking for hosts/paths availability: 0 seconds
  11. [DEBUG wait] - Sleeping time once all hosts/paths are available: 0 seconds
  12. [DEBUG wait] - Sleeping time between retries: 1 seconds
  13. [DEBUG wait] --------------------------------------------------------
  14. [INFO wait] docker-compose-wait - Everything's fine, the application can now start!
  15. [INFO wait] --------------------------------------------------------
  16. yarn run v1.22.5
  17. $ ts-node-dev ./node_modules/typeorm/cli.js migration:run
  18. [INFO] 23:26:05 ts-node-dev ver. 1.1.6 (using ts-node ver. 9.1.1, typescript ver. 4.3.4)
  19. Error during migration run:
  20. Error: connect ECONNREFUSED 172.18.0.2:5432
  21. at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16) {
  22. errno: -111,
  23. code: 'ECONNREFUSED',
  24. syscall: 'connect',
  25. address: '172.18.0.2',
  26. port: 5432
  27. }
  28. error Command failed with exit code 1.
  29. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
  30. PS C:\Users\joaov\Documents\gittin>

现在node.js应用程序没有运行,因为容器已停止,但是如果我运行 docker start web-dev ,我从日志中得到以下信息:

  1. [INFO wait] --------------------------------------------------------
  2. [INFO wait] docker-compose-wait 2.9.0
  3. [INFO wait] ---------------------------
  4. [DEBUG wait] Starting with configuration:
  5. [DEBUG wait] - Hosts to be waiting for: []
  6. [DEBUG wait] - Paths to be waiting for: []
  7. [DEBUG wait] - Timeout before failure: 30 seconds
  8. [DEBUG wait] - TCP connection timeout before retry: 5 seconds
  9. [DEBUG wait] - Sleeping time before checking for hosts/paths availability: 0 seconds
  10. [DEBUG wait] - Sleeping time once all hosts/paths are available: 0 seconds
  11. [DEBUG wait] - Sleeping time between retries: 1 seconds
  12. [DEBUG wait] --------------------------------------------------------
  13. [INFO wait] docker-compose-wait - Everything's fine, the application can now start!
  14. [INFO wait] --------------------------------------------------------
  15. yarn run v1.22.5
  16. $ ts-node-dev ./node_modules/typeorm/cli.js migration:run
  17. [INFO] 23:29:24 ts-node-dev ver. 1.1.6 (using ts-node ver. 9.1.1, typescript ver. 4.3.4)
  18. query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations'
  19. query: CREATE TABLE "migrations" ("id" SERIAL NOT NULL, "timestamp" bigint NOT NULL, "name" character varying NOT NULL, CONSTRAINT "PK_8c82d7f526340ab734260ea46be" PRIMARY KEY ("id"))
  20. query: SELECT * FROM "migrations" "migrations" ORDER BY "id" DESC
  21. 0 migrations are already loaded in the database.
  22. 1 migrations were found in the source code.
  23. 1 migrations are new migrations that needs to be executed.
  24. query: START TRANSACTION
  25. query: CREATE TABLE "users" ("id" uuid NOT NULL, "name" varchar NOT NULL, "email" varchar NOT NULL, "password" varchar NOT NULL, "admin" boolean NOT NULL DEFAULT false, "created_at" timestamp NOT NULL DEFAULT now(), "updated_at" timestamp NOT NULL DEFAULT now(), CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))
  26. query: INSERT INTO "migrations"("timestamp", "name") VALUES ($1, $2) -- PARAMETERS: [1623957922252,"CreateUsers1623957922252"]
  27. Migration CreateUsers1623957922252 has been executed successfully.
  28. query: COMMIT
  29. Done in 1.23s.
  30. yarn run v1.22.5
  31. $ ts-node-dev ./src/index.ts
  32. [INFO] 23:29:25 ts-node-dev ver. 1.1.6 (using ts-node ver. 9.1.1, typescript ver. 4.3.4)
  33. PS C:\Users\joaov\Documents\gittin>

有了它,我们可以看到连接是有效的,但在docker映像构建的第一次尝试中它不起作用。。。
为了清楚起见,我将在这里直接留下一些我认为有用的代码,并共享github存储库。
github:https://github.com/joaocasarin/gittin
package.json脚本:

  1. "scripts": {
  2. "docker:dev": "docker-compose -f ./docker-compose.dev.yml up --build -d",
  3. "docker:prod": "docker-compose -f ./docker-compose.yml up --build -d",
  4. "docker:clear": "docker-compose down",
  5. "dev": "ts-node-dev ./src/index.ts",
  6. "test": "jest",
  7. "migration:run": "ts-node-dev ./node_modules/typeorm/cli.js migration:run",
  8. "migration:create": "ts-node-dev ./node_modules/typeorm/cli.js migration:create",
  9. "entity:create": "ts-node-dev ./node_modules/typeorm/cli.js entity:create",
  10. "build": "tsc",
  11. "start": "node ."
  12. }

您可以看到,当节点容器启动时,它会等待db,运行迁移:run and run yarn dev 从ts-node-dev开始。
ormconfig.js:

  1. module.exports = {
  2. "type": "postgres",
  3. "ssl": process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
  4. "url": process.env.DATABASE_URL,
  5. "entities": ["dist/entities/*.js"], // typeorm loads entities from this directory
  6. "migrations": ["dist/database/migrations/*.js"], // typeorm loads migrations from the directory
  7. "cli": {
  8. "migrationsDir": "src/database/migrations", // typeorm creates migrations in this directory
  9. "entitiesDir": "src/entities" // typeorm creates entities in this directory
  10. }
  11. }

dockerfile.dev:

  1. FROM node:14-alpine as base
  2. WORKDIR /usr/app
  3. COPY package.json yarn.lock jest.config.ts tsconfig.json ormconfig.js ./
  4. COPY src ./src
  5. # used for waiting while database container is running to continue with node stuffs
  6. ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.9.0/wait /wait
  7. RUN chmod +x /wait
  8. RUN yarn install
  9. RUN yarn build
  10. FROM base as dev
  11. WORKDIR /usr/app
  12. ENV NODE_ENV=development
  13. COPY --from=base /usr/app/dist/database/migrations ./
  14. CMD /wait && yarn migration:run && yarn dev
  15. EXPOSE 3000

docker-compose.dev.yml:

  1. version: "3.8"
  2. services:
  3. web:
  4. container_name: web-dev
  5. build:
  6. context: .
  7. dockerfile: Dockerfile.dev
  8. depends_on:
  9. - db
  10. ports:
  11. - ${PORT}:${PORT}
  12. environment:
  13. DATABASE_URL: ${DATABASE_URL}
  14. NODE_ENV: ${NODE_ENV}
  15. PORT: ${PORT}
  16. db:
  17. container_name: db-dev
  18. image: postgres:13.3-alpine
  19. restart: always
  20. environment:
  21. POSTGRES_USER: postgres
  22. POSTGRES_PASSWORD: docker
  23. POSTGRES_DB: gittin
  24. ports:
  25. - 5432:5432
  26. volumes:
  27. - ./postgres-dev:/var/lib/postgresql/data
  28. pgadmin:
  29. container_name: pgadmin-dev
  30. image: dpage/pgadmin4:5.5
  31. restart: always
  32. environment:
  33. PGADMIN_DEFAULT_EMAIL: db@db.com
  34. PGADMIN_DEFAULT_PASSWORD: docker
  35. PGADMIN_PORT: 80
  36. ports:
  37. - 8080:80
  38. volumes:
  39. - pgadmin-data-dev:/var/lib/pgadmin
  40. depends_on:
  41. - db
  42. volumes:
  43. db-data:
  44. pgadmin-data-dev:

.env:

  1. PORT=3000
  2. NODE_ENV=development
  3. DATABASE_URL=postgres://postgres:docker@db:5432/gittin

如果需要,请询问任何问题,我真的想知道如何解决这个问题,并且,如果可能的话,给出关于dockerfile和docker compose文件是如何编写的以及我可以做些什么来改进的建议。
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题