Skip to content
This repository has been archived by the owner on Mar 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #227 from spotify/explicit-dockerfile
Browse files Browse the repository at this point in the history
Support building with path to explicit Dockerfile
  • Loading branch information
davidxia committed Aug 13, 2015
2 parents 9df2e40 + 415a77b commit b3b7371
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
22 changes: 16 additions & 6 deletions src/main/java/com/spotify/docker/client/DefaultDockerClient.java
Expand Up @@ -786,6 +786,13 @@ public String build(final Path directory, final ProgressHandler handler,
public String build(final Path directory, final String name, final ProgressHandler handler,
final BuildParameter... params)
throws DockerException, InterruptedException, IOException {
return build(directory, name, null, handler, params);
}

@Override
public String build(final Path directory, final String name, final String dockerfile,
final ProgressHandler handler, final BuildParameter... params)
throws DockerException, InterruptedException, IOException {
checkNotNull(handler, "handler");

WebTarget resource = resource().path("build");
Expand All @@ -794,7 +801,10 @@ public String build(final Path directory, final String name, final ProgressHandl
resource = resource.queryParam(param.buildParamName, String.valueOf(param.buildParamValue));
}
if (name != null) {
resource = resource.queryParam("t", name);
resource = resource.queryParam("t", name);
}
if (dockerfile != null) {
resource = resource.queryParam("dockerfile", dockerfile);
}

log.debug("Auth Config {}", authConfig);
Expand All @@ -813,10 +823,10 @@ public String build(final Path directory, final String name, final ProgressHandl

try (final CompressedDirectory compressedDirectory = CompressedDirectory.create(directory);
final InputStream fileStream = Files.newInputStream(compressedDirectory.file());
final ProgressStream build =
final ProgressStream build =
request(POST, ProgressStream.class, resource,
resource.request(APPLICATION_JSON_TYPE)
.header("X-Registry-Config",
.header("X-Registry-Config",
authRegistryHeader(authRegistryConfig)),
Entity.entity(fileStream, "application/tar"))) {

Expand Down Expand Up @@ -1020,7 +1030,7 @@ public ExecState execInspect(final String execId) throws DockerException, Interr
}

@Override
public ContainerStats stats(final String containerId)
public ContainerStats stats(final String containerId)
throws DockerException, InterruptedException {
final WebTarget resource = resource().path("containers").path(containerId).path("stats")
.queryParam("stream", "0");
Expand All @@ -1036,7 +1046,7 @@ public ContainerStats stats(final String containerId)
}
}
}

private WebTarget resource() {
final WebTarget target = client.target(uri);
if (!isNullOrEmpty(apiVersion)) {
Expand Down Expand Up @@ -1164,7 +1174,7 @@ private String authRegistryHeader(final AuthRegistryConfig authRegistryConfig)
return "null";
}
try {
String authRegistryJson =
String authRegistryJson =
ObjectMapperProvider.objectMapper().writeValueAsString(authRegistryConfig);
log.debug("Registry Config Json {}", authRegistryJson);
String authRegistryEncoded = Base64.encodeAsString(authRegistryJson);
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/com/spotify/docker/client/DockerClient.java
Expand Up @@ -178,7 +178,7 @@ List<RemovedImage> removeImage(String image, boolean force, boolean noPrune)
* @throws InterruptedException If the thread is interrupted
*/
List<ImageSearchResult> searchImages(String term) throws DockerException, InterruptedException;

/**
* Pull a docker container image.
*
Expand Down Expand Up @@ -318,6 +318,23 @@ String build(final Path directory, final String name, final ProgressHandler hand
final BuildParameter... params)
throws DockerException, InterruptedException, IOException;

/**
* Build a docker image.
*
* @param directory The directory containing the dockerfile.
* @param name The repository name and optional tag to apply to the built image.
* @param dockerfile The path within the build context to the Dockerfile
* @param handler The handler to use for processing each progress message received from Docker.
* @param params Additional flags to use during build.
* @return The id of the built image if successful, otherwise null.
* @throws DockerException if a server error occurred (500)
* @throws InterruptedException If the thread is interrupted
* @throws IOException If some IO shit happened.
*/
String build(final Path directory, final String name, final String dockerfile,
final ProgressHandler handler, final BuildParameter... params)
throws DockerException, InterruptedException, IOException;

/**
* Flags which can be passed to the <code>build</code> method.
*/
Expand Down Expand Up @@ -589,14 +606,14 @@ public String getName() {

/**
* Retrieves one-time stats (stream=0) for the container with the specified id.
*
*
* @param containerId The id of the container to retrieve stats for.
* @return The container stats
* @throws DockerException if a server error occurred (500)
* @throws InterruptedException If the thread is interrupted
*/
ContainerStats stats(String containerId) throws DockerException, InterruptedException;

/**
* Closes any and all underlying connections to docker, and release resources.
*/
Expand Down Expand Up @@ -641,7 +658,7 @@ LogStream attachContainer(String containerId, AttachParameter... params)
* @return the docker host name or IP
*/
String getHost();

/**
* Parameters for {@link #listContainers(ListContainersParam...)}
*/
Expand Down
Expand Up @@ -51,7 +51,8 @@ public void testFile() throws Exception {
final String name = entry.getName();
names.add(name);
}
assertThat(names, containsInAnyOrder("Dockerfile", "bin/date.sh"));
assertThat(names,
containsInAnyOrder("Dockerfile", "bin/date.sh", "innerDir/innerDockerfile"));
}
}

Expand All @@ -70,7 +71,8 @@ public void testFileWithIgnore() throws Exception {
final String name = entry.getName();
names.add(name);
}
assertThat(names, containsInAnyOrder("Dockerfile", "bin/date.sh", "subdir2/keep-me", ".dockerignore"));
assertThat(names,containsInAnyOrder("Dockerfile", "bin/date.sh", "subdir2/keep-me",
".dockerignore"));
}
}

Expand Down
Expand Up @@ -419,6 +419,25 @@ public void progress(ProgressMessage message) throws DockerException {
assertThat(returnedImageId, is(imageIdFromMessage.get()));
}

@Test
public void testBuildImageIdPathToDockerFile() throws Exception {
final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
final AtomicReference<String> imageIdFromMessage = new AtomicReference<>();

final String returnedImageId = sut.build(
Paths.get(dockerDirectory), "test", "innerDir/innerDockerfile", new ProgressHandler() {
@Override
public void progress(ProgressMessage message) throws DockerException {
final String imageId = message.buildImageId();
if (imageId != null) {
imageIdFromMessage.set(imageId);
}
}
});

assertThat(returnedImageId, is(imageIdFromMessage.get()));
}

@Test
public void testBuildImageIdWithAuth() throws Exception {
final String dockerDirectory = Resources.getResource("dockerDirectory").getPath();
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/dockerDirectory/innerDir/innerDockerfile
@@ -0,0 +1,3 @@
FROM busybox
ADD bin/date.sh /
CMD ["/date.sh"]

0 comments on commit b3b7371

Please sign in to comment.