Accessing Container logs

In this post under TestContainers, I will show with example how to access container logs.

Below is the complete main code for your reference.

Main Class


1  package package2;
2  
3  import org.testcontainers.containers.GenericContainer;
4  import org.testcontainers.utility.DockerImageName;
5  
6  public class Example2 {
7      public static void main(String[] args) {
8          DockerImageName dockerImageName = DockerImageName.parse("hello-world:latest");
9          try (GenericContainer<?> container = new GenericContainer<>(dockerImageName)) {
10             container.start();
11             String data = container.getLogs();
12             System.out.println(data);
13         }
14     }
15 }

At line 9, In the try with resources block, I create an GenericContainer container instance.

At line 10, I start the container.

At line 11, I get the logs by calling “getLogs” method on the container instance.

In this way we can access the container logs.

Leave a Reply