Docker Compose Relative paths vs Docker volume
Asked Answered
R

2

49

I have a docker compose file for a website, which amongst a bunch of other containers for various purposes, includes a mysql database that will have persistent data. At the moment the compose file specifies a relative path for the data, e.g.:

 
mysql: 
  image: mysql:5.7
  container_name: sqldb
  volumes:
   - ./mysql_data/_data:/var/lib/mysql

and the folder structure:

 --mysql_data
 --static_content
 docker-compose.yml

which means that at any point I can move the whole site (including persisted content) to another server by copying the whole folder and running docker-compose up.

But reading about docker volumes it sounds like it is the preferred method (plus relative bind mount paths don't seem to be supported using "docker run", but work in compose) so I'm wondering if I need to change this approach to use volumes? Is there something inherently wrong with this relative binding approach? If I do switch to volumes, when moving the containers do I have to manually move the volumes (e.g. this method How to port data-only volumes from one host to another?)?

Randa answered 24/10, 2017 at 9:59 Comment(1)
This approch works fine for compose case but not in swarm mode. You can also give named volumes with customer path as your folder path to work with swarm mode. No need to worry much about run now.Assegai
A
78

Persistence of data in Docker

There are four possible options to mount any volume:

  1. Relative Path
  2. Absolute Path
  3. Docker Volume Default Path
  4. Docker Volume with Absolute Path

Here is the example for above:

version: '3'
services:
    sample:
        image: sample
        volumes:
            - ./relative-path-volume:/var/data-two
            - /home/ubuntu/absolute-path-volume:/var/data-one
            - docker-volume-default-path-volume:/var/data-three
            - docker-volume-absolute-path-volume:/var/data-four
volumes:
  docker-volume-default-path-volume: {}
  docker-volume-absolute-path-volume:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: /home/path/of/your/folder

Relative Path: ./relative-path-volume:/var/data-two

Absolute Path: /home/ubuntu/absolute-path-volume:/var/data-one

Docker Volume Default Path: docker-volume-default-path-volume:/var/data-three

Docker Volume with Absolute Path: docker-volume-absolute-path-volume:/var/data-four

This works for any server as we customize the volume device property to the respective directory path.

Assegai answered 24/10, 2017 at 12:44 Comment(6)
Hi Jinna, thanks for this, but will this not have portability issues with different folder structures possibly on different environments? I know I could docker volumes anyway but thought the relative path binding had the advantage of portability (though I had not considered Swarm, which is definitely a good point...)Randa
Good stuff, thank you! This is great for setting up local stuff for personal use etc...Gunar
While this is a good explanation about the possible options, it's no answer to the question about differences and drawbacks: "Is there something inherently wrong with this relative binding approach? If I do switch to volumes, when moving the containers do I have to manually move the volumes?"Masquerade
Downvote because it doesn't really answer the question that was askedPeanuts
Does a relative volume path depend on or have any relation to what build: context: is set to?Kerr
Jarad, There is no dependency on build context for volume mounting of any typeAssegai
K
4

To answer the original question, the reason you would prefer one type of path over another is entirely outside the scope of docker itself. There's no performance benefit one way or the other.

Which one is better is entirely dependent on your environment and the internet can't answer which is best, particularly with no other information given.

A reason you may want a full path like /foo/bar is because that path is always going to be there on all the hosts machines - like maybe an nfs mount point. Or a socket or a global config file.

A reason you may want a relative path is you are building things from a script (like ansible, salt, ...) or you are going to have multiple containers running on the same host and they all need independent data directories. The scripting in that case would be a lot easier with relative paths.

As for why to use a volume versus a directory, there's a good answer on serverfault.

Kristine answered 31/12, 2022 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.