testcontainers-go icon indicating copy to clipboard operation
testcontainers-go copied to clipboard

fix: preserve unix socket schema in testcontainersHostFromProperties

Open knqyf263 opened this issue 4 months ago • 1 comments

What does this PR do?

This PR fixes an issue where UNIX domain socket URLs configured in the tc.host property in ~/.testcontainers.properties would have their schema stripped, causing Docker client initialization to fail.

Changes made:

  • Modified testcontainersHostFromProperties function to preserve the original URL with schema after validation
  • Added test coverage for UNIX socket schema preservation in the properties file
  • Ensured backward compatibility for TCP hosts and other existing functionality

Technical details: The function now validates the URL format using parseURL() but returns the original URL instead of the parsed result, ensuring the Docker client receives the full URL with schema (e.g., unix:///var/run/docker.sock instead of just /var/run/docker.sock).

Initially, I considered modifying parseURL() to preserve the schema for UNIX sockets. However, this function is also used by dockerHostFromContext which expects only the path portion (without schema) for internal operations.

To avoid breaking existing functionality, I chose a more targeted approach: update testcontainersHostFromProperties now.

Why is it important?

When users configure UNIX domain sockets in ~/.testcontainers.properties using tc.host=unix:///var/run/docker.sock, the application would panic with:

panic: check host "/var/run/docker.sock": new client: unable to parse docker host `/var/run/docker.sock`

This occurs because:

  1. testcontainersHostFromProperties calls parseURL() which strips the unix:// schema
  2. The stripped path is passed to dockerHostCheck
  3. Docker client's ParseHostURL expects protocol://address format, but receives only the path
  4. Client initialization fails with a parsing error

This fix ensures UNIX socket configurations in ~/.testcontainers.properties work correctly while maintaining compatibility with existing TCP host configurations.

Related issues

If an issue should be created first, I'm happy to create one before merging this PR

knqyf263 avatar Jun 18 '25 09:06 knqyf263