title: Testcontainers, Docker in Docker with custom network, Containers don't belong to network
body: <p>I'm trying to get Testcontainers running on TeamCity using a Docker builder image.</p>
  
  <p>The test runs fine locally (not inside the builder image). And only partially within the builder image on TeamCity. I followed the <a href="https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/" rel="nofollow noreferrer">guide on DinD</a> but there are no examples on how a docker network comes into play.</p>
  
  <p>The way we start the build in TeamCity (note the --network param, ryuk is disabled as it had connection issues):</p>
  
  <pre><code>docker network create --driver bridge custom_network
  
  docker run --rm -it -v $PWD:$PWD -w $PWD \
    --privileged \
    --network=custom_network \
    -e TESTCONTAINERS_RYUK_DISABLED=true \
    -e _JAVA_OPTIONS="" \
    -e DOCKER_HOST="unix:///var/run/docker.sock" \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v /home/teamcity/.docker:/home/java/.docker
    -v /local/maven/cache/repository:/opt/m2/repository \
    registry.ch/java:11-builder \
    mvn verify
  </code></pre>
  
  <p>The build runs quite normal: the junit test starts, the custom oracle-xe image we use is downloaded, and the log suggests it is started. But locally I can see that testcontainers is polling to create a connection, on TeamCity the build just continues and runs into an error:</p>
  
  <pre><code>[14:01:07] :     [Step 3/3] 14:01:07.006 [tc-okhttp-stream-276714561] DEBUG com.github.dockerjava.core.command.PullImageResultCallback - ResponseItem(stream=null, status=Extracting, progressDetail=ResponseItem.ProgressDetail(current=625569807, total=625569807, start=null), progress=[==================================================&gt;]  625.6MB/625.6MB, id=2538d1d7e815, from=null, time=null, errorDetail=null, error=null, aux=null)
  [14:01:07] :     [Step 3/3] 14:01:07.211 [tc-okhttp-stream-276714561] DEBUG com.github.dockerjava.core.command.PullImageResultCallback - ResponseItem(stream=null, status=Pull complete, progressDetail=ResponseItem.ProgressDetail(current=null, total=null, start=null), progress=null, id=2538d1d7e815, from=null, time=null, errorDetail=null, error=null, aux=null)
  ...
  [14:01:07] :     [Step 3/3] 14:01:07.228 [tc-okhttp-stream-276714561] INFO    [registry/private/oracle/database:18c_xe] - Pull complete. 2 layers, pulled in 46s (downloaded 637 MB at 13 MB/s)
  [14:01:07] :     [Step 3/3] 14:01:07.228 [main] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: registry/private/oracle/database:18c_xe
  ...
  [14:01:07]i:     [Step 3/3] Docker event: {"status":"pull","id":"registry/private/oracle/database:18c_xe","Type":"image","Action":"pull","Actor":{"ID":"registry/private/oracle/database:18c_xe","Attributes":{"name":"registry/private/oracle/database"}},"scope":"local","time":1588075267,"timeNano":1588075267227817791}
  ...
  [14:01:08] :     [Step 3/3]  :: Spring Boot ::        (v2.2.6.RELEASE)
  [14:01:08] :     [Step 3/3] 
  [14:01:08] :     [Step 3/3] 2020-04-28 14:01:08.502 ERROR 47 --- [           main] o.s.boot.SpringApplication               : Application run failed
  [14:01:08] :     [Step 3/3] 
  [14:01:08] :     [Step 3/3] java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
  [14:01:08] :     [Step 3/3]     at org.testcontainers.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:174) ~[testcontainers-1.14.1.jar:na]
  [14:01:08] :     [Step 3/3]     at org.testcontainers.containers.ContainerState.getMappedPort(ContainerState.java:129) ~[testcontainers-1.14.1.jar:na]
  [14:01:08] :     [Step 3/3]     at org.testcontainers.containers.OracleContainer.getOraclePort(OracleContainer.java:95) ~[oracle-xe-1.14.1.jar:na]
  [14:01:08] :     [Step 3/3]     at org.testcontainers.containers.OracleContainer.getJdbcUrl(OracleContainer.java:64) ~[oracle-xe-1.14.1.jar:na]
  [14:01:08] :     [Step 3/3]     at ch.package.OracleFlywayDatabaseTest$Initializer.initialize(OracleFlywayDatabaseTest.java:35) ~[test-classes/:na]
  [14:01:08] :     [Step 3/3]     at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:626) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
  ...
  [14:01:08] :     [Step 3/3]     at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
  ...
  [14:01:08] :     [Step 3/3]     at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]
  [14:01:08] :     [Step 3/3] 
  ...
  [14:01:08] :     [Step 3/3] org.testcontainers.containers.ContainerLaunchException: Container startup failed
  [14:01:08] :     [Step 3/3] Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=registry/private/oracle/database:18c_xe, imagePullPolicy=DefaultPullPolicy())
  [14:01:08] :     [Step 3/3] Caused by: java.time.format.DateTimeParseException: Text '2020-03-04T15:17:25.025952651+01:00' could not be parsed at index 29
  [14:01:08] :     [Step 3/3] 
  </code></pre>
  
  <p>I'm not sure about the last exception, it does not seem to bad, the problem seems to be the oracle container we start 'is not visible'. The date in that DateTimeParseException is the created date of our oracle-xe image in our registry.</p>
  
  <p>I tried creating the container also with the <code>withNetwork</code> option on the builder:</p>
  
  <pre><code>@Testcontainers
  public abstract class OracleFlywayDatabaseTest {
  
    @Container
    private static final OracleContainer oracle =
          new OracleContainer("registry/private/oracle/database:18c_xe")
                  // .withNetwork(Network.builder().id("custom_network").build())
                  .withUsername("TESTUSR")
                  .withPassword("TESTPWD");
  </code></pre>
  
  <p>If I investigate this locally using <code>docker network inspect custom_network</code> the database container started by Testcontainers is not in that network.</p>
  
  <p>What is the correct way to put a container in that network? Meaning the same network the builder image initially starts in? Is <code>id</code> really the id that docker assigns to the network when it is created? (I tried that but maybe I was doing something wrong).</p>