private boolean isRunning(InspectContainerResponse containerInfo) { try { return containerInfo != null && containerInfo.getState().getRunning(); } catch (DockerException e) { return false; } } }
public static boolean isContainerExitCodeSuccess(InspectContainerResponse.ContainerState state) { int exitCode = state.getExitCode(); // 0 is the only exit code we can consider as success return exitCode == 0; } }
@Override public <T extends ResultCallback<A_RES_T>> T exec(T resultCallback) { execution.exec((CMD_T) this, resultCallback); return resultCallback; }
private void putImageInRegistry() throws InterruptedException { // It doesn't matter which image we use for this test, but use one that's likely to have been pulled already final String dummySourceImage = TestcontainersConfiguration.getInstance().getRyukImage(); client.pullImageCmd(dummySourceImage) .exec(new PullImageResultCallback()) .awaitCompletion(1, TimeUnit.MINUTES); final String id = client.inspectImageCmd(dummySourceImage) .exec() .getId(); // push the image to the registry client.tagImageCmd(id, testImageName, "latest").exec(); client.pushImageCmd(testImageNameWithTag) .exec(new PushImageResultCallback()) .awaitCompletion(1, TimeUnit.MINUTES); // remove the image tag from local docker so that it must be pulled before use client.removeImageCmd(testImageNameWithTag).withForce(true).exec(); } }
private void exposePortThroughProxy(String networkAlias, int originalPort, int mappedPort) { ExecCreateCmdResponse createCmdResponse = dockerClient .execCreateCmd(proxy.getContainerId()) .withCmd("/usr/bin/socat", "TCP-LISTEN:" + originalPort + ",fork,reuseaddr", "TCP:" + networkAlias + ":" + mappedPort) .exec(); dockerClient.execStartCmd(createCmdResponse.getId()) .exec(new ExecStartResultCallback()); }
/** * Check whether the image is available locally and pull it otherwise */ public void checkAndPullImage(DockerClient client, String image) { List<Image> images = client.listImagesCmd().withImageNameFilter(image).exec(); if (images.isEmpty()) { client.pullImageCmd(image).exec(new PullImageResultCallback()).awaitSuccess(); } }
private void tagImage(String sourceImage, String targetImage, String targetTag) throws InterruptedException { DockerClient client = DockerClientFactory.instance().client(); client.pullImageCmd(sourceImage).exec(new PullImageResultCallback()).awaitCompletion(); client.tagImageCmd(sourceImage, targetImage, targetTag).exec(); } }
/** * @return has the container health state 'healthy'? */ default boolean isHealthy() { if (getContainerId() == null) { return false; } try { InspectContainerResponse inspectContainerResponse = getCurrentContainerInfo(); String healthStatus = inspectContainerResponse.getState().getHealth().getStatus(); return healthStatus.equals(STATE_HEALTHY); } catch (DockerException e) { return false; } }
protected void configure(BuildImageCmd buildImageCmd) { buildImageCmd.withTag(this.getDockerImageName()); } }
@Override protected void configure() { setStartupAttempts(3); withCreateContainerCmdModifier(cmd -> cmd.withCapAdd(IPC_LOCK)); if(!isVaultPortRequested()){ withEnv("VAULT_ADDR", "http://0.0.0.0:" + VAULT_PORT); } }
/** * {@inheritDoc} */ @Override public synchronized Info fetchDockerDaemonInfo() throws IOException { if (this.dockerDaemonInfo == null) { this.dockerDaemonInfo = this.dockerClient.infoCmd().exec(); } return this.dockerDaemonInfo; }
@Override public void onComplete() { resultCallback.onComplete(); command.close(); } };
public DockerClientImpl withDockerCmdExecFactory(DockerCmdExecFactory dockerCmdExecFactory) { checkNotNull(dockerCmdExecFactory, "dockerCmdExecFactory was not specified"); this.dockerCmdExecFactory = dockerCmdExecFactory; this.dockerCmdExecFactory.init(dockerClientConfig); return this; }
@Override @SuppressWarnings("unchecked") public RES_T exec() throws DockerException { LOGGER.debug("Cmd: {}", this); return execution.exec((CMD_T) this); }
/** * @return is the container currently running? */ default boolean isRunning() { if (getContainerId() == null) { return false; } try { Boolean running = getCurrentContainerInfo().getState().getRunning(); return Boolean.TRUE.equals(running); } catch (DockerException e) { return false; } }
@Override public void close() throws IOException { resultCallback.close(); command.close(); }
@Override public void onComplete() { resultCallback.onComplete(); command.close(); } };
@Override public void close() throws IOException { resultCallback.close(); command.close(); }