@Test @Ignore("Need a running Docker daemon") public void testCreateKarafContainer() throws Exception { ContainerConfig config = new ContainerConfig(); config.setTty(true); config.setAttachStdout(true); config.setAttachStderr(true); config.setAttachStdin(true); config.setImage("karaf:latest"); config.setHostname("docker"); config.setUser(""); config.setCmd(new String[]{ "/bin/karaf" }); config.setWorkingDir(""); config.setOpenStdin(true); config.setStdinOnce(true); Map<String, Map<String, String>> exposedPorts = new HashMap<>(); Map<String, String> exposedPort = new HashMap<>(); exposedPorts.put("8101/tcp", exposedPort); config.setExposedPorts(exposedPorts); HostConfig hostConfig = new HostConfig(); hostConfig.setPrivileged(false); hostConfig.setPublishAllPorts(false); File dock = new File("/tmp/docker", "karaf"); if (dock.exists()) { hostConfig.setBinds(new String[]{dock.getAbsolutePath() + ":/opt/apache-karaf"}); } hostConfig.setNetworkMode("bridge"); hostConfig.setLxcConf(new String[]{}); Map<String, List<HostPortBinding>> portBindings = new HashMap<>(); List<HostPortBinding> hostPortBindings = new ArrayList<>(); HostPortBinding hostPortBinding = new HostPortBinding(); hostPortBinding.setHostIp(""); hostPortBinding.setHostPort("8101"); hostPortBindings.add(hostPortBinding); portBindings.put("8101/tcp", hostPortBindings); hostConfig.setPortBindings(portBindings); config.setHostConfig(hostConfig); dockerClient.create(config, "karaf"); }
public void create(ContainerConfig config, String name) throws Exception { URL dockerUrl = new URL(this.url + "/containers/create?name=" + name); HttpURLConnection connection = (HttpURLConnection) dockerUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); mapper.writeValue(connection.getOutputStream(), config); if (connection.getResponseCode() != 201) { throw new IllegalStateException("Can't create Docker container " + name + ": " + connection.getResponseMessage()); } }
DockerClient { public void create(ContainerConfig config, String name) throws Exception { URL dockerUrl = new URL(this.url + "/containers/create?name=" + name); HttpURLConnection connection = (HttpURLConnection) dockerUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); mapper.writeValue(connection.getOutputStream(), config); if (connection.getResponseCode() != 201) { throw new IllegalStateException("Can't create Docker container " + name + ": " + connection.getResponseMessage()); } } }
DockerClient { public void create(ContainerConfig config, String name) throws Exception { URL dockerUrl = new URL(this.url + "/containers/create?name=" + name); HttpURLConnection connection = (HttpURLConnection) dockerUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); mapper.writeValue(connection.getOutputStream(), config); if (connection.getResponseCode() != 201) { throw new IllegalStateException("Can't create Docker container " + name + ": " + connection.getResponseMessage()); } } DockerClient(String url); }
DockerClient { public void create(ContainerConfig config, String name) throws Exception { URL dockerUrl = new URL(this.url + "/containers/create?name=" + name); HttpURLConnection connection = (HttpURLConnection) dockerUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); mapper.writeValue(connection.getOutputStream(), config); if (connection.getResponseCode() != 201) { throw new IllegalStateException("Can't create Docker container " + name + ": " + connection.getResponseMessage()); } } DockerClient(String url); Info info(); Version version(); List<Container> ps(boolean showAll); Container inspect(String id); Top top(String id); void create(ContainerConfig config, String name); void rm(String id, boolean removeVolumes, boolean force); void start(String id); void stop(String id, int timeToWait); void restart(String id, int timeToWait); void kill(String id, String signal); void rename(String id, String name); void pause(String id); void unpause(String id); String logs(String id, boolean stdout, boolean stderr, boolean timestamps, boolean details); void commit(String container, ContainerConfig config, String message, String repo, String tag); List<Image> images(boolean showAll); void pull(String name, String tag, boolean verbose); void push(String name, String tag, boolean verbose); void tag(String name, String repo, String tag); void rmi(String name, boolean force, boolean noprune); List<ImageSearch> search(String term); Image getImage(String name); List<ImageHistory> history(String name); }
DockerClient { public void create(ContainerConfig config, String name) throws Exception { URL dockerUrl = new URL(this.url + "/containers/create?name=" + name); HttpURLConnection connection = (HttpURLConnection) dockerUrl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); mapper.writeValue(connection.getOutputStream(), config); if (connection.getResponseCode() != 201) { throw new IllegalStateException("Can't create Docker container " + name + ": " + connection.getResponseMessage()); } } DockerClient(String url); Info info(); Version version(); List<Container> ps(boolean showAll); Container inspect(String id); Top top(String id); void create(ContainerConfig config, String name); void rm(String id, boolean removeVolumes, boolean force); void start(String id); void stop(String id, int timeToWait); void restart(String id, int timeToWait); void kill(String id, String signal); void rename(String id, String name); void pause(String id); void unpause(String id); String logs(String id, boolean stdout, boolean stderr, boolean timestamps, boolean details); void commit(String container, ContainerConfig config, String message, String repo, String tag); List<Image> images(boolean showAll); void pull(String name, String tag, boolean verbose); void push(String name, String tag, boolean verbose); void tag(String name, String repo, String tag); void rmi(String name, boolean force, boolean noprune); List<ImageSearch> search(String term); Image getImage(String name); List<ImageHistory> history(String name); static final String DEFAULT_URL; }