简体   繁体   中英

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

I'm wondering how you can mount a host directory using docker-compose.

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

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

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"

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.

EDIT BASED ON COMMENT:

Create a .env file with the contents:

HOST_PATH=~/path/on/host

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM