testcontainers / testcontainers/testcontainers-java

[Bug]: Testcontainers ignores Docker proxies settings

Open
#5,981 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type/bug
Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

Module

Core

Testcontainers version

1.17.5

Using the latest Testcontainers version?

Yes

Host OS

Linux

Host Arch

x86_64

Docker version
Client: Docker Engine - Community
 Version:           20.10.9
 API version:       1.41
 Go version:        go1.16.8
 Git commit:        c2ea9bc
 Built:             Mon Oct  4 16:08:29 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.9
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.8
  Git commit:       79ea9d3
  Built:            Mon Oct  4 16:06:37 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.11
  GitCommit:        5b46e404f6b9f661a205e28d59c982d3634148f8
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
What happened?

It seems that Testcontainers doesn't load HTTPS_PROXY and HTTP_PROXY environment variables inside the container build process whereas Docker successfully include those variables when it builds images itself.

I have an authenticated enterprise proxy which I set up on my local machine using the ~/.docker/config.json file

{
 "proxies":
 {
   "default":
   {
     "httpProxy": "http://myproxyuser:myproxypassword@proxy.example.com:3128",
     "httpsProxy": "http://myproxyuser:myproxypassword@proxy.example.com:3128",
     "noProxy": "localhost,127.0.0.1,docker-registry.example.com,.corp"
   }
 }
}

as well as using a /etc/systemd/system/docker.service.d/http-proxy.conf file which looks like

[Service]
Environment="HTTP_PROXY=http://myproxyuser:myproxypassword@proxy.example.com:3128"
Environment="HTTPS_PROXY=http://myproxyuser:myproxypassword@proxy.example.com:3128"
Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"

I developed the following project to illustrate my problem : when I run ./build-using-docker.sh https://proxy.example.com/proxy.crt it launches a ./mvnw --settings /home/myuser/.m2/settings.proxy.xml clean package command inside a Docker container run like this docker run -it --rm -u 1000:998 -v /home/myuser/myprojects/testcontainers-proxy-issue:/home/myuser/myprojects/testcontainers-proxy-issue -w /home/myuser/myprojects/testcontainers-proxy-issue -v /home/myuser/.m2:/home/myuser/.m2 -v /var/run/docker.sock:/var/run/docker.sock -e 'MAVEN_OPTS=-Dhttps.proxyHost=proxy.example.com -Dhttps.proxyPort=3128 -Dhttps.proxyUser=myproxyuser -Dhttps.proxyPassword=myproxypassword -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=3128 -Dhttp.proxyUser=myproxyuser -Dhttp.proxyPassword=myproxypassword -Djavax.net.ssl.trustStore=./proxy-data/cacerts -Duser.home=/home/myuser' -v /home/myuser/.docker/config.json:/home/myuser/.docker/config.json -e DOCKER_CONFIG=/home/myuser/.docker eclipse-temurin:17-jdk-alpine sh -cex 'cp /opt/java/openjdk/lib/security/cacerts ./proxy-data/ && keytool -importcert -alias proxy.crt -file ./proxy-data/proxy.crt -trustcacerts -keystore ./proxy-data/cacerts -storepass changeit -noprompt; ./mvnw --settings /home/myuser/.m2/settings.proxy.xml clean package'.

Then Maven launches the tests of the projects which executes findAllTest() test which launches @ExtendWith(OpenldapTestContainerIntegrationTestExtension.class) before, which executes the following code:

        final var imageFromDockerfile = new ImageFromDockerfile()
                .withFileFromPath(".", Path.of("src", "main", "docker"));

The targetted Dockerfile only contains this:

FROM bitnami/openldap:2.6

RUN test -n "$HTTPS_PROXY" || exit 42

Here is the JUnit failing log:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.959 s <<< FAILURE! - in com.github.anthony_o.testcontainers_proxy_issue.repository.UserRepositoryIntegrationTest
[ERROR] com.github.anthony_o.testcontainers_proxy_issue.repository.UserRepositoryIntegrationTest  Time elapsed: 1.957 s  <<< ERROR!
org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=<resolving>, imagePullPolicy=DefaultPullPolicy(), imageNameSubstitutor=org.testcontainers.utility.ImageNameSubstitutor$LogWrappedImageNameSubstitutor@4a0df195)
Caused by: com.github.dockerjava.api.exception.DockerClientException: Could not build image: The command '/bin/bash -o pipefail -c test -n "$HTTPS_PROXY" || exit 42' returned a non-zero code: 42

Whereas if I simply run the following Docker command, it succeed:

docker build src/main/docker/ -t testcontainers-proxy-issue --no-cache
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM bitnami/openldap:2.6
 ---> eb7e0974d701
Step 2/2 : RUN test -n "$HTTPS_PROXY" || exit 42
 ---> Running in e298e31d18f4
Removing intermediate container e298e31d18f4
 ---> 5a87cf389f41
Successfully built 5a87cf389f41
Successfully tagged testcontainers-proxy-issue:latest
Relevant log output

No response

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction, especially OpenldapTestContainerIntegrationTestExtension.java, UserRepositoryIntegrationTest.java, and its Dockerfile. Run findAllTest() and compare the ImageFromDockerfile build with the direct docker build shown in the report. Done means the Dockerfile's HTTPS_PROXY check succeeds when the image is built through Testcontainers and the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java
Domain
devops, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.