Connect container process to container STDIN

I have an image with ENTRYPOINT ["/usr/sbin/init"], i.e. systemd since one of the processes running in the container requires systemd. I need to use this same image for Gitlab CI, which creates a container, attaches to it and sends the CI command to the STDIN. So it actually requires a shell running in the container and listening to the container’s STDIN. I can start a bash shell in the container using a systemd unit, but I cannot figure out how to connect its STDIN to the container’s STDIN. Is there any way to do this?

Thanks and sorry for the confused explanation but I am a beginner and I’m not familiar with these topics.

It depends on how you want to run the container (docker run, docker compose, etc…). The option -i of docker run and the compose parameter stdin_open is what you need, but if you run Systemd in the container, how will your process get the input? Is this something that Systemd does automatically forwarding the standard input to every process?


I wrote too quickly…

docker attach will not work. docker exec would with the option -i

I found a way to obtain the desired behavior. I created a unit starting a bash shell whose stdin and stdout are those of PID 1:

Description=Start bash shell

ExecStart=/bin/bash -c "exec /bin/bash < /proc/1/fd/0 > /proc/1/fd/1"


In this way after docker attach -i the input is piped to the shell in the container, so Gitlab CI is able to send commands to the container. The trick works because PID 1, i.e. systemd, does not read from stdin so the pipe is exclusively read by the shell process.