Fork me on GitHub
#docker
<
2023-05-16
>
Dan Boitnott12:05:43

Anybody understand the Docker API enough to tell how to do the equivalent of the command below using the API?

$ printf "print('hi')" |docker run -i --rm python:latest
hi
I've been trying several approaches and I can't seem to make all of these things happen at once but they're clearly possible because the CLI's doing it. 1. The input is flushed despite not ending with a newline character. 2. EOF is sent to python it writes the result and exits

lispyclouds14:05:43

I think this would be a combination of the create, start and attach api calls. Are you using the api directly or via a programmatic interface?

lispyclouds14:05:14

If using contajners, after the container is created and started this could be used to send data to its stdin: https://github.com/lispyclouds/contajners/blob/main/doc/002-general-guidelines.md#attach-to-a-container-and-send-data-to-stdin

Dan Boitnott15:05:20

I am using contajners and followed the example but I've had no luck. I'm seeing suggestions that maybe the Docker CLI just handles it specially and it's not doable with the API. To clarify, I'm wanting to use contajners to launch a container, pipe in some input, then read the output until the process exits on its own.

Dan Boitnott15:05:46

(Exits from receiving EOF since I can't guarantee there will be an exit() at the end of the input stream)

lispyclouds15:05:39

Do you have the code that you tried that can be shared here?

lispyclouds15:05:03

The way I’d do it is; create, start, attach to stdin, attach to stdout, container wait til it exits.

Dan Boitnott15:05:16

I'm able to start the process and I'm able to pipe in the input and able to confirm (using docker logs -f) that it's being received and the appropriate output logged. But I don't have a way to send EOF to the running container process so that it will finish processing input and exit.

Dan Boitnott15:05:10

Based on the documentation and what I'm finding with Google, it seems like maybe this is a known thing you can't do with the API.

lispyclouds15:05:15

I’ll try it out when I have access to a proper machine