在 docker run 上創建 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"
所以我想要做的是創建一個數據庫“shippingchallenge”,其中包含一個具有 2 個值的表“people”:“firstname”和“surname”。
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
在此先感謝您的幫助!
/bin/su
由於在可執行權限和 和 的內容/etc/pam.d/su
上沒有發現問題/etc/pam.d/su-l
。image
應該沒問題!在我在本地拉取圖像後,似乎錯誤是由於
su postgres -c
;它應該被刪除,因為如果你添加whoami
到腳本中,你會注意到腳本將預設執行postgresql
,這是因為容器在啟動時做了一個su
topostgres
,因為守護程序需要用這個使用者啟動。碼頭工人文件:
FROM registry.hub.docker.com/library/postgres COPY postgres.sh /docker-entrypoint-initdb.d/ RUN chmod +x /docker-entrypoint-initdb.d/postgres.sh
腳本:
#!/bin/bash whoami 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
測試:
docker exec -it <container> bash su postgres psql postgres=# \l
輸出(表已創建,所有者為 postgres):
shippingchallenge|postgres|UTF8|en_US.utf8|en_US.utf8|
這就是為什麼
su
要求輸入postgres
使用者密碼並且由於沒有通過任何內容,因此您在建構期間遇到了身份驗證失敗。
當使用官方 Postgres基礎鏡像https://github.com/docker-library/postgres建構自定義鏡像時,將帶有 SQL 語句的腳本文件放置到 docker-entrypoint-initdb.d 目錄中即可
docker-context-dir /docker-entrypoint-initdb.d |-create-table.sql |-Dockerfile
Dockerfile
FROM postgres:latest COPY docker-entrypoint-initdb.d /docker-entrypoint-initdb.d
創建表.sql
CREATE TABLE people ( id SERIAL PRIMARY KEY, ...);