Docker | Postgres База данных не инициализирована и пароль суперпользователя не указан - PullRequest
2 голосов
/ 05 августа 2020

Я использую docker-compose.yml для создания нескольких запущенных контейнеров, но не могу запустить сервер Postgres docker со следующими журналами, и да, я просмотрел много связанных сообщений SO, но они мне не помогли.

Creating network "complex_default" with the default driver
Creating complex_server_1   ... done
Creating complex_redis_1    ... done
Creating complex_postgres_1 ... done
Attaching to complex_postgres_1, complex_redis_1, complex_server_1
postgres_1  | Error: Database is uninitialized and superuser password is not specified.
postgres_1  |        You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres_1  |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres_1  |
postgres_1  |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres_1  |        connections without a password. This is *not* recommended.
postgres_1  |
postgres_1  |        See PostgreSQL documentation about "trust":
postgres_1  |        https://www.postgresql.org/docs/current/auth-trust.html
complex_postgres_1 exited with code 1

ниже моя конфигурация docker -compose:

version: '3'
services:
    postgres:
        image: 'postgres:11-alpine'
    redis:
        image: 'redis:latest'
    server:
        build:
            dockerfile: Dockerfile.dev
            context: ./server
        volumes:
            - /app/node_modules
            - ./server:/app
        environment:
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - PGUSER=postgres
            - PGHOST=postgres
            - PGDATABASE=postgres
            - PGPASSWORD=postgres_password
            - PGPORT=5432

, а также package.json внутри server директория выглядит следующим образом:

{
    "dependencies": {
        "body-parser": "^1.19.0",
        "cors": "^2.8.4",
        "express": "^4.16.3",
        "nodemon": "^2.0.4",
        "pg": "7.4.3",
        "redis": "^2.8.0"
    },
    "scripts": {
        "dev": "nodemon",
        "start": "node index.js"
    }
}

также для лучшего рассмотрения я приложил свою практическую структуру проекта:

сложная многоконтейнерная docker структура проекта

Год go он действительно работал нормально, Кто-нибудь знает, что сейчас не так в моем docker-compose файле.

Ответы [ 2 ]

0 голосов
/ 05 августа 2020

@ Adiii да, вы почти правы, поэтому я должен явно упомянуть среду также для изображения postgres, но без родительского тега db.

Итак, здесь я прямо упоминаю docker-compose.yaml config, чтобы помочь другим для лучшего понимания, также теперь я использую последнюю стабильную postgres версию изображения 12-alpine, в настоящее время последняя - postgres:12.3

version: '3'
services:
    postgres:
        image: 'postgres:12-alpine'
        environment:
            POSTGRES_PASSWORD: postgres_password
    redis:
        image: 'redis:latest'
    server:
        build:
            dockerfile: Dockerfile.dev
            context: ./server
        volumes:
            - /app/node_modules
            - ./server:/app
        environment:
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - PGUSER=postgres
            - PGHOST=postgres
            - PGDATABASE=postgres
            - PGPASSWORD=postgres_password
            - PGPORT=5432

и поэтому после docker-compose up создание и текущие журналы были следующими:

PS E:\docker\complex> docker-compose up
Creating network "complex_default" with the default driver
Creating complex_postgres_1 ... done
Creating complex_redis_1    ... done
Creating complex_server_1   ... done
Attaching to complex_redis_1, complex_postgres_1, complex_server_1
postgres_1  | The files belonging to this database system will be owned by user "postgres".
postgres_1  | This user must also own the server process.
postgres_1  |
postgres_1  | The database cluster will be initialized with locale "en_US.utf8".
postgres_1  | The default database encoding has accordingly been set to "UTF8".
postgres_1  | The default text search configuration will be set to "english".
postgres_1  |
postgres_1  | Data page checksums are disabled.
postgres_1  |
postgres_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres_1  | creating subdirectories ... ok
postgres_1  | selecting dynamic shared memory implementation ... posix
postgres_1  | selecting default max_connections ... 100
postgres_1  | selecting default shared_buffers ... 128MB
postgres_1  | selecting default time zone ... UTC
redis_1     | 1:C 05 Aug 2020 14:24:48.692 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1     | 1:C 05 Aug 2020 14:24:48.692 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1     | 1:C 05 Aug 2020 14:24:48.692 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
postgres_1  | creating configuration files ... ok
redis_1     | 1:M 05 Aug 2020 14:24:48.693 * Running mode=standalone, port=6379.
redis_1     | 1:M 05 Aug 2020 14:24:48.693 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1     | 1:M 05 Aug 2020 14:24:48.694 # Server initialized
redis_1     | 1:M 05 Aug 2020 14:24:48.694 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1     | 1:M 05 Aug 2020 14:24:48.694 * Ready to accept connections
postgres_1  | running bootstrap script ... ok
server_1    |
server_1    | > @ dev /app
server_1    | > nodemon
server_1    |
postgres_1  | performing post-bootstrap initialization ... sh: locale: not found
postgres_1  | 2020-08-05 14:24:50.153 UTC [29] WARNING:  no usable system locales were found
server_1    | [nodemon] 2.0.4
server_1    | [nodemon] to restart at any time, enter `rs`
server_1    | [nodemon] watching path(s): *.*
server_1    | [nodemon] watching extensions: js,mjs,json
server_1    | [nodemon] starting `node index.js`
postgres_1  | ok
server_1    | Listening
postgres_1  | syncing data to disk ... ok
postgres_1  |
postgres_1  |
postgres_1  | Success. You can now start the database server using:
postgres_1  |
postgres_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1  |
postgres_1  | initdb: warning: enabling "trust" authentication for local connections
postgres_1  | You can change this by editing pg_hba.conf or using the option -A, or
postgres_1  | --auth-local and --auth-host, the next time you run initdb.
postgres_1  | waiting for server to start....2020-08-05 14:24:51.634 UTC [34] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
postgres_1  | 2020-08-05 14:24:51.700 UTC [34] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2020-08-05 14:24:51.981 UTC [35] LOG:  database system was shut down at 2020-08-05 14:24:50 UTC
postgres_1  | 2020-08-05 14:24:52.040 UTC [34] LOG:  database system is ready to accept connections
postgres_1  |  done
postgres_1  | server started
postgres_1  |
postgres_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1  |
postgres_1  | waiting for server to shut down....2020-08-05 14:24:52.121 UTC [34] LOG:  received fast shutdown request

postgres_1  | 2020-08-05 14:24:52.186 UTC [34] LOG:  aborting any active transactions
postgres_1  | 2020-08-05 14:24:52.188 UTC [34] LOG:  background worker "logical replication launcher" (PID 41) exited with exit code 1
postgres_1  | 2020-08-05 14:24:52.188 UTC [36] LOG:  shutting down
postgres_1  | 2020-08-05 14:24:52.669 UTC [34] LOG:  database system is shut down
postgres_1  |  done
postgres_1  | server stopped
postgres_1  |
postgres_1  | PostgreSQL init process complete; ready for start up.
postgres_1  |
postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2020-08-05 14:24:52.954 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2020-08-05 14:24:53.136 UTC [43] LOG:  database system was shut down at 2020-08-05 14:24:52 UTC
postgres_1  | 2020-08-05 14:24:53.194 UTC [1] LOG:  database system is ready to accept connections

Надеюсь, это поможет многим.

0 голосов
/ 05 августа 2020

Год go он действительно работал нормально, Кто-нибудь знает, что сейчас не так, внутри моего docker -составного файла.

Похоже на вас вытащил образ fre sh, где в новом образе вы должны указать пароль пользователя Postgres. Вы можете заглянуть в Dockerhub, образ update один месяц go

enter image description here

postgress-11-alpine

  db:
    image: postgres:11-alpine
    restart: always
    environment:
      POSTGRES_PASSWORD: example

Поскольку сообщение об ошибке самоочевидно

You must specify POSTGRES_PASSWORD to a non-empty value for the
        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".

или POSTGRES_HOST_AUTH_METHOD=trust, используйте это, что не рекомендуется.

You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
 connections without a password. This is *not* recommended.

POSTGRES_PASSWORD

Эта переменная среды требуется для использования образа PostgreSQL. Он не должен быть пустым или неопределенным . Эта переменная среды устанавливает пароль суперпользователя для PostgreSQL. Суперпользователь по умолчанию определяется переменной среды POSTGRES_USER.

Переменные среды

...