qweq
qweq

Reputation: 125

How to change directories in Linux with Docker

My code is working but I can't change directories using the command. I can ls,touch, etc... but I can't cd.

In this case message is the command I want. ex: ls

What is this fix for this?

public static String getDockerConsole(String containerName, String message) {
    String[] cmd = new String[]{
            "/bin/sh",
            "-c",
            message
    };
    DockerClient dockerClient = dockerClient();
    Container container = getContainer(containerName);
    ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId()).withAttachStdout(true).withTty(true).withAttachStderr(true).withCmd(cmd).withUser("root").exec();
    ByteArrayOutputStream  out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();

    try {
        dockerClient.execStartCmd(container.getId()).withExecId(execCreateCmdResponse.getId()).withTty(true).exec(new ExecStartResultCallback(out, err)).awaitCompletion();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return out.toString() + err.toString();

Upvotes: 1

Views: 96

Answers (1)

Jônatas Renan
Jônatas Renan

Reputation: 93

When ExecCreateCmdResponse is executed with the message 'cd folder', a process is created connected to a tty that executes sh and makes the change to the new folder, but it proccess dies. As it dies, if you run again the sh will return to the home folder.

Try for example cd and ls in the same command sh processes:

message = "cd /bin; ls ; cd /var; ls"

Upvotes: 1

Related Questions