Gitlab CI 中的 postgres docker 容器有问题

problem with postgres docker container inside Gitlab CI

我的项目在这个问题上受阻已经有几天了,它在本地主机上工作但在 gitlab 上不工作CI。 我想在 gitlabCI 中的 postgres docker 图像上构建一个测试数据库,但它不起作用,我尝试了很多东西并浪费了很多时间才问这个问题:' (. 在我的 docker-compose.yml 文件下方:

version: "3"
services: 
    nginx: 
        image: nginx:latest
        container_name: nginx
        depends_on: 
            - postgres
            - monapp
        volumes: 
            - ./nginx-conf:/etc/nginx/conf.d
            - ./util/certificates/certs:/etc/nginx/certs/localhost.crt
            - ./util/certificates/private:/etc/nginx/certs/localhost.key
        ports:
        - 81:80
        - 444:443
        networks:
            - monreseau
    monapp:
        image: monimage
        container_name: monapp
        depends_on: 
            - postgres
        ports:
            - "3000:3000"
        networks:
            - monreseau
        command: "npm run local"

    postgres:
        image: postgres:9.6
        container_name: postgres
        environment:
            POSTGRES_USER: postgres
            POSTGRES_HOST: postgres
            POSTGRES_PASSWORD: postgres      
        volumes: 
            - ./pgDatas:/var/lib/postgresql/data/
            - ./db_dumps:/home/dumps/
        ports:
            - "5432:5432"
        networks: 
            - monreseau
        
networks: 
    monreseau:

在我的 gitlab-ci.yml 文件下面:

stages:
  # - build
  - test

image: 
  name: docker/compose:latest

services:
- docker:dind

before_script:
- docker version
- docker-compose version 

variables:
  DOCKER_HOST: tcp://docker:2375/


# build:
#   stage: build
#   script:
#   - docker build -t monimage .
#   - docker-compose up -d

test:
  stage: test
  script :
  - docker build -t monimage .
  - docker-compose up -d
  - docker ps
  - docker exec -i postgres psql -U postgres -h postgres -f /home/dumps/test/dump_test_001 -c \q
  - exit
  - docker exec -i monapp ./node_modules/.bin/env-cmd -f ./env/.env.builded-test npx jasmine spec/auth_queries.spec.js
  - exit

这是docker ps登录gitlabCI服务器的内容:

docker ps on gitlab-CI

我认为将 postgres 放在主机上会起作用,但我总是在 gitlab-ci 终端中没有:

psql: could not connect to server: Connection refused
    Is the server running on host "postgres" (172.19.0.2) and accepting
    TCP/IP connections on port 5432?

我也尝试将 docker 放在主机上,但错误:

psql: could not translate host name "docker" to address: Name or service not known

little precision : 当我做 make builded-test 时,它正在我电脑的本地主机上工作 下面是我的 makefile:

builded-test:
    docker build -t monimage .
    docker-compose up -d
    docker ps
    docker exec -i postgres psql -U postgres -h postgres -f /home/dumps/test/dump_test_001 -c \q
    exit
    docker exec -i monapp ./node_modules/.bin/env-cmd -f ./env/.env.builded-test npx jasmine spec/auth_queries.spec.js
    exit
    docker-compose down

我想在我的docker-compose on gitlab CI中制作postgres图像来执行我的测试请帮助我:)提前感谢


更新

现在它在 gitlab-runner 中工作但在我推送时仍然不在 gitlab 上,我更新文件如下

我补充说:

variables:
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: ""
  POSTGRES_HOST_AUTH_METHOD: trust

并改变了

  test:
  stage: test
  script :
  - docker build -t monimage .
  - docker-compose up -d
  - docker ps
  - docker exec postgres psql -U postgres **-h postgres** -f /home/dumps/test/dump_test_001
  - docker exec monapp ./node_modules/.bin/env-cmd -f ./env/.env.builded-test npx jasmine spec/auth_queries.spec.js

在.gitlab-ci.yml

但是当我将它推送到 gitlab 时仍然不起作用,它给了我:

sql: could not connect to server: Connection refused
    Is the server running on host "postgres" (172.19.0.2) and accepting
    TCP/IP connections on port 5432?

有什么想法吗? :)

也许您需要等待 PostgreSQL 服务启动 运行。

你可以在尝试 psql 之前添加 10 秒的延迟吗?类似于:

 - sleep 10

如果可行,那么您可以使用更具体的解决方案来等待 PostgreSQL 初始化,例如