【问题标题】:Docker Volumes Error. Need help setting it rightDocker 卷错误。需要帮助设置正确
【发布时间】:2021-08-29 08:23:21
【问题描述】:
  1. 我一直在尝试使用来自 docker hub 的 official image 安装 drupal。我在 D 目录中为我的 Drupal 项目创建了一个新文件夹,并创建了一个 docker-compose.yml 文件。
  Drupal with PostgreSQL
 
  Access via "http://localhost:8080"
    (or "http://$(docker-machine ip):8080" if using docker-machine)
 
  During initial Drupal setup,
  Database type: PostgreSQL
  Database name: postgres
  Database username: postgres
  Database password: example
  ADVANCED OPTIONS; Database host: postgres
 
 version: '3.1' services:
 
  drupal:
     image: drupal:8-apache    ports:
       - 8080:80
      volumes:
       - /var/www/html/modules
       - /var/www/html/profiles
       - /var/www/html/themes
         this takes advantage of the feature in Docker that a new anonymous
        volume (which is what we're creating here) will be initialized with the
         existing content of the image at the same location
        - /var/www/html/sites
      restart: always
      postgres:
      image: postgres:10
      environment:
       POSTGRES_PASSWORD: example
     restart: always

当我从包含 docker-compose.yml 文件的文件夹中的终端中运行 docker-compose up -d 命令时,我的 drupal 容器及其数据库已成功安装并运行,并且我能够访问该站点来自 http://localhost:8080 但我在文件夹中找不到他们的核心文件。它只是文件夹中的 docker-compose.yml 文件。

  1. 然后我删除了整个 docker 容器并再次开始全新安装,方法是编辑 docker-compose.yml 文件中的卷部分以指向我希望填充 drupal 核心文件的目录和文件夹。 示例 D:/我的项目/Drupal 项目。
  Drupal with PostgreSQL
 
  Access via "http://localhost:8080"
    (or "http://$(docker-machine ip):8080" if using docker-machine)
 
  During initial Drupal setup,
  Database type: PostgreSQL
  Database name: postgres
  Database username: postgres
  Database password: example
  ADVANCED OPTIONS; Database host: postgres
 
 version: '3.1'
 
 services:
 
   drupal:
     image: drupal:latest
     ports:
       - 8080:80
     volumes:
       - d:\projects\drupalsite/var/www/html/modules
       - d:\projects\drupalsite/var/www/html/profiles
       - d:\projects\drupal/var/www/html/themes
        this takes advantage of the feature in Docker that a new anonymous
        volume (which is what we're creating here) will be initialized with the
        existing content of the image at the same location
       - d:\projects\drupalsite/var/www/html/sites
     restart: always
 
   postgres:
     image: postgres:10
     environment:
       POSTGRES_PASSWORD: example
     restart: always

当我运行 docker-compose.yml 命令时,我收到如下所示的错误。

 Container drupalsite_postgres_1  Created                                                                          3.2s
 - Container drupalsite_drupal_1    Creating                                                                         3.2s
 Error response from daemon: invalid mount config for type "volume": invalid mount path: 'z:/projects/drupalsite/var/www/html/sites' mount path must be absolute
 PS Z:\Projects\drupalsite>

请帮我找到解决办法。

【问题讨论】:

    标签: docker docker-volume


    【解决方案1】:

    如果这些目录包含您的应用程序,它们可能根本不应该在volumes: 中。创建一个名为 Dockerfile 的文件来初始化您的自定义应用程序:

    FROM drupal:8-apache
    COPY modules/ /var/www/html/modules/
    COPY profiles/ /var/www/html/profiles/
    COPY themes/ /var/www/html/themes/
    COPY sites/ /var/www/html/sites/
    # EXPOSE, CMD, etc. come from the base image
    

    然后在您的docker-compose.yml 文件中引用它:

    version: '3.8'
    services:
      drupal:
        build: .  # instead of image:
        ports:
          - 8080:80
        restart: always
        # no volumes:
      postgres:
        image: postgres:10
        environment:
          POSTGRES_PASSWORD: example
        restart: always
        volumes:
          - pgdata:/var/lib/postgresql/data
    volumes:
      pgdata:
    

    如果你真的想在这里使用volumes:,该指令有三种形式。您在问题中只有一个路径的表单会创建一个匿名卷:它会导致 Compose 保留该目录,从图像中的内容初始化,但与您的主机系统断开连接。使用裸名称和路径,它会创建一个命名卷,它与此类似,但可以显式管理。通过两个路径,它创建了一个绑定挂载,它无条件地将容器内容替换为主机系统内容(没有初始化)。

    version: '3.8'
    services:
      something:
        volumes:
          - /path1             # anonymous volume
          - named:/path2       # named volume
          - /host/path:/path3  # bind mount
    volumes:                   # named volumes referenced in containers only
      named:                   # usually do not need any settings
    

    因此,如果您确实想用主机目录替换图像的内容,则需要使用绑定挂载语法。此处的相对路径是相对于docker-compose.yml 文件的位置进行解释的。

    version: '3.8'
    services:
      drupal:
        image: drupal:8-apache
        volumes:
          - ./modules:/var/www/html/modules
          # etc.
    

    关于命名卷初始化的最后一条评论:您的文件有一条关于初始化匿名卷的评论。但是,这种方法存在两个主要问题。首先,在您启动容器的时间,卷的内容优先,并且底层图像中的任何更改都将被忽略。其次,此设置仅适用于 Docker 命名卷和匿名卷,但不适用于 Docker 绑定挂载、Kubernetes 中的卷挂载或其他类型的挂载。我通常会避免依赖这个“功能”。

    【讨论】:

    • 基本上当我运行 docker-compose up -d 文件时。图像和容器开始运行,但它将与该特定图像有关的所有数据、代码和文件存储在我的 Windows 系统上的某个位置,或者可能将其保存在它自己的虚拟容器中。我担心这些文件和代码位于我的本地目录中,可以通过这些文件和代码进行任何更改。假设 Drupal 图像有一个 index.html 文件需要编辑。所以我需要访问那个 html 文件。
    • 一旦构建 Docker 镜像,您将无法对其内容进行编辑。理论上,您可以在容器中编辑文件,但一旦容器退出,这些更改将丢失。我通常使用的方法是在普通的非 Docker 环境中进行日常开发,然后 docker build 一个镜像(就像这里的第一个设置,没有卷)进行集成测试和部署。
    • 感谢大卫的评论,有没有关于这个主题的教程我可以学习?为像我这样的初学者找到合适的教程真的很沮丧。非常感谢您的帮助。
    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    相关资源
    最近更新 更多