如何在卷中装载单个文件 [英] How to mount a single file in a volume
问题描述
我正在尝试将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.
但随后失败,并显示以下内容:
But then it fails with:
/ var / www / html / config.php\导致not不是目录\
/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
您也可以执行以下操作:
You can also do:
volumes:
- ${PWD}/upload:/var/www/html/upload
- ${PWD}/upload/config.php:/var/www/html/config.php
如果您从 / src / docker / myapp触发docker-compose
文件夹
这篇关于如何在卷中装载单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!