tmux on Docker - blitterated/docker-dev-env GitHub Wiki

How to run tmux in a Docker container?

Is it better to do

docker run -it --rm foo /bin/bash
tmux

or

docker run -it --rm foo /usr/bin/tmux

Doesn't seem to matter. Just running tmux on its own picks up on the shell set for the user in /etc/passwd.

Would it be better to just use CMD to run tmux?

First, test that tmux uses the shell specified in /etc/passwd

Create an image with tmux installed.

cat <<"EOF" > Dockerfile
FROM ubuntu

RUN apt update && apt --yes upgrade && \
    apt install --yes tmux
EOF
docker build -t tmuxing .

Create a named container running tmux.

docker run -it --name tmux_shell tmuxing /usr/bin/tmux

Check to see if tmux is running and which shell is running.

ps a
  PID TTY      STAT   TIME COMMAND
    1 pts/0    Ss+    0:00 /usr/bin/tmux
   10 pts/1    Ss     0:00 -bash
   20 pts/1    R+     0:00 ps a

See what other shells are available on the image.

cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/tmux

Setup the root user to use dash shell instead.

usermod --shell /bin/dash root

Bail out of the container so we can restart it.

exit

Restart and attach to the container.

docker start --attach -i tmux_shell

Check again to see if tmux is running and which shell is running.

ps a
  PID TTY      STAT   TIME COMMAND
    1 pts/0    Ss+    0:00 /usr/bin/tmux
    9 pts/1    Ss     0:00 -dash
   14 pts/1    R+     0:00 ps a

And we're up and running with dash instead. This shows that tmux uses the user's default shell.

Exit out, and then delete the image.

exit
docker rm tmux_shell

Second, test running tmux with CMD and attaching

Overwrite the Dockerfile with a new one that uses CMD.

cat <<"EOF" > Dockerfile
FROM ubuntu

RUN apt update && apt --yes upgrade && \
    apt install --yes tmux

CMD ["tmux"]
EOF

Build the image.

docker build -t tmuxd .

Run a container in daemon mode.

docker run -d --name tmuxd_test tmuxd

Check on it.

docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                      PORTS     NAMES
71a754b70a2c   tmuxd     "tmux"    35 seconds ago   Exited (1) 34 seconds ago             tmuxd_test

That quit pretty much immediately.

Let's try by explicitly starting a tmux session.

First, delete the old image and container.

docker rm tmuxd_test
docker rmi tmuxd

Update the Dockerfile.

cat <<"EOF" > Dockerfile
FROM ubuntu

RUN apt update && apt --yes upgrade && \
    apt install --yes tmux

ENTRYPOINT ["tmux"]
CMD ["new-session", "-s", "tmux-test", "/bin/bash"]
EOF

Build the image.

docker build -t tmuxd .

Run a container in daemon mode.

docker run -d --name tmuxd_test tmuxd

Check on it.

docker ps -a

This also bailed immediately.

CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                     PORTS     NAMES
dd728aa7dbc5   tmuxd     "tmux new-session -s…"   5 seconds ago   Exited (1) 4 seconds ago             tmuxd_test

Tried to start and attach unsuccessfully in a couple of ways.

docker start -a tmuxd_test
docker start -i tmuxd_test
docker start -ia tmuxd_test

They all exited with the same message.

open terminal failed: not a terminal

Running the same command interactively works fine.

docker run -it --rm --entrypoint tmux tmuxd new-session -s tmux-test /bin/bash

Not sure how to keep the session up and running while in daemon mode. Overall this was a good set of experiments with Docker and tmux.

⚠️ **GitHub.com Fallback** ⚠️