Skip to main content
BLOG.siposdani87

Setup docker-compose for external volume location

Configure Docker Compose to store volume data in an external directory
By: Dániel Sipos on

The Docker documentation on managing data describes how to use external volumes. By default, volumes are stored in /var/lib/docker/volumes/ on Linux. For bind mounts to external directories, the configuration requires a more explicit setup.

Example of docker-compose.yml

I use Redis as a cache storage on some projects. Sometimes it is necessary to store the data on an external directory or a mounted volume, so I would show the details below as an example.

version: '3.7'
services:
  redis:
  container_name: redis
  image: redis:5.0.14-alpine
  command: ['redis-server', '--appendonly', 'yes']
  volumes:
    - redis_data:/data
  networks:
    - redis_net
  deploy:
    replicas: 1
    restart_policy:
      condition: on-failure
  healthcheck:
    test: ['CMD-SHELL', 'redis-cli', 'ping']
    interval: 1m
    timeout: 10s
    retries: 3
    start_period: 60s
networks:
  redis_net:
volumes:
  redis_data:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: '/mnt/volume_01/docker_redis_data'

Conclusion

The initial expectation was a simpler configuration, but Docker’s volume management requires this more explicit setup.

Share with your friends

Related posts