Create a local volume with mount options
In this step, you will learn how to create a local Docker volume and specify mount options. Mount options allow you to control how the filesystem is mounted, such as setting permissions, enabling specific features, or optimizing performance.
We will create a local volume named myvolume3 and use the o option to specify mount options. For this example, we will set the uid and gid options to ensure that files created in the volume by a container are owned by a specific user and group on the host. This can be useful for managing permissions when sharing data between containers and the host.
First, let's create a directory on the host that we will use as the source for our volume. This is not strictly necessary for a standard local volume, but it helps illustrate how mount options can affect the underlying filesystem.
mkdir -p ~/project/myvolumedata
Now, let's create the volume myvolume3 using the local driver and specify the o option with uid and gid. We will use the user and group ID of the current labex user. You can find your user and group ID using the id -u and id -g commands.
USER_ID=$(id -u)
GROUP_ID=$(id -g)
docker volume create --driver local --opt type=none --opt device=/home/labex/project/myvolumedata --opt o=bind,uid=$USER_ID,gid=$GROUP_ID myvolume3
Let's break down this command:
docker volume create: The command to create a volume.
--driver local: Specifies the local volume driver.
--opt type=none: Specifies that no filesystem type should be automatically created. We are binding to an existing directory.
--opt device=/home/labex/project/myvolumedata: Specifies the device to mount, which is the directory we created on the host. Note the use of the absolute path /home/labex/project/myvolumedata.
--opt o=bind,uid=$USER_ID,gid=$GROUP_ID: Passes the mount options.
bind: Specifies a bind mount, linking the volume to the specified device (our host directory).
uid=$USER_ID: Sets the user ID for files created in the volume to the current user's ID.
gid=$GROUP_ID: Sets the group ID for files created in the volume to the current user's group ID.
myvolume3: The name of the volume.
You should see the name of the volume printed to the console.
Now, let's inspect the volume to see the options.
docker volume inspect myvolume3
In the output, you should see the Driver as local, the Options including type=none, device=/home/labex/project/myvolumedata, and o=bind,uid=...,gid=... (with your user and group IDs). The Mountpoint will be the same as the device path.
Next, we will run a container and attach this volume. We will use the ubuntu image and mount myvolume3 to /app inside the container. Then, we will create a file inside the container's /app directory and check its ownership on the host.
First, pull the ubuntu image if needed.
docker pull ubuntu
Now, run the container and create a file in the mounted volume.
docker run --rm -v myvolume3:/app ubuntu bash -c "echo 'Testing ownership' > /app/testfile.txt && ls -l /app/testfile.txt"
This command runs an ubuntu container, mounts myvolume3 to /app, writes "Testing ownership" to /app/testfile.txt, and then lists the file details inside the container. You should see the file listed with root ownership inside the container, as containers typically run as root by default.
Now, let's check the ownership of the file on the host machine in the ~/project/myvolumedata directory.
ls -l ~/project/myvolumedata/testfile.txt
You should see that the file testfile.txt is owned by the labex user and group on the host, thanks to the uid and gid mount options we specified when creating the volume.
Finally, let's clean up the created directory.
rm -rf ~/project/myvolumedata