feat: docker basis
This commit is contained in:
parent
c96103e17b
commit
e18c86e483
5
pom.xml
5
pom.xml
@ -79,6 +79,11 @@
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkiverse.docker</groupId>
|
||||
<artifactId>quarkus-docker-client</artifactId>
|
||||
<version>0.0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
@ -0,0 +1,40 @@
|
||||
package fr.la_banquise.backend.rest;
|
||||
|
||||
import fr.la_banquise.backend.services.DockerService;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.*;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Path("/containers")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class ContainerResource {
|
||||
|
||||
@Inject
|
||||
DockerService dockerService;
|
||||
|
||||
@POST
|
||||
@Path("/nginx")
|
||||
public Response launchNginx() {
|
||||
try {
|
||||
String id = dockerService.createAndStartNginx();
|
||||
return Response.ok(Map.of("containerId", id, "status", "running")).build();
|
||||
} catch (Exception e) {
|
||||
return Response.status(500).entity(Map.of("error", e.getMessage())).build();
|
||||
}
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/{id}")
|
||||
public Response remove(@PathParam("id") String id) {
|
||||
try {
|
||||
dockerService.stopAndRemove(id);
|
||||
return Response.ok(Map.of("containerId", id, "status", "removed")).build();
|
||||
} catch (Exception e) {
|
||||
return Response.status(500).entity(Map.of("error", e.getMessage())).build();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package fr.la_banquise.backend.services;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.CreateContainerResponse;
|
||||
import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.PortBinding;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
@ApplicationScoped
|
||||
public class DockerService {
|
||||
|
||||
@Inject
|
||||
DockerClient dockerClient;
|
||||
|
||||
public String createAndStartNginx() {
|
||||
ExposedPort tcp80 = ExposedPort.tcp(80);
|
||||
|
||||
Ports portBindings = new Ports();
|
||||
portBindings.bind(tcp80, Ports.Binding.bindPort(8081));
|
||||
|
||||
HostConfig hostConfig = HostConfig.newHostConfig()
|
||||
.withPortBindings(portBindings);
|
||||
|
||||
CreateContainerResponse container = dockerClient.createContainerCmd("nginx:latest")
|
||||
.withName("my-nginx")
|
||||
.withExposedPorts(tcp80)
|
||||
.withHostConfig(hostConfig)
|
||||
.exec();
|
||||
|
||||
dockerClient.startContainerCmd(container.getId()).exec();
|
||||
return container.getId();
|
||||
}
|
||||
|
||||
public void stopAndRemove(String containerId) {
|
||||
dockerClient.stopContainerCmd(containerId).exec();
|
||||
dockerClient.removeContainerCmd(containerId).exec();
|
||||
}
|
||||
}
|
@ -36,3 +36,5 @@ quarkus.hibernate-orm.database.generation=update
|
||||
|
||||
quarkus.quinoa.dev-server.port=5173
|
||||
quarkus.quinoa.enable-spa-routing=true
|
||||
|
||||
quarkus.docker.docker-host=unix:///var/run/docker.sock
|
||||
|
Loading…
x
Reference in New Issue
Block a user