Python 无法连接到基于postgres的容器
我是docker集装箱公司的新手。我正在尝试在Circle CI上自动对我的Flask应用程序进行单元测试。但是,它无法连接到postgres容器。它在我的本地计算机(macOS Sierra)中工作。如果您需要更多信息来解决此问题,请告诉我。谢谢 docker-compose.ymlPython 无法连接到基于postgres的容器,python,postgresql,docker,docker-compose,Python,Postgresql,Docker,Docker Compose,我是docker集装箱公司的新手。我正在尝试在Circle CI上自动对我的Flask应用程序进行单元测试。但是,它无法连接到postgres容器。它在我的本地计算机(macOS Sierra)中工作。如果您需要更多信息来解决此问题,请告诉我。谢谢 docker-compose.yml version: '3' services: web: container_name: web build: ./web ports: - "5000:5000"
version: '3'
services:
web:
container_name: web
build: ./web
ports:
- "5000:5000"
depends_on:
- postgres
volumes:
- ./web/.:/app
tty: true
postgres:
container_name: postgres
build: ./db
ports:
- "5432:5432"
waitfordb:
image: dadarek/wait-for-dependencies
depends_on:
- postgres
command: postgres:5432
config.yml
version: 2
jobs:
build:
machine: true
working_directory: ~/repo
steps:
- checkout
- run:
name: Install Docker Compose
command: |
sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
- run:
name: Start container and veryfy it's working
command: |
set -x
cd ~/repo/docker
docker-compose up --build -d
- run:
name: Run test
command: |
cd ~/repo/docker
docker-compose run web python tests/test_therapies.py
循环Ci生成日志
connection = pool._invoke_creator(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "postgres" (172.18.0.2) and accepting
TCP/IP connections on port 5432?
----------------------------------------------------------------------
Ran 1 test in 0.025s
FAILED (errors=1)
Exited with code 1
我认为问题在于,当你的网络应用启动时,postgres服务并没有完全启动。根据你在添加睡眠计时器后对其工作的评论,似乎这就是问题所在 您可以运行一个称为机制的容器来等待服务启动(在您的例子中是postgres) 以下是您如何实现它: 1) 。向docker-compose.yml添加新服务
version: '3'
services:
web:
container_name: web
build: ./web
ports:
- "5000:5000"
depends_on:
- postgres
volumes:
- ./web/.:/app
tty: true
postgres:
container_name: postgres
build: ./db
ports:
- "5432:5432"
waitfordb:
image: dadarek/wait-for-dependencies
depends_on:
- postgres
command: postgres:5432
您的docker-compose.yml现在应该如下所示:
version: '3'
services:
waitfordb:
image: dadarek/wait-for-dependencies
depends_on:
- postgres
command: postgres:5432
web:
container_name: web
build: ./web
ports:
- "5000:5000"
depends_on:
- waitfordb
- postgres
volumes:
- ./web/.:/app
tty: true
postgres:
container_name: postgres
build: ./db
ports:
- "5432:5432"
2) 。启动组合
docker-compose run --rm waitfordb
docker-compose up -d web postgres
结果是,您的
web
服务现在应该等待端口5432在您的postgres容器中启动,然后再尝试启动。我认为问题在于,当您的web应用启动时,postgres服务没有完全启动。根据你在添加睡眠计时器后对其工作的评论,似乎这就是问题所在
您可以运行一个称为机制的容器来等待服务启动(在您的例子中是postgres)
以下是您如何实现它:
1) 。向docker-compose.yml添加新服务
version: '3'
services:
web:
container_name: web
build: ./web
ports:
- "5000:5000"
depends_on:
- postgres
volumes:
- ./web/.:/app
tty: true
postgres:
container_name: postgres
build: ./db
ports:
- "5432:5432"
waitfordb:
image: dadarek/wait-for-dependencies
depends_on:
- postgres
command: postgres:5432
您的docker-compose.yml现在应该如下所示:
version: '3'
services:
waitfordb:
image: dadarek/wait-for-dependencies
depends_on:
- postgres
command: postgres:5432
web:
container_name: web
build: ./web
ports:
- "5000:5000"
depends_on:
- waitfordb
- postgres
volumes:
- ./web/.:/app
tty: true
postgres:
container_name: postgres
build: ./db
ports:
- "5432:5432"
2) 。启动组合
docker-compose run --rm waitfordb
docker-compose up -d web postgres
结果是,您的
web
服务现在应该等待端口5432在您的postgres容器中启动,然后再尝试启动。我在docker compose up--build-d
之后添加了sleep 20
。它起作用了!!然而,解决方案本质上并不是……我在docker compose up--build-d之后添加了sleep 20
。它起作用了!!然而,解决方案本质上不是。。。