Docker
Docker, Part Eight and a Half: docker volume

by Samir Talwar

Monday, 7 March 2016 at 08:00 GMT

I know I said I wouldn’t be writing about Docker this week but I wanted to correct the record. After Friday’s post, Lewis got in touch to ask why I hadn’t talked about the new Docker volumes. (Thanks Lewis!) I have to confess, I hadn’t even heard of them. Like the new networking functionality, docker volume represents a shift in the way of thinking towards more than just containers. Instead of volumes being an attribute of a container, they’re entities in their own right.

So let’s revisit our PostgreSQL container from last week, but this time using a Docker volume.

First off, we need to create one.

$ docker volume create --name=bemorerandom-postgresql

This will create a directory somewhere under /var/lib/docker:

$ docker volume inspect bemorerandom-postgresql
[
    {
        "Name": "bemorerandom-postgresql",
        "Driver": "local",
        "Mountpoint": "/mnt/sda1/var/lib/docker/volumes/bemorerandom-postgresql/_data"
    }
]

Now, when we run PostgreSQL, we can just use the name of the volume rather than a path to it. In reality, it’ll mount the path we see above.

$ docker run \
    -d \
    --name=postgres \
    -v bemorerandom-postgresql:/var/lib/postgresql/data \
    postgres

Everything else proceeds as normal, except that as Docker is managing the volume, we don’t need to worry about file permissions because the container will be able to write to the mount point, guaranteed. We can just start our application:

$ docker run \
    -d \
    --name=bemorerandom-api \
    -p 8080:8080 \
    --net=bemorerandom \
    -e DB_URL=jdbc:postgresql://postgres.bemorerandom/bemorerandom \
    -e DB_USER=bemorerandom \
    -e DB_PASSWORD=<password> \
    samirtalwar/bemorerandom.com-api

And everything is gravy.

But How Safe Is It?

That volume will last until you remove it, either deliberately or accidentally. It’s very easy, when developing, to delete something you didn’t mean to. Maybe you trash all volumes and then realise you needed one. Perhaps you delete your entire Docker Machine VM. Whatever. The point is that it’s pretty easy to lose data during development.

Turns out this happens on production too. Servers die. Hard disks fail. People type the wrong thing and drop entire tables.

So wherever your data is, on a Docker volume or somewhere in the omnibenevolent cloud, back your data up. Volumes are for persisting data between containers, not a magical solution that will preserve it forever. The same is true of your hard disks when you’re not using Docker. At the very least, set up a nightly backup and and practice a restore once per month.

End of freak-out.

  1. Docker, Part One: Getting Started
  2. Docker, Part Two: Images and Containers
  3. Docker, Part Three: Running Software
  4. Docker, Part Four: Repeating Yourself
  5. Docker, Part Five: Let's Make A Website
  6. Docker, Part Six: A Slow Build Is Worse Than No Build At All
  7. Docker, Part Seven: Start Talking
  8. Docker, Part Eight: Turn Up The Volume
  9. Docker, Part Eight and a Half: docker volume
  10. Docker, Part Nine: Scripted Deployment
  11. Docker, Part Ten: Docker Compose
  12. Docker, Part Eleven: The Guts Of Docker Compose
  13. Docker, Part Twelve: Cleaning House
  14. Docker, Part Thirteen: The Twelve-Factor App
  15. Docker, Part Fourteen: Behave Like A Process
  16. Docker, Part Fifteen: Securing Your Containers
  17. Docker, Part Sixteen: Logging
  18. Docker, Part Seventeen: Continuous Deployment

If you enjoyed this post, you can subscribe to this blog using Atom or RSS.

Maybe you have something to say. You can email me or toot at me. I love feedback. I also love gigantic compliments, so please send those too.

Please feel free to share this on any and all good social networks.

This article is licensed under the Creative Commons Attribution 4.0 International Public License (CC-BY-4.0). The Markdown source is available on Codeberg.