简体   繁体   English

无法基于postgres连接到容器

[英]Can not connect to the container based on postgres

I am new to docker container. 我是Docker容器的新手。 I am trying to unit test my Flask application on Circle CI automatically. 我正在尝试在Circle CI上自动对Flask应用程序进行单元测试。 However it can not connect to postgres container. 但是,它无法连接到postgres容器。 It works in my local computer (macOS Sierra). 它可以在我的本地计算机(macOS Sierra)中工作。 Let me know if you need more information to solve this issue. 让我知道您是否需要更多信息来解决此问题。 Thank you!! 谢谢!!

docker-compose.yml 泊坞窗,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"

config.yml 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

Circle Ci build log Circle 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

I think the problem is that the postgres service is not fully up when your web app starts. 我认为问题在于,当您的Web应用程序启动时,postgres服务还没有完全启动。 Based on your comment about it working after adding the sleep timer, it seems this is the problem. 根据您对添加睡眠计时器后其工作的评论,看来这是问题所在。

You can run a container called dadarek/wait-for-dependencies as a mechanism to wait for services to be up(in your case, postgres). 您可以运行一个名为dadarek / wait-for-dependencies的容器作为一种机制来等待服务启动(在您的情况下为postgres)。

Here is how you can implement it: 这是实现它的方法:

1). 1)。 Add a new service to your docker-compose.yml 向您的docker-compose.yml添加新服务

  waitfordb:
    image: dadarek/wait-for-dependencies
    depends_on:
      - postgres 
    command: postgres:5432

Your docker-compose.yml should look now look like this: 您的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). 2)。 Startup compose 启动撰写

docker-compose run --rm waitfordb
docker-compose up -d web postgres

The result is that your web service should now wait for port 5432 to be up in your postgres container, before trying to start up. 结果是您的web服务现在应在尝试启动之前,等待端口5432在postgres容器中启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM