问题:在 Docker Compose 中连接到数据库的问题

在一个文件夹中,我有 4 个文件:base.py、Dockerfile、docker-compose.yml 和 wait-for-it.sh。

base.py:

import psycopg2

print("JJJJJJJJ")

conn = psycopg2.connect("dbname='base123' user='postgres' host='db' password='pw1234'")
cur = conn.cursor()
cur.execute("CREATE TABLE test(id serial PRIMARY KEY, num int);")

Docker 文件:

FROM ubuntu:16.04

RUN apt-get update 
RUN apt-get -y install python-pip 
RUN apt-get update
RUN pip install --upgrade pip 
RUN pip install psycopg2-binary

COPY base.py base.py
COPY wait-for-it.sh wait-for-it.sh
RUN chmod +x /wait-for-it.sh
CMD ["python","base.py"]

docker-compose.yml

version: '3'
services:
  db:
    image: 'postgres:latest'
    expose:
      - "5432"
    ports:
    - "5559:5432"
     environment:
      POSTGRES_PASSWORD: pw1234
      POSTGRES_DB: base123
  aprrka:
    build: .
    depends_on:
      - db

    command: ["./wait-for-it.sh", "db:5432", "--", "python", "base.py"]

等待-fot-it.sh:

链接到 github 中的代码

docker-compose up之后我有这个 OUTPUT:

...
Creating postgres_db_1 ... done
Creating postgres_aprrka_1 ... done
Attaching to postgres_db_1, postgres_aprrka_1
db_1      | The files belonging to this database system will be owned by user "postgres".
db_1      | This user must also own the server process.
db_1      | 
db_1      | The database cluster will be initialized with locale "en_US.utf8".
db_1      | The default database encoding has accordingly been set to "UTF8".
db_1      | The default text search configuration will be set to "english".
db_1      | 
db_1      | Data page checksums are disabled.
db_1      | 
db_1      | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1      | creating subdirectories ... ok
db_1      | selecting default max_connections ... 100
db_1      | selecting default shared_buffers ... 128MB
db_1      | selecting dynamic shared memory implementation ... posix    
aprrka_1  | wait-for-it.sh: waiting 15 seconds for db:5432
db_1      | creating configuration files ... ok
db_1      | running bootstrap script ... ok
db_1      | performing post-bootstrap initialization ... ok
db_1      | syncing data to disk ... ok
db_1      | 
db_1      | WARNING: enabling "trust" authentication for local connections
db_1      | You can change this by editing pg_hba.conf or using the option -A, or
db_1      | --auth-local and --auth-host, the next time you run initdb.
db_1      | 
db_1      | Success. You can now start the database server using:
db_1      | 
db_1      |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1      | 
db_1      | waiting for server to start....2018-08-10 09:59:13.529 UTC [38] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1      | 2018-08-10 09:59:13.582 UTC [39] LOG:  database system was shut down at 2018-08-10 09:59:12 UTC
db_1      | 2018-08-10 09:59:13.599 UTC [38] LOG:  database system is ready to accept connections
db_1      |  done
db_1      | server started
db_1      | CREATE DATABASE
db_1      | 
db_1      | ALTER ROLE
db_1      | 
db_1      | 
db_1      | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1      | 
db_1      | 2018-08-10 09:59:14.585 UTC [38] LOG:  received fast shutdown request
db_1      | waiting for server to shut down....2018-08-10 09:59:14.593 UTC [38] LOG:  aborting any active transactions
db_1      | 2018-08-10 09:59:14.613 UTC [38] LOG:  worker process: logical replication launcher (PID 45) exited with exit code 1
db_1      | 2018-08-10 09:59:14.613 UTC [40] LOG:  shutting down
db_1      | 2018-08-10 09:59:14.660 UTC [38] LOG:  database system is shut down
db_1      |  done
db_1      | server stopped 
db_1      | 
db_1      | PostgreSQL init process complete; ready for start up.
db_1      | 
db_1      | 2018-08-10 09:59:14.726 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1      | 2018-08-10 09:59:14.726 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1      | 2018-08-10 09:59:14.729 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1      | 2018-08-10 09:59:14.806 UTC [65] LOG:  database system was shut down at 2018-08-10 09:59:14 UTC
db_1      | 2018-08-10 09:59:14.829 UTC [1] LOG:  database system is ready to accept connections
db_1      | 2018-08-10 09:59:15.217 UTC [72] LOG:  incomplete startup packet
aprrka_1  | wait-for-it.sh: db:5432 is available after 5 seconds
aprrka_1  | JJJJJJJJ
postgres_aprrka_1 exited with code 0

当我在另一个终端输入命令:psql -U postgres -h localhost -p 5559时,我可以连接到base1234,但是当输入 `\dt' 时我没有任何关系,所以我的 py 文件不会在 postgres 数据库中创建表。我认为与 postgres db 没有任何联系。请帮我

解答

您需要关闭游标并将提交所有挂起的事务到数据库才能使更改生效。

base.py的末尾添加以下 2 行:

cur.close()
conn.commit() 
Logo

云原生社区为您提供最前沿的新闻资讯和知识内容

更多推荐