Docker-撰写我无法创建Postgress服务器

时间:2020-03-17 23:55:02

标签: node.js docker

你好,我正在尝试创建一个postgres compose docker pg管理员4 和节点

但是我遇到了困难,无法创建服务器:

enter image description here

我尝试了所有可能的名称,但我不知道自己在输入什么错误:

我的 docker撰写:

version: "3.7"
services:
  emasa-postgres:
    image: postgres
    environment:
      POSTGRES_PASSWORD: emasa@
      POSTGRES_USER: postgres
      POSTGRES_DB: emasa
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - postgres-compose-network
  web:
    image: emasa-web
    depends_on:
      - emasa-postgres
    ports:
      - "4000:4000"
    networks:
      - postgres-compose-network
  teste-pgadmin-compose:
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: "emasa@hotmail.com"
      PGADMIN_DEFAULT_PASSWORD: "emasa"
    ports:
      - "16543:80"
    depends_on:
      - emasa-postgres
    networks:
      - postgres-compose-network

networks:
  postgres-compose-network:
    driver: bridge

DockerFile

FROM node as builder
WORKDIR usr/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build


FROM node
WORKDIR usr/app
COPY package*.json ./
RUN npm install --production

COPY --from=builder /usr/app/dist ./dist

COPY ormconfig.docker.json ./ormconfig.json
COPY .env . 

expose 4000
CMD node dist/src/index.js

我的orm配置:

{
  "type": "postgres",
  "host": "db",
  "port": 5432,
  "username": "postgres",
  "password": "emasa@",
  "database": "postgres",
  "synchronize": true,
  "logging": false,
  "entities": ["src/entity/**/*.ts"],
  "migrations": ["src/migration/**/*.ts"],
  "subscribers": ["src/subscriber/**/*.ts"],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }
}

我的docker-compose记录器:

Attaching to back-end_web_1, back-end_emasa-postgres_1, back-end_teste-pgadmin-compose_1
emasa-postgres_1         | The files belonging to this database system will be owned by user "postgres".
emasa-postgres_1         | This user must also own the server process.
emasa-postgres_1         |
emasa-postgres_1         | The database cluster will be initialized with locale "en_US.utf8".
emasa-postgres_1         | The default database encoding has accordingly been set to "UTF8".
emasa-postgres_1         | The default text search configuration will be set to "english".
emasa-postgres_1         |
emasa-postgres_1         | Data page checksums are disabled.
emasa-postgres_1         |
emasa-postgres_1         | fixing permissions on existing directory /var/lib/postgresql/data ... ok
emasa-postgres_1         | creating subdirectories ... ok
emasa-postgres_1         | selecting dynamic shared memory implementation ... posix
emasa-postgres_1         | selecting default max_connections ... 20
emasa-postgres_1         | selecting default shared_buffers ... 400kB
emasa-postgres_1         | selecting default time zone ... Etc/UTC
emasa-postgres_1         | creating configuration files ... ok
emasa-postgres_1         | 2020-03-17 23:48:08.890 UTC [81] FATAL:  data directory "/var/lib/postgresql/data" has wrong ownership
emasa-postgres_1         | 2020-03-17 23:48:08.890 UTC [81] HINT:  The server must be started by the user that owns the data directory.
emasa-postgres_1         | child process exited with exit code 1
emasa-postgres_1         | initdb: removing contents of data directory "/var/lib/postgresql/data"
emasa-postgres_1         | running bootstrap script ... web_1                    | Error: getaddrinfo ENOTFOUND db

我的容器ls:

docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                            NAMES
3a20bcd4f105        emasa-web           "docker-entrypoint.s…"   5 minutes ago       Up 5 minutes        0.0.0.0:4000->4000/tcp           back-end_web_1
b377e650e772        dpage/pgadmin4      "/entrypoint.sh"         14 minutes ago      Up 14 minutes       443/tcp, 0.0.0.0:16543->80/tcp   back-end_teste-pgadmin-compose_1

[![在此处输入图片描述] [2]] [2]

我真的尝试过使用容器名称:

emasa-postgres_1 emasa-postgres

无济于事

4 个答案:

答案 0 :(得分:1)

您的容器是 bridge 网络的一部分,用于根据您在docker-compose.yml中定义并使用{{ 1}},其中您定义为桥梁

postgres-compose-network

尝试

  1. 运行 networks: postgres-compose-network: driver: bridge 来查找在docker运行时中创建的网络,所有网络都将在控制台中列出,您会发现docker network ls类似{{1} }这应该是您的网络。
  2. 下一次运行 [name]_default来检查网络,它将打印如下内容:

    name-of-project-directory_default
  3. 查找 docker network inspect [name]_default,您应该使用此IP地址而不是[ { "Name": "dockerizing_default", "Id": "98f8a8ef5153fbe91acc28ed0688179f75e08b321b72aab0abe7bf57e1880cc8", "Created": "2020-03-07T14:13:49.446907044+01:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": true, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": { "com.docker.compose.network": "default", "com.docker.compose.project": "dockerizing", "com.docker.compose.version": "1.25.4" } } ] ,它可能是IPv4Address 我希望这能解决问题

答案 1 :(得分:1)

问题的原因可以在您提供的日志中看到。

2020-03-17 23:48:08.890 UTC [81]致命:数据目录“ / var / lib / postgresql / data”具有错误的所有权

您可能正在Windows计算机上运行docker?如果是这样,这似乎是一个常见的错误-check here。因此,您可能要使用永久性的docker卷,而不是la

services:
  emasa-postgres:
    ...
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
    - postgres-compose-network

volumes:
  pgdata:

我仍然对在pgadmin中使用哪个本地主机感到怀疑

为了从emasa-postgres连接到pgadmin,您可以像这样设置emasa-postgres容器的主机名和网络别名:

services:
    emasa-postgres:
        hostname: my_super_cool_postgres
        image: postgres
        networks:
            postgres-compose-network:
                aliases:
                    - my_super_cool_postgres

然后,您应该能够通过主机localhost连接到数据库,而不是使用dbmy_super_cool_postgres

这样,您应该可以从pgadmin连接到postgres容器。如果仍然无法使用,您也可以尝试使用links,它是Docker的旧功能,但应该可以工作,例如

services:
    teste-pgadmin-compose:
        image: dpage/pgadmin4
        links:
            - "emasa-postgres:my_super_cool_postgres"

这次,您必须将其设置为teste-pgadmin-compose(或从任何需要访问emasa-postgres容器的位置)。然后应该可以访问主机my_super_cool_postgres和端口5432

答案 2 :(得分:0)

尝试主机名是127.0.0.1:4532。 还要确保postgres容器可以运行使用 docker ps

更新: 这应该起作用

volumes:
      - ./pgdata:/var/lib/postgresql/data/pgdata
ports:
    - "5432:5432"
environment:
    PGDATA: /var/lib/postgresql/data/pgdata

答案 3 :(得分:0)

不是数据库,请更改此“主机”:“数据库”

对此

“主机”:“ emasa-postgres”

emasa-postgres是您用来代替服务之间连接的IP地址的名称

在您的orm配置中更改