我正在尝试使用docker容器内的测试在gitlab CI上进行e2e测试。
我具有以下用于执行测试的docker-compose:
version: '3.5'
services:
api:
build:
dockerfile: Dockerfile.dev
context: ./
depends_on:
- db
ports:
- '${NODE_PORT}:${NODE_PORT}'
env_file:
- ./.env-ci
volumes:
- ./src:/usr/src/app/src
db:
image: postgres:11
container_name:
- prp_db
build:
dockerfile: Dockerfile.dev
context: ./database
container_name: pgsql
env_file:
- ./.env-ci
volumes:
- ./database/schema/base.sql:/docker-entrypoint-initdb.d/0.schema.sql
- ./database/schema/tables.sql:/docker-entrypoint-initdb.d/1.tables.sql
- ./database/fixtures/basics.sql:/docker-entrypoint-initdb.d/2.basics.sql
- ./database/fixtures/relations.sql:/docker-entrypoint-initdb.d/3.relations.sql
ports:
- '5432:5432'
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
以及以下gitlab-ci文件
image: docker/compose:latest
services:
- docker:dind
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ''
before_script:
- docker version
- docker-compose version
variables:
NODE_PORT: '3000'
DB_HOST: 'host.docker.internal'
DB_PORT: '5432'
DB_USERNAME: 'postgres'
DB_PASSWORD: 'postgres'
DB_DATABASE: 'prpp'
NODE_HOST: 'host.docker.internal'
POSTGRES_DB: prpp
POSTGRES_PASSWORD: 'postgres'
stages:
- test
test:
stage: test
script:
- docker-compose -p app -f docker-compose.ci.yml build
- docker-compose -p app -f docker-compose.ci.yml up --exit-code-from api
# test:
# stage: test
# cache: {}
我有几个使用supertest库的e2e测试。因此,我先启动单元测试,然后再进行集成测试:
docker-compose -f docker-compose.ci.yml up --exit-code-from api
这很好,但是当我看到gitlab上的构建时。看来nodejs服务器无法连接到数据库:
console.error
api_1 | { Error: getaddrinfo ENOTFOUND host.docker.internal host.docker.internal:5432
api_1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:58:26)
api_1 | errno: 'ENOTFOUND',
api_1 | code: 'ENOTFOUND',
api_1 | syscall: 'getaddrinfo',
api_1 | hostname: 'host.docker.internal',
api_1 | host: 'host.docker.internal',
api_1 | port: 5432 }
api_1 |
api_1 | 26 | });
api_1 | 27 | } catch (err) {
api_1 | > 28 | console.error(err);
这很奇怪,因为我的env变量与.env-ci文件相同:
DB_HOST=host.docker.internal
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=prpp
DB_DATABASE=prpp
NODE_HOST=0.0.0.0
NODE_PORT=3000
在此先感谢
答案 0 :(得分:0)
几周后,我找出了问题的根源,这是由于主机变量引起的。您需要使用使docker可以使用DNS。这是我的新环境文件:
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=prpp
DB_DATABASE=prpp
NODE_HOST=api
NODE_PORT=3000
DB_HOST=db
还有docker-compose ci:
version: '3.7'
services:
api:
container_name: api
build:
dockerfile: Dockerfile.test
context: ./
depends_on:
- db
ports:
- '3000:3000'
env_file:
- ./.env-ci
networks:
- main
volumes:
- ./src:/usr/src/app/src
db:
image: postgres:11
container_name:
- prp_db
build:
dockerfile: Dockerfile.dev
context: ./database
container_name: pgsql
env_file:
- ./.env-ci
volumes:
- ./database/schema/base.sql:/docker-entrypoint-initdb.d/0.schema.sql
- ./database/schema/tables.sql:/docker-entrypoint-initdb.d/1.tables.sql
- ./database/fixtures/basics.sql:/docker-entrypoint-initdb.d/2.basics.sql
- ./database/fixtures/relations.sql:/docker-entrypoint-initdb.d/3.relations.sql
ports:
- '5432:5432'
networks:
- main
restart: unless-stopped
networks:
main:
driver: bridge
gitlab-ci保持不变:)
快乐编码