在 CircleCI 中找不到 postgres_1 的容器

No container found for postgres_1 in CircleCI

我有 circleCI 脚本,其中 运行s docker-compose exec postgres pg_isready 命令在 Azure 容器中设置,但我们收到代码构建错误,如下所示。相同的脚本之前 运行 宁没问题,但从过去几天我们得到这个。

running docker-compose up
Creating network "docker_default" with the default driver
Pulling postgres (postgres:9.6)...
9.6: Pulling from library/postgres


Status: Downloaded newer image for postgres:9.6

Creating docker_myproject_1 ... 

Creating docker_postgres_1 ... 

waiting for postgres to startAttaching to docker_myproject_1
ERROR: No container found for postgres_1
myproject_1  | 2020-02-18T14:40:42.317723661Z starting...
.myproject_1  | 2020-02-18T14:40:43.718394751Z {"name":"myproject","hostname":"xxxxxx","pid":1,"level":30,"msg":"myproject app version v2.8 listening on port 3030 (Node.js version 8.17.0, ci environment)","time":"2020-02-18T14:40:43.717Z","v":0}
myproject_1  | 2020-02-18T14:40:43.718573206Z myproject app version v2.8 listening on port 3030 (Node.js version 8.17.0, ci environment)
myproject_1  | 2020-02-18T14:40:43.793220191Z Failed to prune sessions: getaddrinfo ENOTFOUND postgres postgres:5432
ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.ERROR: No container found for postgres_1
.Too many attempts

CircleCI 配置文件 --- config.yml

version: 2
jobs:
  build:
    working_directory: /home/circleci/app

    docker:
      - image: circleci/node:8-browsers
      - image: circleci/postgres:9.6-alpine
        environment:
          POSTGRES_USER: postgres
          POSTGRES_DB: myproject_ci

    steps:
      # Prepare build environment
      - setup_remote_docker:
          reusable: true
      - checkout
      - restore_cache:
          key: dependency-cache-v2-{{ checksum "package.json" }}

      # Install dependencies + cache them
      - run: npm install
      - save_cache:
          key: dependency-cache-v2-{{ checksum "package.json" }}
          paths:
            - node_modules

      # Build
      - run: CONFIG_ENV=ci npm run build

      # Start for testing
      - run:
          command: ./scripts/run-ci.sh
          background: true
      - run: ./scripts/run-ci-wait.sh

      # Run tests
      - run: CONFIG_ENV=ci npm test

      # Run tests for docker image
      - run: ./test/docker/run-docker-tests.sh

      - deploy:
          name: Deploy master to dev
          command: |
            if [ "${CIRCLE_BRANCH}" == "master" ]; then
              ./scripts/deploy-dev.sh
            fi

运行-docker-tests.sh 的内容失败

#!/bin/bash
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR

function cleanup() {
  docker-compose down || true
}
trap cleanup SIGINT SIGTERM EXIT

echo 'running docker-compose down'
docker-compose down
echo 'running docker-compose up'
docker-compose up -d --force-recreate
docker-compose logs --follow --timestamps --no-color myproject &

printf 'waiting for postgres to start'
attempt=0
until docker-compose exec postgres pg_isready; do
  printf '.'
  sleep 2
  attempt=$(( $attempt + 1 ))
  if [ $attempt -gt 15 ]; then
    echo Too many attempts
    exit 1
  fi
done
echo 'postgres is started'

printf 'waiting for server to start'
attempt=0
until docker-compose run -T myproject curl --output /dev/null --silent --head --fail http://myproject:3030; do
  printf '.'
  sleep 2
  attempt=$(( $attempt + 1 ))
  if [ $attempt -gt 15 ]; then
    echo Too many attempts
    exit 1
  fi
done
echo 'server is started'


上周使​​用相同脚本成功构建。

Status: Downloaded newer image for postgres:9.6

Creating docker_postgres_1 ... 

Creating docker_myproject_1 ... 

waiting for postgres to startAttaching to docker_myproject_1
myproject_1  | 2020-02-14T14:57:56.436816113Z starting...
^@^@/var/run/postgresql:5432 - no response
myproject_1  | 2020-02-14T14:57:58.305638546Z {"name":"myproject","hostname":"xxxxxx","pid":1,"level":30,"msg":"myproject app version v2 listening on port 3030 (Node.js version 8.17.0, ci environment)","time":"2020-02-14T14:57:58.305Z","v":0}
myproject_1  | 2020-02-14T14:57:58.306243774Z myproject app version v2 listening on port 3030 (Node.js version 8.17.0, ci environment)
.myproject_1  | 2020-02-14T14:57:58.413266214Z Failed to prune sessions: connect ECONNREFUSED x.x.x.x:5432
/var/run/postgresql:5432 - no response
./var/run/postgresql:5432 - accepting connections
postgres is started
waiting for server to startserver is started

撰写文件

version: "3"

services:
  myproject:
    image: myproject:latest
    environment:
      - CONFIG_ENV=ci
      - CONFIG_PASSWORD
      - POSTGRES_HOST=postgres
  postgres:
    image: postgres:9.6
    environment:
      - POSTGRES_DB=myproject_ci

这里有任何帮助都将非常有用。

在您的 docker-compose.yml 文件中,请检查是否在 environment 参数下提供。 如果未提供,请提供如下内容:- POSTGRES_HOST_AUTH_METHOD=trust.

这个解决方案对我们有用,这个构建问题的原因应该是 Docker Postgres 的官方图像最近更新了,这导致了以前的一系列新问题正在工作的 PG 容器现在在初始化时抛出错误。

更多信息在这里: https://github.com/docker-library/official-images/commit/044fa382fb4ce789c44636bf5ca1b4aca253fa0f#diff-24ff720298244763c283810044b398c8