title: Running TestContainers on Jenkins inside Docker image
body: <p>Recently, I've been trying to figure out how to run out integration tests using a TestContainers on Jenkins where they are ran inside another Docker image, but without any success.</p>
  
  <p>TestContainers version: 1.13.0</p>
  
  <p>Command that gets executed on Jenkins looks similar to this one (I've removed some env variables and private data):</p>
  
  <pre class="lang-sh prettyprint-override"><code>docker run --rm -v /home/jenkins/.m2:/root/.m2 -v /home/jenkins/.sbt:/root/.sbt -v /home/jenkins/.ivy2:/root/.ivy2 -v /home/jenkins/workspace/re_FC-5370-create-dummy-endpoint@2:/home/jenkins/workspace/re_FC-5370-create-dummy-endpoint@2 -w /home/jenkins/workspace/re_FC-5370-create-dummy-endpoint@2 -v /var/run/docker.sock:/var/run/docker.sock registry/build-java-app-image ./ci/test.sh
  </code></pre>
  
  <p>So, socket is exposed, <code>$PWD</code> is mounted and set as workspace as described here: <a href="https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/" rel="nofollow noreferrer">https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/</a> but I'm still getting an error.</p>
  
  <p>Now, there are two types of errors. First one is <code>NoRouteToHostException</code> which happens if in <code>test.sh</code> I simply run <code>./gradlew test</code> with this setup:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Container
  private static PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer("postgres:12.2-alpine");
  
  static class Initializer
          implements ApplicationContextInitializer&lt;ConfigurableApplicationContext&gt; {
      public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
          TestPropertyValues.of(
                  "spring.datasource.url=" + postgresqlContainer.getJdbcUrl(),
                  "spring.datasource.username=" + postgresqlContainer.getUsername(),
                  "spring.datasource.password=" + postgresqlContainer.getPassword(),
                  "spring.datasource.driverClassName=" + postgresqlContainer.getDriverClassName()
          ).applyTo(configurableApplicationContext);
      }
  }
  </code></pre>
  
  <p><em>NOTE:</em> This works locally from terminal, simple run from IntelliJ and even from Docker image.</p>
  
  <p>Second error happens if I try to setup a Docker networking in <code>test.sh</code> script:</p>
  
  <pre class="lang-sh prettyprint-override"><code>docker network create --driver bridge "build-$HOSTNAME"
  docker network connect "build-$HOSTNAME" $outerContainerId
  
  export dockerNetworkId=$(docker network ls --no-trunc --filter name="build-$HOSTNAME" --format "{{.ID}}")
  </code></pre>
  
  <p>Setup that uses an exposed network ID:</p>
  
  <pre class="lang-java prettyprint-override"><code>private static final String networkId = System.getenv("dockerNetworkId");
  
  @Container
  private static PostgreSQLContainer postgresqlContainer = (PostgreSQLContainer)
          new PostgreSQLContainer("postgres:12.2-alpine")
                  .withNetwork(Network.builder().id(networkId).build())
                  .withNetworkAliases("test-database-container");
  
  static class Initializer
          implements ApplicationContextInitializer&lt;ConfigurableApplicationContext&gt; {     
      public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
          TestPropertyValues.of(
              "spring.datasource.url=jdbc:postgresql://test-database-container:5432/test",
              "spring.datasource.username=" + postgresqlContainer.getUsername(),
              "spring.datasource.password=" + postgresqlContainer.getPassword(),
              "spring.datasource.driverClassName=" + postgresqlContainer.getDriverClassName()
          ).applyTo(configurableApplicationContext);
      }
  }
  </code></pre>
  
  <p>So, I've created a network, linked it to a Jenkins container's network and exposed its ID so that I can fetch it in tests. In tests, I've created a container with that network ID and gave it a name so I can reference it in the URL.</p>
  
  <p>This setup gives me <code>UnknownHostException</code> with message: <code>test-database-container</code> and I cannot figure out what's bothering it.</p>
  
  <p>I've tried also some other variations where I'd setup a network, but use first approach or wouldn't setup a network and used second approach with Docker IP that TestContainers is returning instead of a network alias, but nothing worked. I always get one of those 2 exceptions.</p>