如何使用docker-compose和“〜/path/on/host"挂载主机目录在运行主机时指定,而不是在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挂载主机目录.
I'm wondering how you can mount a host directory using docker-compose.
此刻,我仅使用常规的Dockerfile,并且工作正常.运行容器时,只需在主机上指定路径,再在容器上指定路径.
At the moment I'm just using a regular Dockerfile and it's working fine. When I run the container I just specify the path on my host and the path on the container.
docker run -d -p 3000:3000 -v ~/path/on/host:/path/on/container my-container
我想使用docker-compose来实现这一点,但是我不确定它是如何工作的.我的docker compose在下面.
I'd like to to achieve this using docker-compose but I'm not sure how this works.My docker compose is below.
version: '3'
services:
my-app:
build:
context: .
dockerfile: ./path/to/Dockerfile
此外,我需要在运行主机时指定〜/path/on/host,而不是在docker-compose文件中.
In addition, I need the ~/path/on/host to be specified when running the host, not in the docker-compose file.
推荐答案
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命令启动的容器,否则会发生冲突.
Then you can start your service with docker-compose up -d
. Make sure to stop and remove first the container you started using docker commands otherwise you will get conflicts.
基于评论的
创建一个包含以下内容的.env文件:
Create a .env file with the contents:
HOST_PATH=~/path/on/host
并更改您的docker-compose.yml:
and change your docker-compose.yml:
version: '3'
services:
my-app:
build:
context: .
dockerfile: ./path/to/Dockerfile
volumes:
- ${HOST_PATH}:/path/on/container
ports:
- "3000:3000"
这篇关于如何使用docker-compose和“〜/path/on/host"挂载主机目录在运行主机时指定,而不是在docker-compose文件中指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!