一千萬個為什麽

搜索

運行Ansible playbook後,為什麽/ var/run中的屏幕文件夾在Docker容器中消失了?



我有以下 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"]

以及 install_screen.yml 劇本文件:

- 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/

建立

Now, I 建立 the Docker container in a shell by:

$ docker 建立 -t screen .

並且配置屏幕任務顯示以下輸出:

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": []}

然後我的 ls -la/var/run/ shell命令顯示:

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"]}

問題

It seems after installing screen and running /etc/init.d/screen-cleanup start during provisioning, the container have the 問題 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

在上面的Ansible輸出中,我可以看到/var/run/screen 文件夾按照以下行存在(在 ls -la/var/run/ run期間):

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

但是在Ansible playbook命令完成後它就消失了。

解決方法

The following one-time 解決方法 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
...

轉載註明原文: 運行Ansible playbook後,為什麽/ var/run中的屏幕文件夾在Docker容器中消失了?

一共有 1 個回答:

我沒有解決您遇到的直接問題,而是建議您解決實際問題:

我需要屏幕,所以我可以在Docker的一個實例中使用多個交互式shell。如果我運行多個Docker實例,每個實例需要1G的RAM,那麽10個以上的實例會占用我的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 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.

您可能還希望使用 Vagrant 。 com/docs/docker /“rel =”nofollow noreferrer“> Docker提供程序,以簡化在Docker容器上進行開發的過程。