簡體   English   中英

Docker Compose中連接數據庫的問題

[英]Troubles with connect to database in 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);")

Dockerfile:

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

泊塢窗,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"]

wait-fot-it.sh:

鏈接到github中的代碼

docker-compose up之后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

當我在另一個終端中鍵入命令: psql -U postgres -h localhost -p 5559 ,我可以連接到base1234 ,但是當鍵入\\ dt時,我沒有任何關系,因此我的py文件不在postgres數據庫中創建表。 我認為與postgres db不存在連接。 請幫我

您需要關閉游標並將所有未決事務提交到數據庫,以使更改生效。

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

cur.close()
conn.commit() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM