56

I'm using docker-compose for defining my service. In docker, there are two concepts for docker volume. Firstly is about bind mount: mount on host storage.

docker run -d --name web-app -v $HOST/location:/container/location -p 80:80 httpd:latest

Secondly is about managed mount: abstract storage, not depend on host.

docker run -d --name web-app -v /container/location -p 80:80 httpd:latest

I want to map those concepts to docker-compose. It means how can I define bind mount and managed mount when using docker-compose.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107
  • 2
    Check https://docs.docker.com/compose/compose-file/#/volumes-volumedriver – kTT Dec 23 '16 at 10:45
  • 2
    No offense, but you ask a lot of fundamental questions about how to use Docker Compose. I suggest you start going through the Docker Compose overview, it explains all of the basic concepts you're asking about: https://docs.docker.com/compose/overview/ – nwinkler Dec 23 '16 at 10:46

4 Answers4

111

Below is the configuration for both types. https://docs.docker.com/compose/compose-file/compose-file-v3/#volumes

version: "3.2"
services:
  web:
    image: httpd:latest
    volumes:
      - type: bind
        source: $HOST/location
        target: /container/location
      - type: volume
        source: mydata
        target: /container/location
volumes:
  mydata:
elbowlobstercowstand
  • 3,812
  • 1
  • 25
  • 22
Raman Sharma
  • 1,940
  • 2
  • 11
  • 10
  • 1
    The documentation link did not work for me, bout found the information here: https://github.com/compose-spec/compose-spec/blob/master/spec.md#volumes – Adarsha Jan 21 '21 at 05:09
  • Can you not specify a specific source location for volumes? – NoName Jun 02 '21 at 01:13
  • 1
    @NoName : For bind mount yes. For volume, you can't specify specific source location. It's fully managed by docker. – KMC Jul 01 '22 at 21:26
45

You can find these Docker concepts in the volumes section of Docker Compose: https://docs.docker.com/compose/compose-file/#/volumes-volumedriver

Examples:

volumes:
  # Just specify a path and let the Engine create a volume
  - /container/location

  # Specify an absolute path mapping
  - ./myfolder/location:/container/location
nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • 8
    do you know what syntax would work for the `volumes:` tag in the root of the `.yml` file? Your solution will only work for the `volumes:` tag nested under a `services:` tag. – kapad Nov 03 '17 at 14:58
  • 1
    As far as I understand only named volumes are supposed to be listed in the top level volumes key. "named volumes" are a special method of persisting data for a container, in contrast to bind-mounts or tmpfs mounts. https://docs.docker.com/storage/ – FloE Mar 18 '18 at 10:18
8

I know it's late to answer but I'm mostly writing this for the community.

Answer:

You only need to do it like this:

    volumes:
       - ./root/instantclient_12_2/ojdbc8.jar:/etc/kafka-connect/jars/ojdbc8.jar
       - type: bind
         source: $HOST/etc
         target: /kernel-etc

Then, for running it in 'easy to debug mode' do it first with docker-compose up and when you made sure it was working fine put a ring on it by adding a -d at the end.

Important Notes

1-Make sure your docker-compose version is at least 3.2 and in my case, it is3.7. for updating and getting rid of the old version do as below:

sudo apt-get remove docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Source: https://github.com/10up/wp-local-docker/issues/58#issuecomment-476786006

2- Don't forget to rm your old container so you don't face weird port-related issues. heres how:

docker container stop YOUR_CONTAINER_ID
docker container rm YOUR_CONTAINER_ID

Good luck.

Aramis NSR
  • 1,602
  • 16
  • 26
  • Confused a bit: you mean docker compose version for `yaml` file and not `docker-compose` itself. As of now latest docker compose is 2.11.2. And you used 1.23.2 – Eugen Konkov Oct 09 '22 at 11:30
  • @EugenKonkov No problem my friend, here's the link to versioning i meant. https://docs.docker.com/compose/compose-file/compose-versioning/ – Aramis NSR Oct 12 '22 at 10:31
2

As of Docker 3 on OSX, I had to disable Experimental Feature gRPC and restart as per: https://github.com/microsoft/vscode-remote-release/issues/4171

glimmbo
  • 143
  • 10
  • @dippas That's what the issue says to do "disable Experimental Feature gRPC" There's not a lot more context – Scratte Dec 11 '20 at 19:40