Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/com/github/dockerjava/api/DockerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.command.CommitCmd;
import com.github.dockerjava.api.command.ContainerDiffCmd;
import com.github.dockerjava.api.command.CopyArchiveFromContainerCmd;
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
import com.github.dockerjava.api.command.CopyFileFromContainerCmd;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateImageCmd;
Expand Down Expand Up @@ -46,6 +48,7 @@
import com.github.dockerjava.api.exception.DockerException;
import com.github.dockerjava.api.model.AuthConfig;
import com.github.dockerjava.api.model.Identifier;
import com.github.dockerjava.core.RemoteApiVersion;

// https://godoc.org/github.com/fsouza/go-dockerclient
public interface DockerClient extends Closeable {
Expand Down Expand Up @@ -121,8 +124,37 @@ public interface DockerClient extends Closeable {

public LogContainerCmd logContainerCmd(@Nonnull String containerId);

/**
* Copy resource from container to local machine.
*
* @param containerId id of the container
* @param resource path to container's resource
* @return created command
* @since {@link RemoteApiVersion#VERSION_1_20}
*/
public CopyArchiveFromContainerCmd copyArchiveFromContainerCmd(@Nonnull String containerId, @Nonnull String resource);

/**
* Copy resource from container to local machine.
*
* @param containerId id of the container
* @param resource path to container's resource
* @return created command
* @see #copyArchiveFromContainerCmd(String, String)
* @deprecated since docker API version 1.20, replaced by {@link #copyArchiveFromContainerCmd(String, String)}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not critical, but for simplification there are API versions for linking javadocs. Example

)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try change link to version here also

*/
@Deprecated
public CopyFileFromContainerCmd copyFileFromContainerCmd(@Nonnull String containerId, @Nonnull String resource);

/**
* Copy archive from local machine to remote container
*
* @param containerId id of the container
* @return created command
* @since {@link RemoteApiVersion#VERSION_1_20}
*/
public CopyArchiveToContainerCmd copyArchiveToContainerCmd(@Nonnull String containerId);

public ContainerDiffCmd containerDiffCmd(@Nonnull String containerId);

public StopContainerCmd stopContainerCmd(@Nonnull String containerId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.dockerjava.api.command;

import java.io.InputStream;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import com.github.dockerjava.api.exception.NotFoundException;

public interface CopyArchiveFromContainerCmd extends SyncDockerCmd<InputStream> {

@CheckForNull
public String getContainerId();

@CheckForNull
public String getHostPath();

@CheckForNull
public String getResource();

public CopyArchiveFromContainerCmd withContainerId(@Nonnull String containerId);

public CopyArchiveFromContainerCmd withHostPath(String hostPath);

public CopyArchiveFromContainerCmd withResource(@Nonnull String resource);

/**
* Its the responsibility of the caller to consume and/or close the {@link InputStream} to prevent connection leaks.
*
* @throws NotFoundException
* No such container
*/
@Override
public InputStream exec() throws NotFoundException;

public static interface Exec extends DockerCmdSyncExec<CopyArchiveFromContainerCmd, InputStream> {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.dockerjava.api.command;

import java.io.InputStream;

import com.github.dockerjava.api.exception.NotFoundException;

public interface CopyArchiveToContainerCmd extends SyncDockerCmd<Void> {

String getContainerId();

String getHostResource();

InputStream getTarInputStream();

boolean isNoOverwriteDirNonDir();

boolean isDirChildrenOnly();

/**
* Set container's id
*
* @param containerId id of the container to copy file to
*/
CopyArchiveToContainerCmd withContainerId(String containerId);

/**
* Set path to the resource on the host machine
*
* @param resource path to the resource on the host machine
*/
CopyArchiveToContainerCmd withHostResource(String resource);

/**
* Set the tar input stream that will be uploaded to the container. withHostResource or withTarInputStream can be defined but not both.
*
* @param tarInputStream the stream to upload to the container
*/
CopyArchiveToContainerCmd withTarInputStream(InputStream tarInputStream);

/**
* If set to true then it will be an error if unpacking the given content would cause an existing directory to be replaced with a non-directory and vice versa
*
* @param noOverwriteDirNonDir flag to know if non directory can be overwritten
*/
CopyArchiveToContainerCmd withNoOverwriteDirNonDir(boolean noOverwriteDirNonDir);

/**
* If this flag is set to true, all children of the local directory will be copied to the remote without the root directory.
* For ex: if I have root/titi and root/tata and the remote path is /var/data.
* dirChildrenOnly = true will create /var/data/titi and /var/data/tata
* dirChildrenOnly = false will create /var/data/root/titi and /var/data/root/tata
*
* @param dirChildrenOnly if root directory is ignored
*/
CopyArchiveToContainerCmd withDirChildrenOnly(boolean dirChildrenOnly);

String getRemotePath();

CopyArchiveToContainerCmd withRemotePath(String remotePath);

@Override
Void exec() throws NotFoundException;

interface Exec extends DockerCmdSyncExec<CopyArchiveToContainerCmd, Void> {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public interface DockerCmdExecFactory extends Closeable {

public CopyFileFromContainerCmd.Exec createCopyFileFromContainerCmdExec();

public CopyArchiveFromContainerCmd.Exec createCopyArchiveFromContainerCmdExec();

public CopyArchiveToContainerCmd.Exec createCopyArchiveToContainerCmdExec();

public StopContainerCmd.Exec createStopContainerCmdExec();

public ContainerDiffCmd.Exec createContainerDiffCmdExec();
Expand All @@ -83,5 +87,4 @@ public interface DockerCmdExecFactory extends Closeable {

@Override
public void close() throws IOException;

}
31 changes: 23 additions & 8 deletions src/main/java/com/github/dockerjava/core/DockerClientImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.github.dockerjava.core;

import static com.google.common.base.Preconditions.checkNotNull;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static usually imported after all imports

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using Intellij, it's re-arranged automatically like that by default. Please feel free to re-organize.


import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.AttachContainerCmd;
import com.github.dockerjava.api.command.AuthCmd;
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.command.CommitCmd;
import com.github.dockerjava.api.command.ContainerDiffCmd;
import com.github.dockerjava.api.command.CopyArchiveFromContainerCmd;
import com.github.dockerjava.api.command.CopyFileFromContainerCmd;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.CreateImageCmd;
Expand Down Expand Up @@ -45,6 +53,9 @@
import com.github.dockerjava.core.command.BuildImageCmdImpl;
import com.github.dockerjava.core.command.CommitCmdImpl;
import com.github.dockerjava.core.command.ContainerDiffCmdImpl;
import com.github.dockerjava.core.command.CopyArchiveFromContainerCmdImpl;
import com.github.dockerjava.api.command.CopyArchiveToContainerCmd;
import com.github.dockerjava.core.command.CopyArchiveToContainerCmdImpl;
import com.github.dockerjava.core.command.CopyFileFromContainerCmdImpl;
import com.github.dockerjava.core.command.CreateContainerCmdImpl;
import com.github.dockerjava.core.command.CreateImageCmdImpl;
Expand Down Expand Up @@ -77,16 +88,8 @@
import com.github.dockerjava.core.command.VersionCmdImpl;
import com.github.dockerjava.core.command.WaitContainerCmdImpl;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import static com.google.common.base.Preconditions.checkNotNull;

/**
* @author Konstantin Pelykh ([email protected])
*
* @see "https://github.com/docker/docker/blob/master/api/client/commands.go"
*/
public class DockerClientImpl implements Closeable, DockerClient {
Expand Down Expand Up @@ -304,6 +307,18 @@ public CopyFileFromContainerCmd copyFileFromContainerCmd(String containerId, Str
containerId, resource);
}

@Override
public CopyArchiveFromContainerCmd copyArchiveFromContainerCmd(String containerId, String resource) {
return new CopyArchiveFromContainerCmdImpl(getDockerCmdExecFactory().createCopyArchiveFromContainerCmdExec(),
containerId, resource);
}

@Override
public CopyArchiveToContainerCmd copyArchiveToContainerCmd(String containerId) {
return new CopyArchiveToContainerCmdImpl(getDockerCmdExecFactory().createCopyArchiveToContainerCmdExec(),
containerId);
}

@Override
public ContainerDiffCmd containerDiffCmd(String containerId) {
return new ContainerDiffCmdImpl(getDockerCmdExecFactory().createContainerDiffCmdExec(), containerId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.github.dockerjava.core.command;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.InputStream;

import com.github.dockerjava.api.command.CopyArchiveFromContainerCmd;
import com.github.dockerjava.api.exception.NotFoundException;

/**
* Copy files or folders from a container.
*/
public class CopyArchiveFromContainerCmdImpl extends AbstrDockerCmd<CopyArchiveFromContainerCmd, InputStream> implements
CopyArchiveFromContainerCmd {

private String containerId;

private String hostPath = ".";

private String resource;

public CopyArchiveFromContainerCmdImpl(CopyArchiveFromContainerCmd.Exec exec, String containerId, String resource) {
super(exec);
withContainerId(containerId);
withResource(resource);
}

@Override
public String getContainerId() {
return containerId;
}

@Override
public String getResource() {
return resource;
}

@Override
public CopyArchiveFromContainerCmdImpl withContainerId(String containerId) {
checkNotNull(containerId, "containerId was not specified");
this.containerId = containerId;
return this;
}

@Override
public CopyArchiveFromContainerCmdImpl withResource(String resource) {
checkNotNull(resource, "resource was not specified");
this.resource = resource;
return this;
}

@Override
public String getHostPath() {
return hostPath;
}

@Override
public CopyArchiveFromContainerCmdImpl withHostPath(String hostPath) {
checkNotNull(hostPath, "hostPath was not specified");
this.hostPath = hostPath;
return this;
}

/**
* @throws NotFoundException No such container
*/
@Override
public InputStream exec() throws NotFoundException {
return super.exec();
}
}
Loading