|
| 1 | +package org.testcontainers.ollama; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.DockerClient; |
| 4 | +import com.github.dockerjava.api.model.DeviceRequest; |
| 5 | +import com.github.dockerjava.api.model.Image; |
| 6 | +import com.github.dockerjava.api.model.Info; |
| 7 | +import com.github.dockerjava.api.model.RuntimeInfo; |
| 8 | +import org.testcontainers.DockerClientFactory; |
| 9 | +import org.testcontainers.containers.GenericContainer; |
| 10 | +import org.testcontainers.utility.DockerImageName; |
| 11 | + |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +/** |
| 17 | + * Testcontainers implementation for Ollama. |
| 18 | + * <p> |
| 19 | + * Supported image: {@code ollama/ollama} |
| 20 | + * <p> |
| 21 | + * Exposed ports: 11434 |
| 22 | + */ |
| 23 | +public class OllamaContainer extends GenericContainer<OllamaContainer> { |
| 24 | + |
| 25 | + private static final DockerImageName DOCKER_IMAGE_NAME = DockerImageName.parse("ollama/ollama"); |
| 26 | + |
| 27 | + public OllamaContainer(String image) { |
| 28 | + this(DockerImageName.parse(image)); |
| 29 | + } |
| 30 | + |
| 31 | + public OllamaContainer(DockerImageName dockerImageName) { |
| 32 | + super(dockerImageName); |
| 33 | + dockerImageName.assertCompatibleWith(DOCKER_IMAGE_NAME); |
| 34 | + |
| 35 | + Info info = this.dockerClient.infoCmd().exec(); |
| 36 | + Map<String, RuntimeInfo> runtimes = info.getRuntimes(); |
| 37 | + if (runtimes != null) { |
| 38 | + if (runtimes.containsKey("nvidia")) { |
| 39 | + withCreateContainerCmdModifier(cmd -> { |
| 40 | + cmd |
| 41 | + .getHostConfig() |
| 42 | + .withDeviceRequests( |
| 43 | + Collections.singletonList( |
| 44 | + new DeviceRequest() |
| 45 | + .withCapabilities(Collections.singletonList(Collections.singletonList("gpu"))) |
| 46 | + .withCount(-1) |
| 47 | + ) |
| 48 | + ); |
| 49 | + }); |
| 50 | + } |
| 51 | + } |
| 52 | + withExposedPorts(11434); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Commits the current file system changes in the container into a new image. |
| 57 | + * Should be used for creating an image that contains a loaded model. |
| 58 | + * @param imageName the name of the new image |
| 59 | + */ |
| 60 | + public void commitToImage(String imageName) { |
| 61 | + DockerImageName dockerImageName = DockerImageName.parse(getDockerImageName()); |
| 62 | + if (!dockerImageName.equals(DockerImageName.parse(imageName))) { |
| 63 | + DockerClient dockerClient = DockerClientFactory.instance().client(); |
| 64 | + List<Image> images = dockerClient.listImagesCmd().withReferenceFilter(imageName).exec(); |
| 65 | + if (images.isEmpty()) { |
| 66 | + DockerImageName imageModel = DockerImageName.parse(imageName); |
| 67 | + dockerClient |
| 68 | + .commitCmd(getContainerId()) |
| 69 | + .withRepository(imageModel.getUnversionedPart()) |
| 70 | + .withLabels(Collections.singletonMap("org.testcontainers.sessionId", "")) |
| 71 | + .withTag(imageModel.getVersionPart()) |
| 72 | + .exec(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public String getEndpoint() { |
| 78 | + return "http://" + getHost() + ":" + getMappedPort(11434); |
| 79 | + } |
| 80 | +} |
0 commit comments