4

Running into a problem where tmux does not initialize in a docker container file. Also, there is search-blockage in the form of people attaching tmux to a pre-existing container. That is not what I am trying to do.

# in the dockerfile
FROM debian:latest
RUN apt-get install tmux
RUN tmux new -s foo -d
RUN tmux ls
_________________________

$: docker build . 
...
 > [8/8] RUN tmux ls:
#11 0.415 no server running on /tmp/tmux-0/default
------
executor failed running [/bin/sh -c tmux ls]: exit code: 1

What I am trying to do is run a couple of 'apps' that need to talk to each other at the same time. The idea is to avoid using docker compose for this particular problem.

I can easily run both commands as background processes.

But, I'd rather have them running in a tmux session that I can attach to when the image is running.

How does one launch tmux inside the docker container during the build process?

Chris
  • 28,822
  • 27
  • 83
  • 158

1 Answers1

3

This is a problem with layers:

This code is gona do your work.

FROM debian:latest
RUN apt-get update && apt-get install -y tmux
RUN tmux new -s foo -d && tmux ls

But it will only solve the build issue, not your problem.

You should do that in the entrypoint, because tmux is killed when the layer is terminated...

During the build on each layers you can start some processes(you started tmux). After layer build is finished, all processes started in that particular layers are killed.

Container is just another layer on top of read-only image layers, so you need to start tmux there.

Entrypoint is a bash script you run when container is bootstrapping.

Example entrypoint can be:

#!/bin/bash
# entrypoint.sh

set -eu
tmux new -s foo -d && tmux ls

exec "$@"
  • Add executable permissions for your entrypoint.sh:
    chmod +x entrypoint.sh
  • Then you need to copy and execute it in Dockerfile \
    COPY ./entrypoint.sh /entrypoint.sh
    ENTRYPOINT ["/entrypoint.sh"]
    

Another solutions

Starting multiple programs in the docker image is bad practice. You are loosing main docker advantage, which is separation.

However, The official docs says a word about your problem: https://docs.docker.com/config/containers/multi-service_container/

I really like to do it (when i have to) with supervisord. This is a small program to manage your programs.

References

Daniel Hornik
  • 1,957
  • 1
  • 14
  • 33