Start a screen session inside LXD-managed container

I'd like to start a screen session inside an LXD-managed container so that I could detach running processes and maybe re-attach in the future when I want to check on their status.

I tried to attach to the container running

$ lxc exec my-ubuntu -- /bin/bash

and then start a session with screen -x, but I get the following error:

Must be connected to a terminal.

I can see that many people have had a similar problem when trying to initiate screen from ssh, but I couldn't apply the suggested solution to my case.


Solution 1:

For LXD using

lxc exec my-ubuntu -- sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -s /bin/bash"

or

lxc exec my-ubuntu -- sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -x"

or with any other screen switch.


The same for Docker ;)

docker run -it my-ubuntu sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -s /bin/bash"