Skip to content

Commit 4ff103a

Browse files
committed
Add docs
1 parent b1ce8b9 commit 4ff103a

File tree

4 files changed

+56
-60
lines changed

4 files changed

+56
-60
lines changed
Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
package org.testcontainers.containers;
22

3-
import com.github.dockerjava.api.command.InspectContainerResponse;
43
import org.testcontainers.containers.wait.strategy.Wait;
54
import org.testcontainers.utility.DockerImageName;
65

7-
import java.io.BufferedReader;
8-
import java.io.IOException;
9-
import java.io.InputStreamReader;
10-
import java.io.OutputStream;
11-
import java.net.HttpURLConnection;
12-
import java.net.URL;
13-
import java.nio.charset.StandardCharsets;
14-
156
/**
167
* Testcontainers proxy container for the Docker Model Runner service
178
* provided by Docker Desktop.
@@ -22,9 +13,7 @@
2213
*/
2314
public class DockerModelRunnerContainer extends SocatContainer {
2415

25-
public static final String MODEL_RUNNER_ENDPOINT = "model-runner.docker.internal";
26-
27-
private String model;
16+
private static final String MODEL_RUNNER_ENDPOINT = "model-runner.docker.internal";
2817

2918
public DockerModelRunnerContainer(String image) {
3019
this(DockerImageName.parse(image));
@@ -36,56 +25,11 @@ public DockerModelRunnerContainer(DockerImageName image) {
3625
waitingFor(Wait.forHttp("/").forResponsePredicate(res -> res.contains("The service is running")));
3726
}
3827

39-
@Override
40-
protected void containerIsStarted(InspectContainerResponse containerInfo) {
41-
pullModel();
42-
}
43-
44-
private void pullModel() {
45-
logger().info("Pulling model: {}. Please be patient, no progress bar yet!", this.model);
46-
try {
47-
String json = String.format("{\"from\":\"%s\"}", this.model);
48-
String endpoint = "http://" + getHost() + ":" + getMappedPort(80) + "/models/create";
49-
50-
URL url = new URL(endpoint);
51-
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
52-
connection.setRequestMethod("POST");
53-
connection.setRequestProperty("Content-Type", "application/json");
54-
connection.setDoOutput(true);
55-
56-
try (OutputStream os = connection.getOutputStream()) {
57-
byte[] input = json.getBytes(StandardCharsets.UTF_8);
58-
os.write(input, 0, input.length);
59-
}
60-
61-
try (
62-
BufferedReader br = new BufferedReader(
63-
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)
64-
)
65-
) {
66-
StringBuilder response = new StringBuilder();
67-
String responseLine;
68-
while ((responseLine = br.readLine()) != null) {
69-
response.append(responseLine.trim());
70-
}
71-
logger().info(response.toString());
72-
}
73-
} catch (IOException e) {
74-
throw new RuntimeException(e);
75-
}
76-
logger().info("Finished pulling model: {}", model);
77-
}
78-
7928
public String getBaseEndpoint() {
8029
return "http://" + getHost() + ":" + getMappedPort(80);
8130
}
8231

8332
public String getOpenAIEndpoint() {
8433
return getBaseEndpoint() + "/engines";
8534
}
86-
87-
public DockerModelRunnerContainer withModel(String modelName) {
88-
this.model = modelName;
89-
return this;
90-
}
9135
}

core/src/test/java/org/testcontainers/containers/DockerModelRunnerContainerTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@ public void pullsModelAndExposesInference() {
1616
String modelName = "ai/smollm2:360M-Q4_K_M";
1717

1818
try (
19+
// container {
1920
DockerModelRunnerContainer dmr = new DockerModelRunnerContainer("alpine/socat:1.7.4.3-r0")
20-
.withModel(modelName)
21+
// }
2122
) {
2223
dmr.start();
2324

24-
Response response = RestAssured.get(dmr.getBaseEndpoint() + "/models").thenReturn();
25-
assertThat(response.body().jsonPath().getList("tags.flatten()")).contains(modelName);
25+
// pullModel {
26+
RestAssured
27+
.given()
28+
.body(String.format("{\"from\":\"%s\"}", modelName))
29+
.post(dmr.getBaseEndpoint() + "/models/create")
30+
.then()
31+
.statusCode(200);
32+
// }
33+
34+
Response modelResponse = RestAssured.get(dmr.getBaseEndpoint() + "/models").thenReturn();
35+
assertThat(modelResponse.body().jsonPath().getList("tags.flatten()")).contains(modelName);
2636

2737
Response openAiResponse = RestAssured.get(dmr.getOpenAIEndpoint() + "/v1/models").prettyPeek().thenReturn();
2838
assertThat(openAiResponse.body().jsonPath().getList("data.id")).contains(modelName);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Docker Model Runner
2+
3+
This module helps connect to [Docker Model Runner](https://docs.docker.com/desktop/features/model-runner/)
4+
provided by Docker Desktop 4.40.0.
5+
6+
## DockerModelRunner's usage examples
7+
8+
You can start a Docker Model Runner proxy container instance from any Java application by using:
9+
10+
<!--codeinclude-->
11+
[Create a DockerModelRunnerContainer](../../core/src/test/java/org/testcontainers/containers/DockerModelRunnerContainerTest.java) inside_block:container
12+
<!--/codeinclude-->
13+
14+
### Pulling the model
15+
16+
Pulling the model is as simple as:
17+
18+
<!--codeinclude-->
19+
[Pull model](../../core/src/test/java/org/testcontainers/containers/DockerModelRunnerContainerTest.java) inside_block:pullModel
20+
<!--/codeinclude-->
21+
22+
## Adding this module to your project dependencies
23+
24+
*Docker Model Runner support is part of the core Testcontainers library.*
25+
26+
Add the following dependency to your `pom.xml`/`build.gradle` file:
27+
28+
=== "Gradle"
29+
```groovy
30+
testImplementation "org.testcontainers:testcontainers:{{latest_version}}"
31+
```
32+
=== "Maven"
33+
```xml
34+
<dependency>
35+
<groupId>org.testcontainers</groupId>
36+
<artifactId>testcontainers</artifactId>
37+
<version>{{latest_version}}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
```
41+

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ nav:
8282
- modules/chromadb.md
8383
- modules/consul.md
8484
- modules/docker_compose.md
85+
- modules/docker_model_runner.md
8586
- modules/elasticsearch.md
8687
- modules/gcloud.md
8788
- modules/grafana.md

0 commit comments

Comments
 (0)