我在撰写文件中有 2 个容器,我想通过 nginx 提供应用程序静态文件。
我读过这个:https://stackoverflow.com/a/43560093/7522096并想使用主机目录在应用程序容器和 nginx 容器之间共享,出于某种原因我不想使用命名卷。
===
Using a host directory Alternately you can use a directory on the host and mount that into the containers. This has the advantage of you being able to work directly on the files using your tools outside of Docker (such as your GUI text editor and other tools).
It's the same, except you don't define a volume in Docker, instead mounting the external directory.
version: '3'
services:
nginx:
volumes:
- ./assets:/var/lib/assets
asset:
volumes:
- ./assets:/var/lib/assets
===
我的 docker 撰写文件:
version: "3.7"
services:
app:
container_name: app
restart: always
ports:
- 8888:8888
env_file:
- ./env/app.env
image: registry.gitlab.com/app/development
volumes:
- ./public/app/:/usr/app/static/
- app-log:/root/.pm2
nginx:
container_name: nginx
image: 'nginx:1.16-alpine'
restart: always
ports:
- 80:80
- 443:443
volumes:
- /home/devops/config/:/etc/nginx/conf.d/:ro
- /home/devops/ssl:/etc/nginx/ssl:ro
- ./public/app/:/etc/nginx/public/app/
depends_on:
- app
volumes:
# app-public:
app-log:
然而,当我在撰写中执行此操作时,nginx 上的目录总是空的,我的应用程序容器中的静态文件也消失了。
请帮助,我尝试了很多方法,但无法弄清楚。
谢谢。
最佳答案
在容器初始化期间,docker 将绑定(bind) ./public/app
主机上带有 /usr/app/static/
的目录容器中的目录。
如果 ./public/app
不存在它将被创建。绑定(bind)是从主机到容器,意思是./public/app
的内容文件夹是
反射(reflect)(复制)到容器中,反之亦然。这就是初始化后静态应用程序目录为空的原因。
如果我的理解是正确的,您的目标是在应用程序容器和 nginx
之间共享应用程序文件。 .
考虑到上述情况,唯一的解决方案是在创建卷之后在卷中创建文件。以下是相关部分的示例:
version: "3"
services:
app:
image: ubuntu
volumes:
- ./public/app/:/usr/app/static_copy/
entrypoint: /bin/bash
command: >
-c "mkdir /usr/app/static;
touch /usr/app/static/shared_file;
mv /usr/app/static/* /usr/app/static_copy;
rm -r /usr/app/static;
ln -sfT /usr/app/static_copy/ /usr/app/static;
exec sleep infinity"
nginx:
image: 'nginx:1.16-alpine'
volumes:
- ./public/app/:/etc/nginx/public/app/
depends_on:
- app
这会将静态文件移动到
static_copy
目录并将这些文件链接回 /usr/app/static
.这些文件将与主机共享(public/app
导演)和
nginx
容器(/etc/nginx/public/app/
)。调整它以满足您的需求。或者,您当然可以使用命名卷。
希望能帮助到你
关于docker - 使用 docker-compose 在主机和多个容器之间共享目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57706872/