Docker Compose中连接数据库的问题

时间:2018-08-10 10:26:32

标签: postgresql docker docker-compose psycopg2

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

base.py:

(6,)

Dockerfile:

%timeit (m[:, None, :]*v[:, None]).reshape(m.shape[0], -1)
# 5.93 µs ± 77.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit np.einsum('ij,k->ikj', m, v).reshape(m.shape[0], -1)
# 5.44 µs ± 61.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit np.array([np.outer(v, m_row).flatten() for m_row in m])
# 179 µs ± 18.9 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

docker-compose.yml

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);")

wait-fot-it.sh:

link to code in github

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"] 之后,我有这个输出:

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"]

当我在另一个终端中键入命令:docker-compose up时,我可以连接到... 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 ,但是当键入`\ dt'时,我没有任何关系,因此我的py文件不在postgres中创建表数据库。我认为与postgres db不存在连接。请帮我

1 个答案:

答案 0 :(得分:1)

您需要关闭游标并将commit所有待处理的事务发送到数据库,以使更改生效。

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

cur.close()
conn.commit()