dockermacossocat

Write to stdin of running docker container with socat on macos


I am using socat to write to the stdin of a running docker container. The following code is working fine on Linux:

echo "ls" | socat EXEC:"docker attach container_name",pty STDIN &> /dev/null

On macos however the above doesn't work. I tried playing around with the settings but have not been able to get it to work.

I am using the following version:

$ brew info socat
==> socat: stable 1.8.0.1 (bottled)

Solution

  • When docker attach attempts to write to the PTY, it encounters a "Bad file descriptor" error because the write side isn't properly set up on MacOS.

    So you need :

    echo "ls" | socat EXEC:"docker attach container_name",pty - &> /dev/null
    

    - meaning both standard input and standard output.

    I don't see an easy way to get the exit code, one workaround is to pass it using standard output:

    echo 'false; echo $?' | socat EXEC:"docker attach container_name",pty -