Score:0

What is the right way to automount an external drive in a docker container?

cn flag

I want to create a personal cloud service in a docker container on a Raspberry Pi. Due to the limited storage and longevity of SD cards, I want to create the database one an external drive of sufficient capacity.

First I ran into the issue of mounting at all in container and could solve this problem by creating a privileged container. Now I wanted to use autofs.service to automatically mount the drive to desired location, but systemd is missing. After some reading I found that using it in a container is not recommended.

So how do I automount a drive the easiest way? Which is the way to go?

Score:0
vn flag

I believe the correct way to do this is by mounting the external drive on your host system, and then mapping the volume onto the container at startup.

I'll provide a basic example with the NextCloud container.

So mount your external drive normally on your host system - let's say you mount it to /mnt/my-data.

Next, you want to create a directory for the NextCloud data:

$ mkdir /mnt/my-data/nc-data

Then, when running your NextCloud container image, provide information to map the volume onto the internal NextCloud data directory:

$ docker run -d \
-v /mnt/my-data/nc-data:/var/www/html \
nextcloud

If you want an external database, do the same for MariaDB:

$ mkdir /mnt/my-data/nc-mariadb

$ docker run -d \
-v /mnt/my-data/nc-mariadb:/var/lib/mysql \
mariadb

Make sure the owner and permissions for the data directories match those of the container running.

I find that it's easiest to use docker-compose to generate a complete installation file of the entire container stack. Using the above example, and the docker-compose file shown on Docker Hub, this would be the result:

version: '2'

services:
  db:
    image: mariadb
    restart: always
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW
    volumes:
      - /mnt/my-data/nc-mariadb:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud

  app:
    image: nextcloud
    restart: always
    ports:
      - 8080:80
    links:
      - db
    volumes:
      - /mnt/my-data/nc-data:/var/www/html
    environment:
      - MYSQL_PASSWORD=
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=db

Note that the "volumes" section is not present, since we map an absolute path on your host system (/mnt/my-data/nc-xxxx) and not a named volume.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.