我正在尝试在GitLab CI中运行单元测试。对于我的测试设置,我正在使用docker-compose启动包含三个neo4j实例的数据库集群,然后通过本地主机在docker主机上运行测试。
在我的机器上,它运行良好,因此我尝试在GitLab CI上执行相同的操作。我最初在使docker-compose在CI容器中运行时遇到了一些问题,但是我设法通过以下.gitlab-ci.yaml来实现:
image: "tiangolo/docker-with-compose:latest"
before_script:
- apk add --update nodejs npm
- npm install npm@latest -g
- npm install
services:
- docker:dind
run-test:
script:
- docker-compose up -d --build
- npm test
安装依赖项,docker-compose成功启动数据库,npm test
运行我的单元测试。到现在为止还挺好。 但是所有使用某种形式的数据库连接的测试都失败了,因为它们无法通过localhost:7687进行访问。我收到以下错误消息
Neo4jError: Could not perform discovery. No routing servers available. Known routing table: RoutingTable[database=default database, expirationTime=0, currentTime=1588275075260, routers=[], readers=[], writers=[]]
请记住,我在本地计算机上使用了相同的docker-compose.yaml,并且一切正常,因此数据库配置应该不会有问题。
我认为我的测试在数据库完全启动之前就已经在运行,因此我像这样添加了sleep 120
run-test:
script:
- docker-compose up -d --build
- sleep 120
- npm test
我什至测量了群集在CI容器中启动所需的时间,并选择了适当的超时时间,但是没有运气,测试仍然失败。
然后我看了看docker网络,看那里是否出了问题
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 docker 3590cde9c5df runner-k3xtxnwc-project-17881831-concurrent-0-6d9d78b64ad6df95-docker-0
172.17.0.3 runner-k3xtxnwc-project-17881831-concurrent-0
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' core1
172.19.0.2
$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' core2
172.19.0.4
但这对我来说很好。然后,我尝试用localhost:7678
,docker:7678
甚至172.0.0.1
替换$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' core1):7678
。但是它们都不起作用。
尝试curl localhost:7474
(neo4j http控制台)将返回Failed to connect to localhost port 7474: Connection refused
编辑:
我的docker-compose.yaml
version: '3'
services:
dbc1:
build: ./database
container_name: core1
hostname: core1
ports:
- 7474:7474
- 7687:7687
environment:
- NEO4J_dbms_mode=CORE
- NEO4J_causal__clustering_minimum__core__cluster__size__at__formation=3
- NEO4J_causal__clustering_minimum__core__cluster__size__at__runtime=3
- NEO4J_causal__clustering_initial__discovery__members=core1:5000,core2:5000,core3:5000
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
- NEO4J_dbms_connector_bolt_advertised__address=localhost:7687
- NEO4J_dbms_connector_http_advertised__address=localhost:7474
volumes:
- dbdata1:/data
ulimits:
nofile:
soft: 40000
hard: 40000
dbc2:
build: ./database
container_name: core2
hostname: core2
ports:
- 8474:7474
- 8687:7687
environment:
- NEO4J_dbms_mode=CORE
- NEO4J_causal__clustering_minimum__core__cluster__size__at__formation=3
- NEO4J_causal__clustering_minimum__core__cluster__size__at__runtime=3
- NEO4J_causal__clustering_initial__discovery__members=core1:5000,core2:5000,core3:5000
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
- NEO4J_dbms_connector_bolt_advertised__address=localhost:8687
- NEO4J_dbms_connector_http_advertised__address=localhost:8474
volumes:
- dbdata2:/data
ulimits:
nofile:
soft: 40000
hard: 40000
dbc3:
build: ./database
container_name: core3
hostname: core3
ports:
- 9474:7474
- 9687:7687
environment:
- NEO4J_dbms_mode=CORE
- NEO4J_causal__clustering_minimum__core__cluster__size__at__formation=3
- NEO4J_causal__clustering_minimum__core__cluster__size__at__runtime=3
- NEO4J_causal__clustering_initial__discovery__members=core1:5000,core2:5000,core3:5000
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
- NEO4J_dbms_connector_bolt_advertised__address=localhost:9687
- NEO4J_dbms_connector_http_advertised__address=localhost:9474
volumes:
- dbdata3:/data
ulimits:
nofile:
soft: 40000
hard: 40000
volumes:
dbdata1:
driver: local
dbdata2:
driver: local
dbdata3:
driver: local
我的数据库Dockerfile(位于./database中):
FROM graphfoundation/ongdb:3.6
RUN echo "* soft nofile 40000" >> /etc/security/limits.conf
RUN echo "* hard nofile 40000" >> /etc/security/limits.conf
## inject password change into startup script
# create alternative startup script
RUN echo "#!/bin/bash -eu" >> /docker-entrypoint-modified.sh
# add command to change password
RUN echo "bin/neo4j-admin set-initial-password testpassword" >> /docker-entrypoint-modified.sh
# copy the contents of docker-entrypoint.sh, but skip the first line
RUN tail -n +2 /docker-entrypoint.sh >> /docker-entrypoint-modified.sh
# replace docker-entrypoint.sh with docker-entrypoint-modify
RUN cat /docker-entrypoint-modified.sh > /docker-entrypoint.sh
EXPOSE 7474 7473 7687
答案 0 :(得分:2)
结果是我对docker-in-docker有一些误解,我通过更改.gitlab-ci.yaml
中的以下行来将localhost“重新路由”到docker-in-docker服务中来解决了这个问题:
services:
- docker:dind
到
services:
- name: docker:dind
alias: localhost