testcontainers / testcontainers/testcontainers-java

DockerComposeContainer not starting Exposed Service

Open
#3,468 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The latest release of testcontainers (1.15.0) seems to have introduced a bug with DockerComposeContainer where the command used to start containers is missing specified exposed services.

What happens is it seems that the docker-compose up command is missing the db service. For some reason it isn't written out as part of the command.

Command: up -d --scale scaling=2 scaled_service
Specific log: 10:34:52.774 [main] INFO 🐳 [docker-compose] - Local Docker Compose is running command: up -d --scale scaling=2 scaled_service

Example

Compose File
version: '3'

services:
  db:
    image: postgres:11
    environment:
      - POSTGRES_DB=test
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=test
    ports:
      - "5432:5432"
  scaled_service:
    image: alpine:latest
Java
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;

import java.io.File;
import java.time.Duration;

import static java.time.temporal.ChronoUnit.SECONDS;

public class Bug {

    public static void main(final String[] args) {

        DockerComposeContainer container = new DockerComposeContainer(new File("test-compose.yml"))
                .withEnv(System.getenv())
                // Add api variable to environment to populate docker-compose port variable
                .withLocalCompose(true)
                .withScaledService("scaled_service", 2)
                .withExposedService("db", 5432, new HostPortWaitStrategy()
                        .withStartupTimeout(Duration.of(60, SECONDS)));

        container.start();

        // Will not reach this point will fail
        container.stop();
    }
}
Dependencies
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <version>1.15.0</version>
    <scope>test</scope>
</dependency>
Logs
10:45:10.981 [main] INFO  o.t.d.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
10:45:11.788 [main] INFO  o.t.d.DockerClientProviderStrategy - Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
10:45:11.790 [main] INFO  o.testcontainers.DockerClientFactory - Docker host IP address is localhost
10:45:11.832 [main] INFO  o.testcontainers.DockerClientFactory - Connected to docker: 
  Server Version: 19.03.13
  API Version: 1.40
  Operating System: Docker Desktop
  Total Memory: 7961 MB
10:45:11.835 [main] INFO  o.t.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (ConfigurationFileImageNameSubstitutor)
10:45:12.555 [main] INFO  o.testcontainers.DockerClientFactory - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
10:45:12.556 [main] INFO  o.testcontainers.DockerClientFactory - Checking the system...
10:45:12.556 [main] INFO  o.testcontainers.DockerClientFactory - ✔︎ Docker server version should be at least 1.6.0
10:45:12.649 [main] INFO  o.testcontainers.DockerClientFactory - ✔︎ Docker environment should have more than 2GB free disk space
10:45:12.681 [main] INFO  🐳 [docker-compose] - Local Docker Compose is running command: up -d --scale scaled_service=2 scaled_service
10:45:13.344 [Thread-3] INFO  🐳 [docker-compose] - Creating network "pqjx8ucx9yor_default" with the default driver
10:45:13.429 [Thread-3] INFO  🐳 [docker-compose] - Creating pqjx8ucx9yor_scaled_service_1 ... 
10:45:13.429 [Thread-3] INFO  🐳 [docker-compose] - Creating pqjx8ucx9yor_scaled_service_2 ... 
10:45:13.843 [Thread-3] INFO  🐳 [docker-compose] - 
10:45:13.844 [Thread-3] INFO  🐳 [docker-compose] - Creating pqjx8ucx9yor_scaled_service_1 ... done
10:45:13.853 [Thread-3] INFO  🐳 [docker-compose] - 
10:45:13.853 [Thread-3] INFO  🐳 [docker-compose] - Creating pqjx8ucx9yor_scaled_service_2 ... done
10:45:13.905 [main] INFO  🐳 [docker-compose] - 
10:45:13.905 [main] INFO  🐳 [docker-compose] - Docker Compose has finished running
10:45:13.927 [main] INFO  🐳 [alpine/socat:1.7.3.4-r0] - Creating container for image: alpine/socat:1.7.3.4-r0
10:45:13.947 [main] ERROR 🐳 [alpine/socat:1.7.3.4-r0] - Could not start container
org.testcontainers.containers.ContainerLaunchException: Aborting attempt to link to container pqjx8ucx9yor_db_1 as it is not running
	at org.testcontainers.containers.GenericContainer.applyConfiguration(GenericContainer.java:776)
	at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:356)
	at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:322)
	at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
	at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:308)
	at org.testcontainers.containers.DockerComposeContainer.startAmbassadorContainers(DockerComposeContainer.java:330)
	at org.testcontainers.containers.DockerComposeContainer.start(DockerComposeContainer.java:177)
	at ReproduceBug.main(ReproduceBug.java:22)
Exception in thread "main" org.testcontainers.containers.ContainerLaunchException: Container startup failed
	at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:327)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:308)
	at org.testcontainers.containers.DockerComposeContainer.startAmbassadorContainers(DockerComposeContainer.java:330)
	at org.testcontainers.containers.DockerComposeContainer.start(DockerComposeContainer.java:177)
	at ReproduceBug.main(ReproduceBug.java:22)
Caused by: org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
	at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:88)
	at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320)
	... 4 more
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
	at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:494)
	at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:322)
	at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
	... 5 more
Caused by: org.testcontainers.containers.ContainerLaunchException: Aborting attempt to link to container pqjx8ucx9yor_db_1 as it is not running
	at org.testcontainers.containers.GenericContainer.applyConfiguration(GenericContainer.java:776)
	at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:356)
	... 7 more

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 DockerComposeContainer.start and the command assembly shown by the stack trace, then compare it with the supplied test-compose.yml and ReproduceBug.java example. Run the reproduction and verify that using withExposedService includes db in the generated docker-compose command and allows startup to complete.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.