Answers: <p>Rather than solving the immediate problem you're having, let me suggest an alternative solution to your actual problem:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>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 <code>docker exec -it &lt;container-id&gt; bash</code> 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.</p>
  
  <p>You may also want to look into using <a href="https://www.vagrantup.com/" rel="nofollow noreferrer">Vagrant</a> with <a href="https://www.vagrantup.com/docs/docker/" rel="nofollow noreferrer">the Docker provider</a> to simplify the process of doing development on a Docker container.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-05-05T17:31:30.630Z
Answers: 2018-05-05T17:31:30.630Z
Answers: Xiong Chiamiov
Body: <h3>Files</h3>
  
  <p>I've got the following <strong><code>Dockerfile</code></strong>:</p>
  
  <pre><code>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"]
  </code></pre>
  
  <p>and the following <strong><code>install_screen.yml</code></strong> playbook file:</p>
  
  <pre><code>- 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/
  </code></pre>
  
  <hr>
  
  <h3>Build</h3>
  
  <p>Now, I build the Docker container in a shell by:</p>
  
  <pre><code>$ docker build -t screen .
  </code></pre>
  
  <p>and the <em>Configure screen</em> task shows the following output:</p>
  
  <blockquote>
    <p><code>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": []}</code></p>
  </blockquote>
  
  <p>then my <code>ls -la /var/run/</code> shell command shows:</p>
  
  <blockquote>
    <p><code>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"]}</code></p>
  </blockquote>
  
  <h3>Problem</h3>
  
  <p>It seems after installing <code>screen</code> and running <code>/etc/init.d/screen-cleanup start</code> during provisioning, the container have the problem running <code>screen</code> command as below:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>In above Ansible's output I can see that <code>/var/run/screen</code> folder exists as per the following line (during <code>ls -la /var/run/</code> run):</p>
  
  <blockquote>
    <p><code>drwxrwxr-x 2 root utmp 4096 May  1 23:09 screen</code></p>
  </blockquote>
  
  <p>But it disappears after Ansible playbook command is finished.</p>
  
  <h3>Workaround</h3>
  
  <p>The following one-time workaround works to recreate the folder:</p>
  
  <pre><code>$ docker run -it -u root screen bash
  ubuntu# bash -x /etc/init.d/screen-cleanup start
  ...
  + mkdir /var/run/screen
  ...
  </code></pre>
  
Comments: CC BY-SA 4.0
Comments: 2018-05-02T17:40:09.307Z
Comments: Hmmm Screen in a container? Could you explain what you are trying to achieve? What issue would you like to solve?
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2018-05-02T18:43:37.507Z
Comments: @030 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. So I'd like to understand why `screen` fails to install in a container, as it's my prefered tool unless there is something similar.
Comments: kenorb
Comments: CC BY-SA 4.0
Comments: 2018-05-02T18:57:38.420Z
Comments: What about setting a maximum amount of memory https://docs.docker.com/config/containers/resource_constraints/#limit-a-containers-access-to-memory
Comments: 030
Tags: <ansible><containers><dockerfile><provisioning>
Title: Why screen folder in /var/run disappears in Docker container after running Ansible playbook?
UserDisplayName: kenorb
ThreadText: # Why screen folder in /var/run disappears in Docker container after running Ansible playbook?
  
  (Asked by: username_0 on 2018-05-01)
  
  ### 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' > /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] => {"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] => {"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
  ...
  
  ```
  
  **Tags:** ansible, containers, dockerfile, provisioning
  
  > **Comments:**
  > **username_1**: Hmmm Screen in a container? Could you explain what you are trying to achieve? What issue would you like to solve?  
  > **username_0**: @username_1 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. So I'd like to understand why `screen` fails to install in a container, as it's my prefered tool unless there is something similar.  
  > **username_1**: What about setting a maximum amount of memory https://docs.docker.com/config/containers/resource_constraints/#limit-a-containers-access-to-memory  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-05-05. Score: 2
  
  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 <container-id> 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](https://www.vagrantup.com/) with [the Docker provider](https://www.vagrantup.com/docs/docker/) to simplify the process of doing development on a Docker container.
  
  
  ---
  
  