4

I wish to create a network via docker-compose (via DockerComposeContainer) and have another container (created via ImageFromDockerfile) join that same network. Is this possible?

Asked another way, can ImageFromDockerfile join an existing network? (For me the order is crucial, because when i start my image it needs to connect to all the services running through compose)

The moving parts I have tried include:

  1. The docker compose file
version: '3.6'

services:
  vault:
    image: docker.x.com/x/vault:123-ytr
    ports:
      - 8200:8200
    networks:
      - gateway
    environment:
      - APP_NAME=requestlogidentityconsumer

networks:
  gateway:
    name: damo
  1. Executing above compose file via DockerComposeContainer (incl. create damo network)

  2. Attempt to build and run rlic-container and have it join damo n/w

    Network network =
        Network.builder().createNetworkCmdModifier(cmd -> cmd.withName("damo")).build();

    new GenericContainer(
            new ImageFromDockerfile("rlic-container", true)
                .withFileFromFile("Dockerfile", DOCKER_FILE_PATH.toFile()))
        .withNetwork(network)
        .start();

When I run step 3 i get:

Caused by: com.github.dockerjava.api.exception.ConflictException: {"message":"network with name damo already exists"}

Which makes sense (in so far as network does exist from step 2), and ties back to my question of; can i write step 3 such that it joins an existing network?

Thanks

Damo
  • 1,449
  • 3
  • 16
  • 29
  • as a workaround, I can create a throwaway testcontainer and have it create the (damo) network as step 1, then mod docker-compose to join this external (damo) n/w. This of course then allows any further testcontainer join that network – Damo Oct 04 '20 at 10:37
  • I was able to work around this problem by exposing the port from the docker-compose and then using `Testcontainers.exposeHostPorts(8200);` and then accessing this from the testcontainer with `http://host.testcontainers.internal:8200` There are details about this approach here: https://www.testcontainers.org/features/networking/ – tttppp Apr 29 '21 at 15:24
  • Why is your new image not referenced in the Compose file that you start, then build the image first next start the Compose file using that image? – Tim van der Leeuw Jul 05 '21 at 15:37

0 Answers0