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

docker-compose bind mount
docker bind mount
docker bind mount permissions
docker volume vs bind mount
docker-compose volumes
docker-compose volume mount
docker bind mount not working
docker create volume from existing directory

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.

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

Use bind mounts, Docker Compose is smart about recognising which variety is used and whether to use a volume or bind mount. When only a target is specified, without a source, Docker Compose will create an anonymous directory and mount it as a volume to the target path inside the container. Docker Compose allows you to configure volumes by using a short syntax string. Whether you end up with a volume or a bind mount, depends on which short syntax variation you use. When you don’t specify a source, Docker Compose will create an anonymous volume. If source is not a path, Docker Compose will assume source is a named volume.

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/#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:

Docker Compose Syntax: Volume or Bind Mount?, The downside is Docker cannot manage that data, and your container is Bind mounts allow you to modify your Docker image by overriding it with files First, let's define the configuration file we want to inject into the container: Let's look at a stripped-down docker-compose configuration for this service:. You can mount a host path as part of a definition for a single service, and there is no need to define it in the top level volumes key. But, if you want to reuse a volume across multiple services, then define a named volume in the top-level volumes key .

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.

Phase2 I Bending Your Containers with Bind Mounts, Steps to reproduce: 1: make a docker-compose.yml file version: "3.4" services: to host and review code, manage projects, and build software together. for bind mount when source is not provided in compose file #820. Once wired up with a bind mount in the docker-compose.yml, the correct file override flow will work: confd will use the overridden template to generate the config file. If you found the technical concepts in this post helpful, you may want to keep the Outrigger documentation on Working with Volumes and Changing Container Configuration handy as

path to compose file being set as source for bind mount when , hey, trying to mount a single file (instead of a directory) throws an error: million developers working together to host and review code, manage projects, and In attempting to bind mount a file rather than a directory, docker-compose please explain or correct my assumptions, because I'm quite new to Docker ecosystem. Define named volume with host mount in the docker compose file With the docker-compose version 3 and above out and about, one of the things that have been dropped is the volumes_from which was kind useful to share the volumes from one service with another.

bind mount (volumes) not allowed for files – only directorys � Issue , To allow all this data to persist we will use Docker Volumes which are just parts of Docker For example, Ghost container's default port 2368 and default mount point for the then you will have to mention the Docker Volume as a top level definition: Bind mounts are parts of the host file system that can be mounted directly� That is because Docker commands, including configuration in Docker Compose, run in the context of the Docker Host. If you run the commands against a remote server, they run in the context of that remote box. It's attempting to mount the paths as they exist on the remote box, not your local computer. There is no way for Docker itself to mount a

Managing Docker Volumes using Docker Compose – Linux Hint, docker run -it --name bindmount --mount type=bind,source=c:\data some usage help for the available commands for managing volumes. Similar to the bind mount, we can use the --mount flag to specify a volume to mount� This P drive has a folder "data" inside it which I want to bind mount to my container. I am using following lines in my docker-compose file to mount this location. volumes: - type: bind source: \\fs.companyname.ltd\project\data target: /usr/mnt/data This gives me the following error

Comments
  • Check docs.docker.com/compose/compose-file/#/volumes-volumedriver
  • 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: docs.docker.com/compose/overview
  • @nwinkler thanks so much. I'm really sorry for this. Docker is too new with me. Then I read a lot of related document so I mismatch many thing :(
  • No need to be sorry - just trying to make your life a bit easier :-)
  • 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.
  • 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. docs.docker.com/storage
  • Way late, but for anyone else looking at this, it looks like the second volumes is indented too far. It should be a top level tag and inline with the services level (no indent). @AmyDoxy