在dockerfile中创建的卷未安装在容器中

时间:2019-01-21 08:51:53

标签: docker docker-compose dockerfile

我想将Firefox锁定在Docker容器中。为此,我创建了以下dockerfile文件:

FROM debian:sid

RUN apt-get update && apt-get install -y \
    firefox \
&& rm -rf /var/lib/apt/lists/*

RUN useradd \
      --create-home \
      --home-dir /home/morfitest/ \
      --shell /bin/bash \
      --uid 1000 \
      --user-group \
      morfitest

USER morfitest

ENV HOME /home/morfitest

RUN mkdir /home/morfitest/.mozilla
VOLUME ["/home/morfitest/.mozilla"]

WORKDIR /home/morfitest/

CMD /usr/bin/firefox

我还有一个docker-compose.yml文件,看起来像这样:

version: '3.6'

services:
   browser:
     build:
       context: .
       dockerfile: dockerfile
     image: firefox:morfitest
     container_name: firefox
     hostname: firefox
     domainname: local
     restart: "no"
     volumes:
#       - user_profile:/home/morfitest/.mozilla/
       - /tmp/.X11-unix:/tmp/.X11-unix
     environment:
       - DISPLAY=$DISPLAY
     logging:
       driver: syslog
       options:
         syslog-address: "tcp://192.168.43.247:514"
         tag: "firefox-morfitest"
     networks:
       default:
         ipv4_address: 10.10.2.10

#volumes:
#    user_profile:

networks:
  default:
    name: firefox
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.10.2.0/24

我已经读到,当您想以非root用户身份启动容器时,不能在volumes:文件中使用docker-compose.yml,因为它无法处理使用bind安装卷的正确方法。因此,必须使用以下命令在dockerfile文件中指定卷:

USER morfitest
RUN mkdir /home/morfitest/.mozilla
VOLUME ["/home/morfitest/.mozilla"]

容器可以正常构建和运行,但是我无法弄清楚为什么容器启动/启动时无法安装该卷。

当我检查容器时,可以看到以下内容:

$ docker container inspect firefox | grep vol
                "Type": "volume",
                "Source": "/media/Zami/docker/volumes/f2a4ee3dd34e2711b262f104e6b1aea6c07d107c38ec837c2baf11a39227c754/_data",


# ls -al /media/Zami/docker/volumes/f2a4ee3dd34e2711b262f104e6b1aea6c07d107c38ec837c2baf11a39227c754/_data
total 20
drwxr-xr-x 5 morfik morfik 4096 2019-01-21 09:30:57 ./
drwxr-xr-x 3 root   root   4096 2019-01-21 09:30:55 ../
drwx------ 2 morfik morfik 4096 2019-01-21 09:30:57 extensions/
drwx------ 5 morfik morfik 4096 2019-01-21 09:30:57 firefox/
drwx------ 2 morfik morfik 4096 2019-01-21 09:30:57 systemextensionsdev/

因此该卷存在并且具有一些内容,但是不起作用。当我在CMD文件中使用以下dockerfile时:

CMD  ls -al /home/morfitest/ &&  ls -al /home/morfitest/.mozilla

要真正查看容器启动/启动时卷的内容是否可见,我只能看到以下内容:

firefox-morfitest[61138]: total 24
firefox-morfitest[61138]: drwxr-xr-x 1 morfitest morfitest 4096 Jan 21 08:05 .
firefox-morfitest[61138]: drwxr-xr-x 1 root      root      4096 Jan 21 08:05 ..
firefox-morfitest[61138]: -rw-r--r-- 1 morfitest morfitest  220 Jun 17  2018 .bash_logout
firefox-morfitest[61138]: -rw-r--r-- 1 morfitest morfitest 3526 Jun 17  2018 .bashrc
firefox-morfitest[61138]: drwxr-xr-x 2 morfitest morfitest 4096 Jan 21 08:05 .mozilla
firefox-morfitest[61138]: -rw-r--r-- 1 morfitest morfitest  807 Jun 17  2018 .profile

firefox-morfitest[61138]: total 8
firefox-morfitest[61138]: drwxr-xr-x 2 morfitest morfitest 4096 Jan 21 08:05 .
firefox-morfitest[61138]: drwxr-xr-x 1 morfitest morfitest 4096 Jan 21 08:05 ..

目录.mozilla具有正确的权限,但为空。那么如何挂载该卷呢?

1 个答案:

答案 0 :(得分:0)

基本上配置文件都可以,问题出在注释掉的行中。实际上,散列应该被删除并且设置工作正常。

但是,当我一开始尝试这样做时,该音量被忽略了-由于某些视觉错误,以下警告的某些部分未显示:

WARNING: Service "browser" is using volume "/home/morfitest/.mozilla" from the
previous container. Host mapping "docker-firefox_user_profile" has no effect.
Remove the existing containers (with docker-compose rm browser)

我根据需要删除了该容器,现在,它创建了一个具有永久名称的卷:docker-firefox_user_profile而不是哈希。现在,当我执行down / up时,它将使用该卷而不是创建一个新卷。这解决了我的问题。

相关问题