为何Github Action步骤无法连接Docker容器?求解决方案
解决Github Actions中Cypress无法访问Docker容器的问题
出现「Connection refused」错误主要有两个核心原因:Jekyll服务绑定地址限制,或者Cypress测试启动过早(Jekyll还未完全就绪)。以下是具体解决步骤:
1. 修改Jekyll绑定地址,允许外部访问
Jekyll默认仅绑定容器内部的127.0.0.1,导致主机无法访问容器内的服务。需要在docker-compose的命令中添加--host 0.0.0.0,让Jekyll监听容器的所有网络接口:
修改后的docker-compose.yaml:
version: '2' services: jekyll: image: jekyll/jekyll:latest command: jekyll serve --watch --force_polling --verbose --host 0.0.0.0 ports: - 4000:4000 volumes: - .:/srv/jekyll
2. 拆分步骤,等待Jekyll服务完全就绪
直接用Cypress的start参数启动docker-compose后,Cypress会立刻开始测试,但此时Jekyll可能还在编译页面,尚未对外提供服务。需要单独拆分启动和等待步骤:
修改后的Github Actions配置:
name: Cypress Tests on: [push] jobs: cypress-run: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: 启动Jekyll容器 run: docker-compose up -d - name: 等待Jekyll服务就绪 run: | until curl -s http://localhost:4000 > /dev/null; do echo "等待Jekyll服务启动..." sleep 5 done - name: 运行Cypress测试 uses: cypress-io/github-action@v4.2.0
3. 可选:指定Cypress的baseUrl
在Cypress配置文件(如cypress.config.js)中明确设置测试的基础地址,避免地址识别错误:
const { defineConfig } = require('cypress') module.exports = defineConfig({ e2e: { baseUrl: 'http://localhost:4000', }, })
内容的提问来源于stack exchange,提问作者David Vos
相关产品推荐
相关产品推荐

