Papkie
Papkie

Reputation: 57

Is it possible to send string into running process inside docker container?

For example i have running docker container with cat (or other process using stdin) defined in CMD Dockerfile option.

I'm trying to send string test\n into running cat (or other process).

Is it possible to do this, or I need to find workaround?

Sum up: I'm looking for something like echo 'test' | docker run -i --rm alpine command for running container.

Upvotes: 2

Views: 1788

Answers (3)

Papkie
Papkie

Reputation: 57

I solved it by simply pipeing stdin to docker attach, for example:

$ docker run -i busybox sh -c "while true; do cat /dev/stdin; sleep 1; done;" test

and in another term

$ echo test | docker attach <containerId>

Upvotes: 2

user2915097
user2915097

Reputation: 32166

Yes as an example see

https://github.com/chilcano/docker-netcat

you need to open a port, extract from the previous link

$ docker run -d -t --name=netcat-jessie -p 8182:8182 -p 9192:9192/udp chilcano/netcat:jessie

and now, you have some examples of communication using those ports send traces to open a TCP port

$ ping 8.8.4.4 | nc -v 192.168.99.100 8182

or send traces to an UDP port

$ ping 8.8.8.8 | nc -vu 192.168.99.100 9192

or send traces to an UDP port without netcat

$ ping 8.8.4.4 > /dev/udp/192.168.99.100/9192

and

$ tail -f /opt/wiremock/wiremock.log | nc -vu 192.168.99.100 9192

or send traces to a TCP port without netcat

$ tail -f /opt/wso2esb01a/repository/logs/wso2carbon.log > /dev/tcp/192.168.99.100/8182

or send traces to an UDP port without netcat

$ tail -f /opt/wso2am02a/repository/logs/wso2carbon.log > /dev/udp/192.168.99.100/9192

Upvotes: 1

thaJeztah
thaJeztah

Reputation: 28997

You can pipe to stdin of the container's process if you start the container with -i. For example;

echo "foobar" | docker run -i --rm alpine cat

Keep in mind, that this is done when starting the container. Your question mentioned cat, which is not a long running process, so the container will actually exit after cat completes.

Upvotes: 3

Related Questions