javadockerkotlintestcontainerstestcontainers-junit5

Testcontainers: build docker image inside testcontainer


I'm creating some tests for my CI/CD service with testcontainers.

One of the tests contains docker image building inside the testcontainer. It fails as it cannot access docker daemon.

The question is how to share local docker daemon to testcontainer or how to run docker doemon inside of testcontainer in the easiest way?


Solution

  • So, finally I have solved it by running docker-dind image as an additional container

    @Slf4j
    @NoArgsConstructor
    public class DockerContainer {
        public static final String HOST = "test.docker.env";
        private static GenericContainer<?> container;
    
        public static void init() {
            container = new GenericContainer<>(DockerImageName.parse("docker:20.10.14-dind"))
                    .withNetworkAliases(HOST)
                    .withNetwork(SHARED)
                    .withExposedPorts(2375)
                    .withEnv("DOCKER_TLS_CERTDIR", "")
                    .withPrivilegedMode(true)
                    .withLogConsumer(new Slf4jLogConsumer(log));
        }
    
        public static GenericContainer<?> getInstance() {
            if (container == null) {
                init();
            }
            return container;
        }
    }
    

    And added docker host to my CI container:

    this.container.dependsOn(DockerContainer.getInstance());
    this.container.addEnv("DOCKER_HOST", String.format("tcp://%s:2375", DockerContainer.HOST));
    

    Now it works well