javadockerdocker-java

Docker-Java: Starting container with arguments


I'm trying to start a docker container using docker-java. Using the method DockerClient.startContainerCmd works for me, but I need to start the container with arguments. The StartContainerCmd class that is returned from this method does not have any methods to supply arguments before I execute it.

Is there a way to do it?


Solution

  • The StartContainerCmd delegates to StartContainerCmdExec which abstracts the Start Container REST operation. The Operation has only one path parameter (id of the container). If you need to supply additional arguments you need to create the container with those arguments:

    CreateContainerResponse container = dockerClient.createContainerCmd(IMAGE_NAME)
                    .withCmd("cmd", "arg1", "arg2").exec()
    

    get the id of the container and then start it:

    dockerClient.startContainerCmd(container.getId()).exec();