How do I mount a host directory as a volume in docker compose
Checkout their documentation
From the looks of it you could do the following on your docker-compose.yml
volumes: - ./:/app
Where ./
is the host directory, and /app
is the target directory for the containers.
EDIT:
Previous documentation source now leads to version history, you'll have to select the version of compose you're using and look for the reference.
Side note: Syntax remains the same for all versions as of this edit
There are a few options
Short Syntax
Using the host : guest
format you can do any of the following:
volumes: # Just specify a path and let the Engine create a volume - /var/lib/mysql # Specify an absolute path mapping - /opt/data:/var/lib/mysql # Path on the host, relative to the Compose file - ./cache:/tmp/cache # User-relative path - ~/configs:/etc/configs/:ro # Named volume - datavolume:/var/lib/mysql
Long Syntax
As of docker-compose v3.2 you can use long syntax which allows the configuration of additional fields that can be expressed in the short form such as mount type
(volume, bind or tmpfs) and read_only
.
version: "3.2"services: web: image: nginx:alpine ports: - "80:80" volumes: - type: volume source: mydata target: /data volume: nocopy: true - type: bind source: ./static target: /opt/app/staticnetworks: webnet:volumes: mydata:
Check out https://docs.docker.com/compose/compose-file/#long-syntax-3 for more info.
If you would like to mount a particular host directory (/disk1/prometheus-data
in the following example) as a volume in the volumes
section of the Docker Compose YAML file, you can do it as below, e.g.:
version: '3'services: prometheus: image: prom/prometheus volumes: - prometheus-data:/prometheusvolumes: prometheus-data: driver: local driver_opts: o: bind type: none device: /disk1/prometheus-data
By the way, in prometheus's Dockerfile, You may find the VOLUME
instruction as below, which marks it as holding externally mounted volumes from native host, etc. (Note however: this instruction is not a must though to mount a volume into a container.):
Dockerfile
...VOLUME ["/prometheus"]...
Refs: