Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CoTA
cota-backend
Commits
c47948b0
Commit
c47948b0
authored
Jan 09, 2025
by
mamunozgil
Browse files
Added volumes for the backend host
parent
f8255ef9
Pipeline
#10761
passed with stage
in 18 seconds
Changes
2
Pipelines
1
Expand all
Hide whitespace changes
Inline
Side-by-side
src/main/java/de/hftstuttgart/dtabackend/utils/DockerUtil.java
View file @
c47948b0
...
@@ -5,6 +5,9 @@ import com.github.dockerjava.api.command.CreateContainerResponse;
...
@@ -5,6 +5,9 @@ import com.github.dockerjava.api.command.CreateContainerResponse;
import
com.github.dockerjava.api.exception.DockerException
;
import
com.github.dockerjava.api.exception.DockerException
;
import
com.github.dockerjava.api.model.Bind
;
import
com.github.dockerjava.api.model.Bind
;
import
com.github.dockerjava.api.model.HostConfig
;
import
com.github.dockerjava.api.model.HostConfig
;
import
com.github.dockerjava.api.model.Mount
;
import
com.github.dockerjava.api.model.MountType
;
import
com.github.dockerjava.api.model.Volume
;
import
com.github.dockerjava.core.DefaultDockerClientConfig
;
import
com.github.dockerjava.core.DefaultDockerClientConfig
;
import
com.github.dockerjava.core.DockerClientConfig
;
import
com.github.dockerjava.core.DockerClientConfig
;
import
com.github.dockerjava.core.DockerClientImpl
;
import
com.github.dockerjava.core.DockerClientImpl
;
...
@@ -15,6 +18,8 @@ import org.springframework.core.env.Environment;
...
@@ -15,6 +18,8 @@ import org.springframework.core.env.Environment;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.stream.Collectors
;
@Component
@Component
public
class
DockerUtil
{
public
class
DockerUtil
{
...
@@ -37,7 +42,7 @@ public class DockerUtil {
...
@@ -37,7 +42,7 @@ public class DockerUtil {
dockerClient
=
DockerClientImpl
.
getInstance
(
dockerClientConfig
,
httpClient
);
dockerClient
=
DockerClientImpl
.
getInstance
(
dockerClientConfig
,
httpClient
);
}
}
public
int
runContainer
(
String
image
,
Bind
...
binds
)
throws
InterruptedException
,
IOException
public
int
runContainer
WithBinds
(
String
image
,
Bind
...
binds
)
throws
InterruptedException
,
IOException
{
{
LOG
.
debug
(
String
.
format
(
"pull image: %s"
,
image
));
LOG
.
debug
(
String
.
format
(
"pull image: %s"
,
image
));
try
{
try
{
...
@@ -87,4 +92,62 @@ public class DockerUtil {
...
@@ -87,4 +92,62 @@ public class DockerUtil {
return
ret
;
return
ret
;
}
}
public
int
runContainerWithVolumes
(
String
image
,
Volume
...
volumes
)
throws
InterruptedException
,
IOException
{
LOG
.
debug
(
String
.
format
(
"Pulling image: %s"
,
image
));
try
{
dockerClient
.
pullImageCmd
(
image
)
.
start
()
.
awaitCompletion
()
.
close
();
}
catch
(
DockerException
e
)
{
LOG
.
error
(
String
.
format
(
"Pulling Docker image %s failed with %s, trying with local image"
,
image
,
e
.
getMessage
()));
}
LOG
.
debug
(
"Creating container"
);
CreateContainerResponse
containerResponse
;
try
{
// Prepare the host configuration with volumes
HostConfig
hostConfig
=
HostConfig
.
newHostConfig
()
.
withMounts
(
Arrays
.
stream
(
volumes
)
.
map
(
volume
->
new
Mount
().
withTarget
(
volume
.
getPath
()).
withType
(
MountType
.
VOLUME
))
.
collect
(
Collectors
.
toList
())
);
// Create the container with the configured volumes
containerResponse
=
dockerClient
.
createContainerCmd
(
"testcontainer"
)
.
withImage
(
image
)
.
withHostConfig
(
hostConfig
)
.
exec
();
}
catch
(
DockerException
e
)
{
LOG
.
error
(
String
.
format
(
"Creating Docker Testrunner container failed with %s"
,
e
.
getMessage
()));
throw
e
;
}
LOG
.
debug
(
String
.
format
(
"Container created: %s"
,
containerResponse
.
getId
()));
LOG
.
debug
(
String
.
format
(
"Starting container %s"
,
containerResponse
.
getId
()));
dockerClient
.
startContainerCmd
(
containerResponse
.
getId
()).
exec
();
LOG
.
debug
(
String
.
format
(
"Waiting for completion of container %s"
,
containerResponse
.
getId
()));
int
ret
=
dockerClient
.
waitContainerCmd
(
containerResponse
.
getId
())
.
start
()
.
awaitCompletion
()
.
awaitStatusCode
();
LOG
.
debug
(
String
.
format
(
"Container completed with status %d"
,
ret
));
LOG
.
debug
(
String
.
format
(
"Deleting container %s"
,
containerResponse
.
getId
()));
dockerClient
.
removeContainerCmd
(
containerResponse
.
getId
())
.
withRemoveVolumes
(
true
)
.
exec
();
return
ret
;
}
}
}
src/main/java/de/hftstuttgart/dtabackend/utils/ExecuteTestUtil.java
View file @
c47948b0
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment