-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeBackendContainer.java
59 lines (50 loc) · 2.08 KB
/
NodeBackendContainer.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.builder.ImageFromDockerfile;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
public class NodeBackendContainer <SELF extends NodeBackendContainer<SELF>> extends GenericContainer<SELF> {
public static final Integer DEFAULT_PORT = 8080;
private String ipv4;
private String ipv6 = "";
private int portHttp;
public NodeBackendContainer(String ipv4, Path path, int portHttp) {
super(
new ImageFromDockerfile()
.withFileFromClasspath("app.js", path.toString())
.withFileFromClasspath("package.json", "node-backends/package.json")
.withDockerfileFromBuilder(builder ->
builder
.from("node:latest")
.run("mkdir app")
.copy("app.js", "/app/app.js")
.copy("package.json", "/app/package.json")
.expose(portHttp)
.workDir("/app")
.run("npm install")
.cmd("node", "app.js")
.build()
)
);
setWaitStrategy(Wait.forListeningPort());
this.ipv4 = ipv4;
this.portHttp = portHttp;
}
@Override
protected void configure() {
addExposedPort(portHttp);
withNetworkMode("my-net");
withEnv("PORT", String.valueOf(portHttp));
withCreateContainerCmdModifier(cmd -> cmd.withIpv4Address(this.ipv4));
if (!this.ipv6.isEmpty())
withCreateContainerCmdModifier(cmd -> cmd.withIpv6Address(this.ipv6));
}
public URL getBaseUrl() throws MalformedURLException {
return new URL("http://" + getContainerIpAddress() + ":" + getMappedPort(portHttp));
}
public void setIpv6(String ipv6) {
this.ipv6 = ipv6;
}
}