![](/img/trans.png)
[英]How to mount Docker directory into host directory with docker-compose
[英]How to mount a host directory with docker-compose, with the "~/path/on/host" to be specified when running the host, not in the docker-compose file
我想知道如何使用 docker-compose 掛載主機目錄。
目前我只是使用一個普通的 Dockerfile,它工作正常。 當我運行容器時,我只指定主機上的路徑和容器上的路徑。
docker run -d -p 3000:3000 -v ~/path/on/host:/path/on/container my-container
我想使用 docker-compose 來實現這一點,但我不確定這是如何工作的。我的 docker compose 如下。
version: '3'
services:
my-app:
build:
context: .
dockerfile: ./path/to/Dockerfile
另外,我需要在運行主機時指定 ~/path/on/host ,而不是在 docker-compose 文件中。
version: '3'
services:
my-app:
build:
context: .
dockerfile: ./path/to/Dockerfile
volumes:
- ~/path/on/host:/path/on/container
ports:
- "3000:3000"
然后您可以使用docker-compose up -d
啟動您的服務。 確保首先停止並刪除您使用 docker 命令啟動的容器,否則會發生沖突。
根據評論編輯:
創建一個包含以下內容的 .env 文件:
HOST_PATH=~/path/on/host
並更改您的 docker-compose.yml:
version: '3'
services:
my-app:
build:
context: .
dockerfile: ./path/to/Dockerfile
volumes:
- ${HOST_PATH}:/path/on/container
ports:
- "3000:3000"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.