Efficient Backup and Data Sharing Techniques in Docker

School
West Valley College**We aren't endorsed by this school
Course
ACC 25
Subject
Information Systems
Date
Dec 12, 2024
Pages
2
Uploaded by lk8d85l5md
You can then use the traditional techniques for backup and recovery of the database. For instance, in the cloud, you might want to use an Elastic Block Store (e.g., AWS EBS) mounted on an instance and then mounted inside a container. You can also keep your MySQL dumps inside an Elastic Storage (e.g., AWS S3). Discussion Although this recipe uses MySQL, the same techniques are valid for Postgres and other databases. If you use the Postgres (https://registry.hub.docker.com/_/postgres/) image from Docker Hub, you can also see in the Dockerfile (http://bit.ly/postgres- dockerfile) that a volume is created (VOLUME /var/lib/postgresql/data). 1.18 Sharing Data in Your Docker Host with Containers Problem You have data on your host that you would like to make available in a container. Solution Use the -v option of docker run to mount a host volume into a container. For example, to share the working directory of your host within a /cookbook directory in a container, do this: $ 1s data $ docker run -ti -v "$PWD":/cookbook ubuntu:14.04 /bin/bash root@11769701f6f7: /# 1s /cookbook data In this example, you mount the working directory in the host into the /cookbook directory in the container. If you create files or directories within the container, the changes will be written directly to the host working directory, as shown here: $ docker run -ti -v "S$PWD":/cookbook ubuntu:14.04 /bin/bash root@44d71a605b5b: /# touch /cookbook/foobar root@44d71a605b5b: /# exit exit $ 1ls -1 foobar -rw-r--r-- 1 root root © Mar 11 11:42 foobar By default, Docker mounts the volume in read-write mode. If you want to mount it in read-only mode, you can specify it after the name of the volume, using a colon. For example, to mount the previous working directory to /cookbook as read-only, you would use -v "$PWD": /cookbook:ro. You can inspect your mount mapping with the docker inspect command. See Recipe 9.1 for more information about inspect. 32 | Chapter 1: Getting Started with Docker
Background image
$ docker inspect -f {{.Mounts}} 44d71a605b5b [{ /Users/sebastiengoasguen/Desktop /cookbook true}] See Also « Managing data in containers (https://docs.docker.com/userguide/dockervolumes/) « Understanding volumes (http://container-solutions.com/2014/12/understanding- volumes-docker/) « Data container (http://container42.com/2014/11/18/data-only-container- madness/) « Docker volumes (http://container42.com/2014/11/03/docker-indepth-volumes/) 1.19 Sharing Data Between Containers Problem You know how to mount a host volume into a running container, but you would like to share a volume defined in a container with other containers. This would have the benefit of letting Docker manage the volumes and support the principle of single responsibility. Solution Use data containers. In Recipe 1.18, you saw how to mount a host volume into a con- tainer. You used the -v option of docker run, specifying a host volume and a path within a container to mount that volume to. If the host path is omitted, you create a data container. The volume specified is created inside the container as a read-write filesystem not layered on top of the read-only layers used to create the container image. Docker manages that filesystem, but you can read and write to it from the host. Let’s illustrate this (with a truncated ID of the volumes for brevity): $ docker run -ti -v /cookbook ubuntu:14.04 /bin/bash root@b5835d2b951e: /# touch /cookbook/foobar root@b5835d2b951e: /# 1s cookbook/ foobar root@5835d2b951e: /# exit exit bash-4.3% docker inspect -f {{.Mounts}} b5835d2b951e [{dbba7caf8d07b862b61b39... /var/lib/docker/volumes/dbba7caf8d07b862b61b39... \ /_data /cookbook local true}] $ sudo ls /var/lib/docker/volumes/dbba7caf8d07b862b61b39. .. foobar 1.19 Sharing Data Between Containers | 33
Background image