Search code examples
postgresqldockerdocker-composegitlab-cidocker-in-docker

problem with postgres docker container inside Gitlab CI


It's been few days I am blocked on this problem with my project, it's working on localhost but not on gitlabCI. I would like to build a test database on the postgres docker image in gitlabCI but it doesn't work, I have try a lot of things and lose a lot of hours before ask this there :'(. below my docker-compose.yml file :

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:

and below my gitlab-ci.yml file:

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

this is the content of docker ps log on gitlabCI server :

docker ps on gitlab-CI

I thought to put postgres on host would work, but no I always have in gitlab-ci terminal:

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?

I also tried to put docker on host but error :

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

little precision : it is working on localhost of my computer when i am doing make builded-test bellow my 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

I want to make work postgres image in my docker-compose on gitlab CI to execute my tests help me please :) thanks by advance


UPDATE

Now it working in gitlab-runner but still not on gitlab when I push, I update the files like following

I added :

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

and changed

  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

in the .gitlab-ci.yml

but still don't work when I push it to gitlab, it give me :

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?

any ideas ? :)


Solution

  • Maybe you need to wait for PostgreSQL service to be up and running.

    Can you add a 10 seconds delay before trying the psql stuff? Something like:

     - sleep 10
    

    If it works, then you can use a more specific solution to wait for PostgreSQL to be initialized, like Docker wait for postgresql to be running