Some docker images will have “CMD” instruction in their DockerFile out of the box. In this post I will explain how to override that “CMD” instruction if present in DockerFile or provide our own instruction in the absence of “CMD” instruction in the DockerFile. For our example I will use the nginx image from DockerHub.…… Continue reading Overriding or providing new command at container startup
Category: TestContainers
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…… Continue reading Executing a command inside running container
Setting startup timeout
In this post under TestContainers, I will show with example how to setup startup timeout. Whenever we start a container whether through programmatically or through Docker Desktop or any other container management software, it takes time to start. TestContainers to figure out whether a container has started or not, it waits for 60 sec so…… Continue reading Setting startup timeout
Setting Environment Variable
In this post under TestContainer, I will explain with example how to set environment variable before starting a container. For our example, we will use the latest “postgres” image from Docker Hub. According to “postgres” help page on Docker Hub we only need to set the environment variable “POSTGRES_PASSWORD” to start the postgres container and…… Continue reading Setting Environment Variable
Communication between host and Container
In this post under TestContainer, I will show with example how to make the host communicate with the docker container. Below is the complete code for your reference. Main Class 1 package package3; 2 3 import org.testcontainers.containers.GenericContainer; 4 import org.testcontainers.utility.DockerImageName; 5 6 public class Example3 { 7 public static void main(String[] args) { 8 DockerImageName…… Continue reading Communication between host and Container
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…… Continue reading Accessing Container logs
Starting and Stopping Docker Containers
In this post under TestContainers, I will show with example how to start and stop a Docker container programmatically. Below is the complete main class for your reference. Main Class 1 package package1; 2 3 import org.testcontainers.containers.GenericContainer; 4 import org.testcontainers.utility.DockerImageName; 5 6 public class Example1 { 7 public static void main(String[] args) { 8 DockerImageName…… Continue reading Starting and Stopping Docker Containers