docker実行時のpostgresqlテーブルの作成

docker実行時のpostgresqlテーブルの作成

コンテナを実行するときにPostgreSQLデータベースを実行するDockerfileを作成したいと思います。

だから私がしたことはDockerfileを作成することでした。

FROM postgres:latest

COPY *.sh /docker-entrypoint-initdb.d/

Dockerfile初期化スクリプト"postgres.sh"を/docker-entrypoint-initdb.d/にコピーします。 postgres.shスクリプトには次の内容が含まれています。

#!/bin/sh

su postgres -c "psql << EOF
ALTER USER postgres WITH PASSWORD '123';
create database shippingchallenge;
\c shippingchallenge;
create table people(firstname CHAR(20), surname CHAR(30));
insert into people values ('Pieter', 'Pauwels');
EOF"

だから私がやろうとしているのは、「firstname」と「surname」という2つの値を持つ「people」テーブルを含む「shippingchallenge」データベースを作成することです。使用sudo docker exec -it <container> /bin/bashして貼り付けるとうまく動作します。

ただし、上記の方法で生成されたイメージを実行すると、sudo docker run -p 5432:5432 -e POSTGRES_PASSWORD=123 <image>次のエラーが発生します。

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
 
The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF18".
The default text search configuration will be set to "english". 

Data page checksums are disabled. 

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix 
selecting default maxconnections ... 100 
selecting default shared_buffers ... 128MB 
selecting default time zone ... Etc/UTC 
creating configuration files ... ok 
running bootstrap script ... ok 
performing post-bootstrap initialization ... ok 
syncing data to disk ... ok 

initdb: warning: enabling "trust" authentication for local connections 
You can change this by editing pg_hba.conf or using the option -A or 
--auth-local and --auth-host, the next time you run initdb.
 
Success. You can now start the database server using: 

    pg_ctl -D /var/lib/postgresql/data -l logfile start 
    
waiting for server to start....2020-12-19 12:42:09.749 UTC [45] LOG: starting PostgreSQL 13.1 (Debian 13.1-1 pgdg100+1) on x86_64 pc linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-12-19 12:42:09.754 UTC [45] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-12-19 12:42:09.771 UTC [46] LOG: database system was shut down at 2020-12-19 12:42:07 UTC
2020-12-19 12:42:09.778 UTC [45] LOG: database system is ready to accept connections
 done 
server started 

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/postgres.sh 
Password: su: Authentication failure 

コンテンツ/etc/pam.d/su:

auth       sufficient pam_rootok.so





session       required   pam_env.so readenv=1
session       required   pam_env.so readenv=1 envfile=/etc/default/locale

session    optional   pam_mail.so nopen

session    required   pam_limits.so

@include common-auth
@include common-account
@include common-session

ご協力ありがとうございます!

ベストアンサー1

これは、ビルド時にSQLステートメントを含むスクリプトファイルをdocker-entrypoint-initdb.dディレクトリに配置することによって行われます。習慣公式Postgresを含む画像~によるとビデオhttps://github.com/docker-library/postgres

docker-context-dir
  /docker-entrypoint-initdb.d
    |-create-table.sql
  |-Dockerfile

ドッカーファイル

FROM postgres:latest
COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d

table.sqlの生成

CREATE TABLE people (
    id SERIAL PRIMARY KEY,
    ...);

おすすめ記事