Executing a command inside running container

In this post under TestContainer, I will show with example how to execute a command inside a running container and get the output.

Below is the complete main code for your reference.

Main Class

1  package package6;
2  
3  import java.io.IOException;
4  
5  import org.testcontainers.containers.Container;
6  import org.testcontainers.containers.GenericContainer;
7  import org.testcontainers.utility.DockerImageName;
8  
9  public class Example6 {
10     public static void main(String[] args) throws InterruptedException, IOException {
11         DockerImageName dockerImageName = DockerImageName.parse("testcontainers/helloworld:latest");
12         try (GenericContainer<?> container = new GenericContainer<>(dockerImageName)) {
13             container.start();
14             Container.ExecResult execResult = container.execInContainer("ls", "-al");
15             String output = execResult.getStdout();
16             System.out.println(output);
17         }
18     }
19 }

In the above code, at line 11, I create an instance of “DockerImageName”.

At line 12, I create an instance of “GenericContainer” class.

At line 13, I start the container.

At line 14, I call the “execInContainer” method available on “GenericContainer” class. The syntax of the method is as shown below

Container.ExecResult execInContainer(java.lang.String... command)
                                      throws java.lang.UnsupportedOperationException,
                                             java.io.IOException,
                                             java.lang.InterruptedException

The method takes String array or variable length argument of type String.

In the our code, I pass the linux “ls” command with options “al”, which list the files in the home directory.

The return value of “execInContainer” method is an instance of “ExecResult” class.

The “ExecResult” instance contains result of command which is printed to stdout, exit code etc.

At line 15, I get the result printed by “ls -al” command in the stdout.

At line 16, I print the output.

In this way we can execute a command inside a running container.

Below is the output

Output

total 6924
drwxr-xr-x    1 root     root          4096 Nov  9 03:48 .
drwxr-xr-x    1 root     root          4096 Nov  9 03:48 ..
-rwxr-xr-x    1 root     root             0 Nov  9 03:48 .dockerenv
drwxr-xr-x    2 root     root          4096 May 29  2020 bin
drwxr-xr-x    5 root     root           340 Nov  9 03:48 dev
drwxr-xr-x    1 root     root          4096 Nov  9 03:48 etc
-rwxr-xr-x    1 root     root       7018976 Oct 13  2020 helloworld
drwxr-xr-x    2 root     root          4096 May 29  2020 home
drwxr-xr-x    7 root     root          4096 May 29  2020 lib
drwxr-xr-x    5 root     root          4096 May 29  2020 media
drwxr-xr-x    2 root     root          4096 May 29  2020 mnt
drwxr-xr-x    2 root     root          4096 May 29  2020 opt
dr-xr-xr-x  317 root     root             0 Nov  9 03:48 proc
drwx------    2 root     root          4096 May 29  2020 root
drwxr-xr-x    2 root     root          4096 May 29  2020 run
drwxr-xr-x    2 root     root          4096 May 29  2020 sbin
drwxr-xr-x    2 root     root          4096 May 29  2020 srv
drwxr-xr-x    2 root     root          4096 Oct 13  2020 static
dr-xr-xr-x   11 root     root             0 Nov  9 03:48 sys
drwxrwxrwt    2 root     root          4096 May 29  2020 tmp
drwxr-xr-x    7 root     root          4096 May 29  2020 usr
drwxr-xr-x   12 root     root          4096 May 29  2020 var

Leave a comment