Why screen folder in /var/run disappears in Docker container after running Ansible playbook? Files

I've got the following Dockerfile:

FROM ansible/ansible:ubuntu1604py3
ENV DEBIAN_FRONTEND noninteractive
RUN pip3 install ansible
RUN printf '[local]\r\nlocalhost ansible_python_interpreter="env python3"\r\n' &gt; /etc/ansible/hosts
ADD . /opt
RUN ansible-playbook /opt/install_screen.yml -c local -v
RUN useradd -d /home/ubuntu -ms /bin/bash -g root -G sudo -p ubuntu ubuntu
USER ubuntu
WORKDIR /home/ubuntu
CMD ["/usr/bin/env", "screen"]


and the following install_screen.yml playbook file:

- hosts: localhost
  tasks:
    - name: Install screen (a full-screen window manager)
      apt: name=screen state=present
      register: screen_installed
    - name: Configure screen
      shell: bash -x /etc/init.d/screen-cleanup start
      register: screen_configured
      when: screen_installed is success
    - shell: ls -la /var/run/




Build

Now, I build the Docker container in a shell by:

$ docker build -t screen .


and the Configure screen task shows the following output:


  changed: [localhost] =&gt; {"changed": true, "cmd": "bash -x /etc/init.d/screen-cleanup start", "delta": "0:00:00.009613", "end": "2018-05-01 23:09:49.159514", "rc": 0, "start": "2018-05-01 23:09:49.149901", "stderr": "+ set -e\n+ test -f /usr/bin/screen\n+ SCREENDIR=/var/run/screen\n+ case \"$1\" in\n+ test -L /var/run/screen\n+ test -d /var/run/screen\n+ find /var/run/screen -type p -delete\n++ stat -c%a /usr/bin/screen\n+ BINARYPERM=2755\n+ '[' 2755 -ge 4000 ']'\n+ '[' 2755 -ge 2000 ']'\n+ chmod 0775 /var/run/screen\n+ exit 0", "stderr_lines": ["+ set -e", "+ test -f /usr/bin/screen", "+ SCREENDIR=/var/run/screen", "+ case \"$1\" in", "+ test -L /var/run/screen", "+ test -d /var/run/screen", "+ find /var/run/screen -type p -delete", "++ stat -c%a /usr/bin/screen", "+ BINARYPERM=2755", "+ '[' 2755 -ge 4000 ']'", "+ '[' 2755 -ge 2000 ']'", "+ chmod 0775 /var/run/screen", "+ exit 0"], "stdout": "", "stdout_lines": []}


then my ls -la /var/run/ shell command shows:


  changed: [localhost] =&gt; {"changed": true, "cmd": "ls -la /var/run/", "delta": "0:00:00.004997", "end": "2018-05-01 23:09:49.442670", "rc": 0, "start": "2018-05-01 23:09:49.437673", "stderr": "", "stderr_lines": [], "stdout": "total 24\ndrwxr-xr-x 6 root root 4096 May  1 23:09 .\ndrwxr-xr-x 1 root root 4096 May  1 23:09 ..\n-rw-r--r-- 1 root root    0 Mar 28 19:06 init.upgraded\ndrwxrwxrwt 2 root root 4096 Feb 28 19:13 lock\ndrwxr-xr-x 2 root root 4096 May  1 23:09 mount\ndrwxrwxr-x 2 root utmp 4096 May  1 23:09 screen\ndrwxr-xr-x 2 root root 4096 May  1 23:09 systemd\n-rw-rw-r-- 1 root utmp    0 Feb 28 19:13 utmp", "stdout_lines": ["total 24", "drwxr-xr-x 6 root root 4096 May  1 23:09 .", "drwxr-xr-x 1 root root 4096 May  1 23:09 ..", "-rw-r--r-- 1 root root    0 Mar 28 19:06 init.upgraded", "drwxrwxrwt 2 root root 4096 Feb 28 19:13 lock", "drwxr-xr-x 2 root root 4096 May  1 23:09 mount", "drwxrwxr-x 2 root utmp 4096 May  1 23:09 screen", "drwxr-xr-x 2 root root 4096 May  1 23:09 systemd", "-rw-rw-r-- 1 root utmp    0 Feb 28 19:13 utmp"]}


Problem

It seems after installing screen and running /etc/init.d/screen-cleanup start during provisioning, the container have the problem running screen command as below:

$ docker run -it screen
Cannot make directory '/var/run/screen': Permission denied
$ docker run -it screen ls -la /var/run/screen
ls: cannot access '/var/run/screen': No such file or directory


In above Ansible's output I can see that /var/run/screen folder exists as per the following line (during ls -la /var/run/ run):


  drwxrwxr-x 2 root utmp 4096 May  1 23:09 screen


But it disappears after Ansible playbook command is finished.

Workaround

The following one-time workaround works to recreate the folder:

$ docker run -it -u root screen bash
ubuntu# bash -x /etc/init.d/screen-cleanup start
...
+ mkdir /var/run/screen
...
Rather than solving the immediate problem you're having, let me suggest an alternative solution to your actual problem:


  I need screen, so I can use multiple interactive shells in one instance of Docker. If I run multiple Docker instances, each takes 1G of RAM, so 10+ instances will kill my RAM.


Run screen (or tmux, but screen if you insist) locally on your workstation.  Start up your docker container, and in the windows where you want to do something on the container, use docker exec -it &lt;container-id&gt; bash to get a new interactive shell on the container.  This will be much simpler than running screen inside the container, and allows you to also have windows running commands on your workstation.

You may also want to look into using Vagrant with the Docker provider to simplify the process of doing development on a Docker container.