如何在卷中挂载单个文件 [英] How to mount a single file in a volume
问题描述
我正在尝试 dockerize 一个 PHP 应用程序.在 dockerfile 中,我下载存档、解压缩等.
I am trying to dockerize a PHP application. In the dockerfile, I download the archive, extract it, etc.
一切正常.但是,如果发布了新版本并且我更新了 dockerfile,我必须重新安装该应用程序,因为 config.php 被覆盖了.
Everything works fine. However, if a new version gets released and I update the dockerfile, I have to reinstall the application, because the config.php gets overwritten.
所以我想我可以将文件挂载为一个卷,就像我对数据库所做的一样.
So I thought I can mount the file as a volume, like I do with the database.
我尝试了两种方法,一种是音量,一种是直接路径.
I tried it two ways, with a volume and a direct path.
docker-compose:
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:
导致错误的原因:
我用给定的路径尝试了它,作为一个已安装的卷.
And I tried it with a given path, as a mounted volume.
/src/docker/myapp/upload:/var/www/html/upload
/src/docker/myapp/upload:/var/www/html/config.php
然而,这两种方式都行不通.使用安装的卷,我看到上传已创建.
However, both ways are not working. With the mounted volume, I see that upload gets created.
但是它失败了:
/var/www/html/config.php"导致不是目录""
/var/www/html/config.php" caused "not a directory"""
如果我尝试使用
/src/docker/myapp/upload/config.php:/var/www/html/config.php
Docker 创建上传文件夹,然后创建一个 config.php 文件夹.不是文件.
Docker creates the upload folder and then a config.php folder. Not a file.
或者有其他方法可以持久化配置吗?
Or is there another way to persist the config?
推荐答案
TL;DR/通知:
如果您遇到一个目录被创建来代替您尝试挂载的文件,您可能没有提供有效和绝对路径.这是无声和混乱的失败模式的常见错误.
TL;DR/Notice:
If you experience a directory being created in place of the file you are trying to mount, you have probably failed to supply a valid and absolute path. This is a common mistake with a silent and confusing failure mode.
文件卷在docker中是这样做的(绝对路径示例(可以使用env变量),需要提文件名):
File volumes are done this way in docker (absolute path example (can use env variables), and you need to mention the file name) :
volumes:
- /src/docker/myapp/upload:/var/www/html/upload
- /src/docker/myapp/upload/config.php:/var/www/html/config.php
你也可以这样做:
volumes:
- ${PWD}/upload:/var/www/html/upload
- ${PWD}/upload/config.php:/var/www/html/config.php
如果你从 /src/docker/myapp
文件夹启动 docker-compose
If you fire the docker-compose from /src/docker/myapp
folder
这篇关于如何在卷中挂载单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!