Docker Community Forums

Share and learn in the Docker community.

Docker exec API: using stdin to upload a file


(Sean Boran) #1

Hi,

A file can be uploaded to a container using the docker command line as follows

echo bar > /tmp/foo.txt
cat /tmp/foo.txt | docker exec -i vanilla2 sh -c 'cat >/tmp/dest.foo.txt’
docker exec -i vanilla2 sh -c ‘cat /tmp/dest.foo.txt’

Now, how to do this via the API?
There are two steps, create the “exec” and start it
In php with a buzzle client, one can create an exec instance:
$body = [
‘AttachStdin’ => true,
‘AttachStdout’ => true,
‘AttachStderr’ => true,
‘Tty’ => false,
‘Cmd’ => ["/bin/bash", “-c”, “cat >/tmp/dest.foo.txt”]
];
$response = $client->post([’/containers/{id}/exec’, [‘id’ => ‘vanilla2’]], [
‘body’ => Json::encode($body),
‘headers’ => [‘content-type’ => ‘application/json’],
]);
$execid=$response->json()[‘Id’];

Next, then to start that exec, its is unclear to me how to pipe in the stdin (i.e. the contenats of /tmp/foo.txt obove)

    $body = ['Detach' => false, 'Tty' => $tty ];
    $response = $client->post(['/exec/{id}/start', ['id' => $execid]], [
        'body'         => Json::encode($body),
        'headers'      => ['content-type' => 'application/json'],
    ]);

Reference doc is here: http://docs.docker.com/reference/api/docker_remote_api_v1.16/#exec-start
It talks about a stream result (for stdout/err), but how does one stream in stdin?

Any suggestions?


(Roel Van De Paar) #2

Docker was recently extended to have a fuller cp implementation - may this help?

See here and bit more history here


(Sean Boran) #3

Yes, looks nice and it looks like that will be included in docker 1.6. I had been hoping to find a solution earlier …


(Roel Van De Paar) #4

@boran Maybe it will be available sooner using in-place “lastest-docker” wget upgrade (somewhere documented)? Not atm yet…

$ sudo service docker stop
Redirecting to /bin/systemctl stop docker.service
$ sudo wget https://get.docker.com/builds/Linux/x86_64/docker-latest -O /usr/bin/docker
[…]
2015-02-12 07:24:06 (750 KB/s) - ‘/usr/bin/docker’ saved [15005140/15005140]
$ sudo service docker start
Redirecting to /bin/systemctl start docker.service
$ sudo docker --version
Docker version 1.5.0, build a8a31ef