在Docker(Codeship)中运行WebdriverIO遇到的问题
Hey Max, let's work through your WebdriverIO + Cucumber Docker issue step by step—this is a super common pain point with containerized E2E testing, but we can nail it down.
The Java install fail is probably tied to your base Docker image choice. Different distros use different package managers, so let's align your Dockerfile with the right commands:
- If you're using a Debian/Ubuntu-based Node image (like
node:18-bullseye), use this to install OpenJDK:RUN apt-get update && \ apt-get install -y --no-install-recommends openjdk-11-jre-headless curl && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk-amd64 - If you're using an Alpine-based Node image (lighter weight, but stricter), use:
RUN apk add --no-cache openjdk11-jre-headless curl ENV JAVA_HOME /usr/lib/jvm/java-11-openjdk
The --no-install-recommends and cleanup steps keep your image small, and setting JAVA_HOME ensures WebdriverIO can find Java correctly.
Even if you added the service, misconfigs often break containerized tests. Here's a validated setup for your wdio.conf.js:
exports.config = { // ... your existing config logLevel: 'debug', // Turn this on to get more detailed error logs services: [ ['docker', { containers: [{ image: 'selenium/standalone-chrome:114.0', // Match your WebdriverIO version portBindings: { '4444/tcp': 4444 }, healthCheck: 'curl -s http://localhost:4444/wd/hub/status | grep "ready"', healthCheckInterval: 500, startupTimeout: 30000 // Give Selenium time to boot up }] }] ], capabilities: [{ browserName: 'chrome', 'goog:chromeOptions': { args: ['--no-sandbox', '--headless', '--disable-gpu', '--window-size=1920,1080'] // --no-sandbox is critical for Chrome to run in Docker } }], baseUrl: 'http://your-react-container-name:3000' // Use container name, not localhost! };
Key notes:
- Never use
localhostfor your React app URL inside Docker—use the container name (from Docker Compose) or internal IP, since each container has its ownlocalhost. - The
--no-sandboxflag is non-negotiable for Chrome in containers (Docker doesn't provide full OS sandboxing).
Local vs Docker discrepancies are usually the root cause:
- Match your Node version exactly: Use the same Node image tag in your Dockerfile as you have locally (e.g.,
node:18-bullseyeinstead of justnode). - Ensure dependencies are fully installed: Use
npm ciinstead ofnpm installin your Dockerfile to get exact dependency versions frompackage-lock.json. - Wait for your React app to be ready: If your React app is also in Docker, use Docker Compose's
depends_onwith health checks to avoid running tests before the app is live. Exampledocker-compose.yml:version: '3.8' services: react-app: build: ./your-react-app-folder command: npm start healthcheck: test: ["CMD", "curl", "-f", "http://localhost:3000"] interval: 5s timeout: 5s retries: 5 e2e-tests: build: ./your-e2e-folder depends_on: react-app: condition: service_healthy environment: - BASE_URL=http://react-app:3000
If you're still stuck, debug inside the container:
- Spin up the container in interactive mode:
docker run -it --rm <your-image> bash - Run your test command manually:
npm run wdio - Check the exact error logs—this will tell you if it's a Selenium connection issue, Chrome startup fail, or something else.
- Verify Selenium is reachable: Inside the test container, run
curl http://localhost:4444/wd/hub/status—you should get a JSON response sayingready: true.
Let me know if you share the specific error messages (from Java install or WebdriverIO runs) and I can help you drill down even further!
内容的提问来源于stack exchange,提问作者MaxKalb

