我有一个看似如下的docker compose文件:
version: "3" services: redis: image: 'redis:3.2.7' # command: redis-server --requirepass redispass postgres: image: postgres:9.6 ports: - "5432:5432" environment: - POSTGRES_USER=airflow - POSTGRES_PASSWORD=airflow - POSTGRES_DB=airflow # - PGDATA=/var/lib/postgresql/data webserver: image: airflow:develop depends_on: - postgres ports: - "8080:8080" command: - webserver
运行docker-compose up
后,我看到所有服务都已启动,看起来运行良好。我的webserver
服务使用以下sqlalchemy连接字符串连接到postgres:postgresql+psycopg2://airflow:airflow@postgres/airflow
我希望能够检查从我的主机创建的postgres数据库。我尝试了psql
的几个变体,但它们都失败了:
8:49AM /Users/paymahn/solvvy/scheduler gitlab-ci ✚ ◼ ❮❮❮ psql -h postgres -p 5432 -U airflow psql: could not translate host name "postgres" to address: nodename nor servname provided, or not known 8:50AM /Users/paymahn/solvvy/scheduler ✘ 2 gitlab-ci ✚ ◼ ❯❯❯ psql -h localhost -p 5432 -U airflow psql: FATAL: database "airflow" does not exist 8:51AM /Users/paymahn/solvvy/scheduler ✘ 2 gitlab-ci ✚ ◼ ❯❯❯ psql -h localhost -p 5432 -U postgres psql: FATAL: role "postgres" does not exist
如何从运行容器的主机连接到postgres容器内的db?
编辑:在接受的解决方案的评论中回答了这个问题,但我认为值得指出:我已经在本地运行了postgres,这引起了各种各样的困惑。10:12AM /Users/paymahn/solvvy/scheduler gitlab-ci ✚ ◼ ❯❯❯ ps aux | rg postgres paymahn 11705 0.0 0.0 4485040 596 ?? Ss 13Feb18 0:00.17 postgres: bgworker: logical replication launcher paymahn 11704 0.0 0.0 4339972 680 ?? Ss 13Feb18 0:13.88 postgres: stats collector process paymahn 11703 0.0 0.0 4485040 1404 ?? Ss 13Feb18 0:02.72 postgres: autovacuum launcher process paymahn 11702 0.0 0.0 4486064 116 ?? Ss 13Feb18 0:06.42 postgres: wal writer process paymahn 11701 0.0 0.0 4476848 204 ?? Ss 13Feb18 0:02.62 postgres: writer process paymahn 11700 0.0 0.0 4486064 228 ?? Ss 13Feb18 0:00.58 postgres: checkpointer process paymahn 11698 0.0 0.0 4478128 592 ?? S 13Feb18 0:12.09 /usr/local/Cellar/postgresql/10.2/bin/postgres -D /usr/local/var/postgres paymahn 92143 0.0 0.0 4262040 24 s011 S+ 10:12AM 0:00.00 rg postgres
杀死我的本地postgres实例并更新我接受的答案中提到的主机文件,清除了所有内容。
答案 0 :(得分:1)
只需在本地PC上将127.0.0.1 postgres
添加到hosts
文件,然后使用命令psql -h postgres -p 5432 -U airflow