PostgreSQL在Docker上运行时未创建表

时间:2018-11-15 08:39:22

标签: postgresql docker docker-compose firewall

我试图在Windows服务器中的linux容器docker上运行PostgreSQL,但是当我运行PostgreSQL时,它创建数据库,但是其中没有表且没有数据,而它应该创建所有表并向其中添加数据使用Actibook_latest.sql

这是Dockerfile

的代码
# Dockerfile
FROM postgres:9.4

RUN mkdir -p /tmp/psql_data/

COPY Actibook_latest.sql /tmp/psql_data/
COPY init_docker_postgres.sh /docker-entrypoint-initdb.d/

EXPOSE 5432

这是init_docker_postgres.sh

的代码
#!/bin/bash
# this script is run when the docker container is built
# it imports the base database structure and create the database for the tests
DATABASE_NAME="postgres"
DB_DUMP_LOCATION="/tmp/psql_data/Actibook_latest.sql"

echo "*** CREATING DATABASE ***"

psql "$DATABASE_NAME" < "$DB_DUMP_LOCATION";

echo "*** DATABASE CREATED! ***"

这是docker-compose

的代码
version: '2'

services:
 db:
    build: '.\Main Database Backup'
    environment:
      POSTGRES_DB: ${DB_POSTGRES_APP_DATABASE}
      POSTGRES_USER: ${DB_POSTGRES_APP_USER}
      POSTGRES_PASSWORD: ${DB_POSTGRES_APP_PASSW}
      PGDATA: /var/lib/postgresql/data/pgdata
    ports:
      - "5432:5432"
    restart: unless-stopped

Main Database Backup是包含该Dockerfile和init_docker_postgres.sh的文件夹。此外,Actibook_latest.sql包含用于创建表,数据等的sql。

当我运行docker-compose up而其他服务启动并运行时,日志显示如下:

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data/pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /var/lib/postgresql/data/pgdata/base/1 ... ok
initializing pg_authid ... ok
setting password ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

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:

    postgres -D /var/lib/postgresql/data/pgdata
or
    pg_ctl -D /var/lib/postgresql/data/pgdata -l logfile start

waiting for server to start....LOG:  database system was shut down at 2018-11-14 16:18:07 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
 done
server started

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init_docker_postgres.sh
/usr/local/bin/docker-entrypoint.sh: /docker-entrypoint-initdb.d/init_docker_postgres.sh: /bin/bash^M: bad interpreter: No such file or directory
LOG:  database system was interrupted; last known up at 2018-11-14 16:18:16 UTC
LOG:  database system was not properly shut down; automatic recovery in progress
LOG:  record with zero length at 0/16A4780
LOG:  redo is not required
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

我认为防火墙可能存在问题,因为它可以在我拥有的其他服务器中完美运行,该问题仅在此服务器中出现。那么,这台机器中的postgresql是否有阻止它的机会?

更新

我尝试重新启动docker,然后使用以下命令:

docker rm $(docker ps -a -q)
docker rmi $(docker images -a -q)
docker volume rm $(docker volume ls -q)

现在显示此错误:

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init_docker_postgres.sh
*** CREATING DATABASE ***
: No such file or directory/init_docker_postgres.sh: line 4: /tmp/psql_data/Actibook_latest.sql
/docker-entrypoint-initdb.d/init_docker_postgres.sh: line 4: $'\r': command not found
*** DATABASE CREATED! ***

当我去docker检查文件时:

docker exec -it db bin
cd /tmp/psql_data 
ls

它表明存在Actibook_latest.sql

1 个答案:

答案 0 :(得分:0)

您的入口点初始化脚本似乎不起作用,请在运行容器之前确保data_volume为空。

这是因为在docker-entrypoint.sh的第57行,它检查您的数据量(如果存在),它将不会执行您的初始化脚本。