@dsgnfox

Как объединить несколько сервисов в одну сеть через docker-compose.yml?

Мой docker-compose.yml. Создал сеть postgres. Добавил ее в каждый сервис через ключ `networks`. После запуска через docker compose up сервисы `postgres` и `pgadmin` попадают в одну сеть, но сервиса `mylib` в ней нет.

version: '3.9'
services:
  postgres:
    container_name: postgres_container
    image: postgres:14.8-alpine3.18
    environment:
      POSTGRES_DB: 'postgres'
      POSTGRES_USER: 'postgres'
      POSTGRES_PASSWORD: 'postgres'
      PGDATA: '/var/lib/postgresql/data/pgdata'
    volumes:
      - ../2. Init Database:/docker-entrypoint-initdb.d
      - postgres:/var/lib/postgresql/data
    ports:
      - '5400:5432'
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U postgres -d postgres']
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    restart: unless-stopped
    networks:
      - postgres

  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4:7.2
    environment:
      PGADMIN_CONFIG_AUTHENTICATION_SOURCES: "['internal']"
      PGADMIN_DEFAULT_EMAIL: 'admin@admin.com'
      PGADMIN_DEFAULT_PASSWORD: 'b5JYHhxyvglFq5'
    volumes:
      - pgadmin-data:/var/lib/pgadmin
    ports:
      - '5050:80'
    restart: unless-stopped
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 1G
    networks:
      - postgres

  mylib:
    container_name: mylib
    image: mylib
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - '3535:3000'
    networks:
      - postgres

volumes:
  postgres:
  pgadmin-data:

networks:
  postgres:
    driver: bridge


Вывод `docker network inspect mylib_postgres`:
[
    {
        "Name": "mylib_postgres",
        "Id": "8c25d459709b37fbe84e05de7b21ca505e76a13a68887d851b3c907dc0fa2e95",
        "Created": "2023-08-28T17:59:47.016484377+03:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.21.0.0/16",
                    "Gateway": "172.21.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "39f96f89e663e7391a76b23bee379e743ab4d7d69b732ee9d0c1a03ed3ee7b80": {
                "Name": "postgres_container",
                "EndpointID": "c4fa9127b6485a009a6ec905a693130b3a983c729792149c6e5b3f699f6145e5",
                "MacAddress": "02:42:ac:15:00:03",
                "IPv4Address": "172.21.0.3/16",
                "IPv6Address": ""
            },
            "6e01ffac49ea521830348879f5f3d6e18b42c1a0c0bda45e65df6a3a932be087": {
                "Name": "pgadmin_container",
                "EndpointID": "d3ba4567822774caeebd16b25aec2793eda367d177f0dac720f19628db72506a",
                "MacAddress": "02:42:ac:15:00:04",
                "IPv4Address": "172.21.0.4/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "postgres",
            "com.docker.compose.project": "mylib",
            "com.docker.compose.version": "2.0.1"
        }
    }
]


Запущенные контейнеры:
64ed9d18bcf1e296889019.png

Логи контейнера `mylib`. Он падает, потому что не может подключиться к базе, а к базе он не может подключиться потому что контейнер `mylib` и `postgres` не находятся в одной сети.
> main@0.0.1 start:prod
> NODE_ENV=production node dist/main

[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [NestFactory] Starting Nest application...
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] AppModule dependencies initialized +37ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] AsxeerModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] ApiModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] BotUpdate dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] TelegrafModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] DiscoveryModule dependencies initialized +1ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] BraveModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] NotesModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] EventsModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] TelegrafCoreModule dependencies initialized +42ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] HttpModule dependencies initialized +1ms
[Nest] 22  - 08/29/2023, 7:23:24 AM     LOG [InstanceLoader] BotModule dependencies initialized +0ms
[Nest] 22  - 08/29/2023, 7:23:24 AM   ERROR [ExceptionHandler] connect ECONNREFUSED 127.0.0.1:5400
SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:5400
    at Client._connectionCallback (/mylib/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:131:24)
    at Client._handleErrorWhileConnecting (/mylib/node_modules/pg/lib/client.js:327:19)
    at Client._handleErrorEvent (/mylib/node_modules/pg/lib/client.js:337:19)
    at Connection.emit (node:events:514:28)
    at Socket.reportStreamError (/mylib/node_modules/pg/lib/connection.js:58:12)
    at Socket.emit (node:events:514:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)


Как это поправить я не знаю. Помогите пожалуйста
  • Вопрос задан
  • 466 просмотров
Решения вопроса 1
@q2digger
никого не трогаю, починяю примус
падает он потому что пытается подключится к 127.0.0.1 , а чтобы подключаться к контейнеру postgres , то и в строке подключения надо указывать ИМЯ КОНТЕЙНЕРА.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы