首页 > 解决方案 > 在 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具有正确的权限,但它是空的。那么如何挂载卷呢?

标签: dockerdocker-composedockerfile

解决方案


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

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

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它使用卷而不是创建一个新卷。这解决了我的问题。


推荐阅读