-
Notifications
You must be signed in to change notification settings - Fork 0
/
startdb.java
executable file
·45 lines (36 loc) · 1.56 KB
/
startdb.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.postgresql:postgresql:42.2.14
//DEPS org.testcontainers:postgresql:1.15.2
//DEPS org.postgresql:postgresql:42.2.14
//DEPS org.slf4j:slf4j-simple:1.7.25
import java.util.function.Consumer;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.model.ExposedPort;
import com.github.dockerjava.api.model.PortBinding;
import com.github.dockerjava.api.model.Ports;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;
public class startdb {
public static void main(String... args) {
GenericContainer gc = startdb();
try {
gc.start();
} finally {
System.out.println("Press enter to stop database");
System.console().readLine();
gc.stop();
}
}
private static GenericContainer startdb() {
int hostPort = 5432;
int containerExposedPort = 5432;
Consumer<CreateContainerCmd> cmd = e -> e.withPortBindings(
new PortBinding((Ports.Binding.bindPort(hostPort)), new ExposedPort(containerExposedPort)));
DockerImageName myImage = DockerImageName.parse("frantiseks/postgres-sakila")
.asCompatibleSubstituteFor("postgres");
var dbcontainer = new PostgreSQLContainer(myImage).withDatabaseName("sakila").withUsername("postgres")
.withPassword("sakila").withCreateContainerCmdModifier(cmd);
return dbcontainer;
}
}