我正在尝试dockerize一个PHP应用程序。在dockerfile中,我下载存档,提取它,等等。

一切都很好。但是,如果发布了一个新版本,并且我更新了dockerfile,我必须重新安装应用程序,因为config.php会被覆盖。

因此,我认为我可以将该文件作为卷挂载,就像对数据库所做的那样。

我尝试了两种方法,用音量和直接路径。

docker-compose:

version: '2'
services:
  app:
    build: src
    ports:
      - "8080:80"
    depends_on:
      - mysql
    volumes:
      -  app-conf:/var/www/html/upload
      -  app-conf:/var/www/html/config.php
    environment:
      DB_TYPE: mysql
      DB_MANAGER: MysqlManager

  mysql:
    image: mysql:5.6
    container_name: mysql
    volumes:
      - mysqldata:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD:
      MYSQL_DATABASE:
      MYSQL_USER:
      MYSQL_PASSWORD:

volumes:
  mysqldata:
  app-conf:

这导致了错误:

我尝试用一个给定的路径,作为一个挂载的卷。

/src/docker/myapp/upload:/var/www/html/upload
/src/docker/myapp/upload:/var/www/html/config.php

然而,这两种方法都不起作用。对于挂载的卷,我看到创建了上传。

但它失败了:

/var/www/html/config.php\"造成\"不是目录\""

如果我用

/src/docker/myapp/upload/config.php:/var/www/html/config.php

Docker创建upload文件夹,然后创建config.php文件夹。不是文件。

或者是否有另一种方法来持久化配置?


当前回答

对于像我这样使用Windows容器的人来说,要知道不能使用Windows容器绑定或挂载单个文件。

The following examples will fail when using Windows-based containers, as the destination of a volume or bind mount inside the container must be one of: a non-existing or empty directory; or a drive other than C:. Further, the source of a bind mount must be a local directory, not a file. net use z: \\remotemachine\share docker run -v z:\foo:c:\dest ... docker run -v \\uncpath\to\directory:c:\dest ... docker run -v c:\foo\somefile.txt:c:\dest ... docker run -v c:\foo:c: ... docker run -v c:\foo:c:\existing-directory-with-contents ...

很难发现,但它就在那里

链接到关于将文件映射到Windows容器的Github问题

其他回答

对于像我这样使用Windows容器的人来说,要知道不能使用Windows容器绑定或挂载单个文件。

The following examples will fail when using Windows-based containers, as the destination of a volume or bind mount inside the container must be one of: a non-existing or empty directory; or a drive other than C:. Further, the source of a bind mount must be a local directory, not a file. net use z: \\remotemachine\share docker run -v z:\foo:c:\dest ... docker run -v \\uncpath\to\directory:c:\dest ... docker run -v c:\foo\somefile.txt:c:\dest ... docker run -v c:\foo:c: ... docker run -v c:\foo:c:\existing-directory-with-contents ...

很难发现,但它就在那里

链接到关于将文件映射到Windows容器的Github问题

如果您忘记在源目录中实际包含想要挂载的文件,似乎也会发生这种情况。我经历了惨痛的教训。没有诸如“文件不存在”之类的错误消息,而是在目标中创建一个目录。

你也可以在docker-compose中使用相对路径。yml文件(在Windows主机,Linux容器上测试):

volumes:
    - ./test.conf:/fluentd/etc/test.conf

也许这能帮到别人。

我遇到了这个问题,试过了所有方法。卷绑定看起来很好,即使我挂载了目录(而不是文件),我在挂载目录中的文件名是正确的,但挂载为dirs。

我尝试重新启用共享驱动器,Docker抱怨防火墙是活动的。

在禁用防火墙后,一切工作正常。

以上答案均正确。

但是有一件事我发现非常有帮助的是,挂载文件应该提前存在docker主机内,否则docker将创建一个目录。

例如:

/a/file/inside/host/hostFile.txt:/a/file/inside/container/containerFile.txt

hostFile.txt应该提前存在。 否则你会收到这个错误:containerFile.txt是一个目录