使用docker-compose时,Docker容器丢失文件

xu3bshqb  于 2023-03-29  发布在  Docker
关注(0)|答案(1)|浏览(414)

我想用两个容器创建docker-compose设置。

  1. MariaDB容器
    1.一个基于bitnami/moodle的Moodle容器:latest
    这些容器应该用于测试Moodle WebServices的自定义PHP插件。
    我在Dockerfile中做了这个
FROM bitnami/moodle:latest

COPY ./local_custom_api/ /bitnami/moodle/local/local_custom_api/

然后我创建了下面的docker-compose.yml

version: '3.8'
services:
  mariadb:
    image: mariadb
    volumes:
      - D:\\Docker\\Moodle\\Database:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=moodle
      - MYSQL_ROOT_USER=root
      - MYSQL_DATABASE=moodle

  moodle:
    build: .
    ports:
      - 8080:8080
      - 8443:8443
    environment:
      - MOODLE_DATABASE_HOST=mariadb
      - MOODLE_DATABASE_USER=root
      - MOODLE_DATABASE_PASSWORD=moodle
      - MOODLE_DATABASE_NAME=moodle
    volumes:
      - D:\\Docker\\Moodle\\Moodle:/bitnami/moodle
      - D:\\Docker\\Moodle\\MoodleData:/bitnami/moodledata
    depends_on:
      - mariadb
    links:
      - mariadb:mariadb

这很好地工作了,两个容器都被创建了,moodle安装脚本成功完成,但是Dockerfile中的copy cmd不起作用。文件没有显示在指定的卷中。
经过一番研究,似乎我必须使用命名卷来使copy cmd工作(不知道为什么),所以我创建了命名卷,但现在moodle安装过程找不到文件,尽管我只是更改了卷的主机路径
新的docker-compose.yml

version: '3.8'
services:
  mariadb:
    image: mariadb
    volumes:
      - moodle_db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=moodle
      - MYSQL_ROOT_USER=root
      - MYSQL_DATABASE=moodle
  moodle:
    build: .
    ports:
      - 8080:8080
      - 8443:8443
    environment:
      - MOODLE_DATABASE_HOST=mariadb
      - MOODLE_DATABASE_USER=root
      - MOODLE_DATABASE_PASSWORD=moodle
      - MOODLE_DATABASE_NAME=moodle
    volumes:
      - moodle_app:/bitnami/moodle
      - moodle_data:/bitnami/moodledata
    depends_on:
      - mariadb
    links:
      - mariadb:mariadb

volumes:
  moodle_db:
    driver: local
    driver_opts:
      type: none
      device: "D:\\Docker\\Moodle\\Database"
      o: bind
    name: moodle_db
  moodle_app:
    driver: local
    driver_opts:
      type: none
      device: "D:\\Docker\\Moodle\\Moodle"
      o: bind
    name: moodle_app
  moodle_data:
    driver: local
    driver_opts:
      type: none
      device: "D:\\Docker\\Moodle\\MoodleData"
      o: bind
    name: moodle_data

这是输出。安装总是崩溃,moodle_app卷中唯一的文件是成功复制的插件文件:

moodle-test-moodle-1   | moodle 16:04:14.05 
moodle-test-moodle-1   | moodle 16:04:14.05 Welcome to the Bitnami moodle container
moodle-test-moodle-1   | moodle 16:04:14.05 Subscribe to project updates by watching https://github.com/bitnami/containers
moodle-test-moodle-1   | moodle 16:04:14.05 Submit issues and feature requests at https://github.com/bitnami/containers/issues
moodle-test-moodle-1   | moodle 16:04:14.05 
moodle-test-moodle-1   | moodle 16:04:14.05 INFO  ==> ** Starting Moodle setup **
moodle-test-moodle-1   | realpath: /bitnami/apache/conf: No such file or directory
moodle-test-moodle-1   | moodle 16:04:14.08 INFO  ==> Configuring Apache ServerTokens directive
moodle-test-moodle-1   | moodle 16:04:14.10 INFO  ==> Configuring PHP options
moodle-test-moodle-1   | moodle 16:04:14.10 INFO  ==> Setting PHP expose_php option
moodle-test-moodle-1   | moodle 16:04:14.12 INFO  ==> Validating settings in MYSQL_CLIENT_* env vars
moodle-test-moodle-1   | moodle 16:04:14.13 INFO  ==> Validating settings in POSTGRESQL_CLIENT_* env vars
moodle-test-moodle-1   | moodle 16:04:14.19 INFO  ==> Restoring persisted Moodle installation
moodle-test-moodle-1   | moodle 16:04:16.25 INFO  ==> Trying to connect to the database server
moodle-test-moodle-1   | grep: /opt/bitnami/moodle/config.php: No such file or directory
moodle-test-moodle-1 exited with code 2

主机位置如何影响容器的内部功能?

kcrjzv8t

kcrjzv8t1#

问题是,在docker compose中,您正在绑定挂载到docker文件中复制的目录的父目录。docker docs说:如果一个容器目录是非空的,它会隐藏在一个绑定挂载目录的内容后面(也就是说,你不能通过这种方式用一个容器文件初始化主机目录)。因此,你在/bitnami/moodle/local/local_custom_api/(在容器中)的副本是隐藏的,而不是目录与D:\Docker\Moodle\Moodle相同

# ...
volumes:
  - D:\\Docker\\Moodle\\Moodle:/bitnami/moodle # -> everything in the `/bitnami/moodle` is gone
# ...

...同样在第二个docker-compose.yml
解决办法其实很简单:把你的插件放在D:\Docker\Moodle\Moodle\local\local_custom_api中。另外,你可以不定义docker文件,而只使用docker-compose.yml中的默认镜像:

version: '3.8'
services:
# db...
  moodle:
    image: bitnami/moodle:latest
    ports:
      - 8080:8080
      - 8443:8443
    environment:
      - MOODLE_DATABASE_HOST=mariadb
      - MOODLE_DATABASE_USER=root
      - MOODLE_DATABASE_PASSWORD=moodle
      - MOODLE_DATABASE_NAME=moodle
    volumes:
      - D:\\Docker\\Moodle\\Moodle:/bitnami/moodle
      - D:\\Docker\\Moodle\\MoodleData:/bitnami/moodledata
    depends_on:
      - mariadb
    links:
      - mariadb:mariadb

Docker文件中的复制在原型开发过程中还有另一个缺点,它只是一个副本,每次你改变任何东西,你都必须重建容器。另一方面,卷是“同步”的,它们是从外部和内部可见的同一目录。所以当你有新版本的插件时,你只需重新启动容器。
使用(命名)卷不是必须的,请参阅documentation以了解更多信息绑定挂载的区别(=使用路径)。

相关问题