Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose: define mount for bind mount and managed mount

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.

like image 835
Trần Kim Dự Avatar asked Dec 23 '16 10:12

Trần Kim Dự


People also ask

What are the two types of mounts in Docker?

Basically, there are 3 types of mounts which you can use in your Docker container viz. Volumes, Bind mount and tmpfs mounts.

How do Docker bind mounts work?

Bind mounts will mount a file or directory on to your container from your host machine, which you can then reference via its absolute path. To use bind mounts, the file or directory does not need to exist on your Docker host already. If it doesn't exist, it will be created on demand.

What is Docker Mountpoint?

It lists details of a volume, including its location on the host file (Mountpoint). Everything stored within the data volume can also be found in the directory listed under the mountpoint path.

What is a bind Mount in Docker?

Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.

What is a mount option in Docker Compose?

MODE is a mount option which can be read-only or read-write. Brackets mean the argument is optional. This optionality leads to three unique variations you can use to configure a container's volumes. Docker Compose is smart about recognising which variety is used and whether to use a volume or bind mount.

What is the difference between-V and--Mount in Docker?

If you are developing new Docker applications, consider using named volumes instead. You can’t use Docker CLI commands to directly manage bind mounts. In general, --mount is more explicit and verbose. The biggest difference is that the -v syntax combines all the options together in one field, while the --mount syntax separates them.

What is the difference between a volume and a bind Mount?

Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.


4 Answers

Although I am answering very late. But maybe it helps other people in future. 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:
like image 160
Raman Sharma Avatar answered Oct 17 '22 09:10

Raman Sharma


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
like image 26
nwinkler Avatar answered Oct 17 '22 08:10

nwinkler


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.

like image 7
Aramis NSR Avatar answered Oct 17 '22 07:10

Aramis NSR


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

like image 2
glimmbo Avatar answered Oct 17 '22 07:10

glimmbo