How can I mount a Block Storage Volume from inside my Docker container?

Linode Staff

I want to attach a Block Storage Volume to my Linode and then read from and write to it inside a Docker container. How can I do this?

2 Replies

This goal can be accomplished on the Linode platform. If you haven't already, create a Block Storage Volume and mount it to your Linode: How to Use Block Storage with Your Linode

For my tests, I named my Block Storage Volume docker-bs-volume-test and mounted it to /mnt/docker-bs-volume-test on my Linode. I then ran the following command to start a container from the latest NGINX Docker image, bind the Block Storage Volume to my container, and execute bash within it (I didn't really need to use the NGINX image here; you could use any other image too):

[email protected]$ docker run -it \
>   --mount type=bind,source=/mnt/docker-bs-volume-test,target=/bs-volume-accessed-from-container \
>   nginx:latest \
>   bash

The bound Block Storage Volume showed up from inside the container as expected:

[email protected]:/# ls /
bin   bs-volume-accessed-from-container  etc   lib    media  opt   root  sbin  sys  usr
boot  dev                 home  lib64  mnt    proc  run   srv   tmp  var

I then created a test file in that directory and exited the container:

[email protected]:/# touch /bs-volume-accessed-from-container/testfile.txt
[email protected]:/# exit

Back outside of the container, I could confirm that the file I created from inside the container was written to the Block Storage Volume:

[email protected]:~$ ls /mnt/docker-bs-volume-test/
lost+found  testfile.txt

Running the image again, detaching from it, and inspecting the container's mounts also shows that the Block Storage Volume is bound as expected:

[email protected]$ docker run -d \
>   --mount type=bind,source=/mnt/docker-bs-volume-test,target=/bs-volume-accessed-from-container \
>   nginx:latest \
>   tail -f /dev/null
6cb6d5a3ca8b73997636ef33e7c293be995fa0a28ff8d5b2d622257f1cd2361b
[email protected]:~$ docker inspect -f '{{ .Mounts }}' 6cb6d5a3ca8b
[{bind  /mnt/docker-bs-volume-test /bs-volume-accessed-from-container   true rprivate}]

You can use the linode volume driver: https://github.com/libgolang/docker-volume-linode

for full disclosure, I'm the author.

At the moment I there is no installation script, so these are the steps to get it running:

  • Download and install golang https://golang.org/dl/
  • Install the driver by running: go get -u github.com/libgolang/docker-volume-linode
  • Run the driver in the background. Create an upstart or systemd script for easier management. The following command illustrates how to run it:
 $ docker-volume-linode -linode.token=<token> -linode.host=<linode-label> -linode.region=<region>

<token>: Linode API access token generated on linode console(cloud.linode.com)

<region>: The region where the linode host is running. Defaults to "us-west"

<linode-label>: the label of the linode host where the driver is runining. The label is not the actual hostname of the machine, but the label assigned on the linode console.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct