Docker Hands-On Part 04
Tags: tutorial, aws, docker
Published: 2024-02-09 17:15:47

## 04. Storing Container Data In Docker Volumes

**Introduction**

In this lesson, you are going to learn how to store your container data in Docker Volumes. You can store config files, static shared files, or any stateful information that you need. 

Here we are going to create 3 docker volumes. 

**Step-by-Step Guide Storing Container Data In Docker Volumes**

Step 01: Run the `docker volume` command to see if having existing docker volumes. 

```
$ docker volume ls

DRIVER    VOLUME NAME
```

Step 02: Now pull the image for Postgres image for setting up the docker volumes. Here I'm using Postgres 12.1. Postgres needs volumes in the background for the data. 

```
$ docker pull postgres:12.1

$ docker run -d --name db1 postgres:12.1

$ docker run -d --name db2 postgres:12.1
```

Step 03: Now run the docker volume command to see the volumes

```
$ docker volume ls

DRIVER    VOLUME NAME
local     1ae4ee6124f0bee423bec887931d9d80aec330a50b5e4edbaf4f151a19c2f23a
local     3dd9a005bb83fc892e2098237fa2bb3ad26407b82bb28096b64a327f26f4eb89
```

Step 04: It's very difficult to identify the volumes that using above. So we can run the below command. 

```
$ docker inspect db1 -f '{{ json .Mounts }}' | python -m json.tool

[
    {
        "Type": "volume",
        "Name": "3dd9a005bb83fc892e2098237fa2bb3ad26407b82bb28096b64a327f26f4eb89",
        "Source": "/var/lib/docker/volumes/3dd9a005bb83fc892e2098237fa2bb3ad26407b82bb28096b64a327f26f4eb89/_data",
        "Destination": "/var/lib/postgresql/data",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
]

```

**Create the Docker Volumes**

Step 01: We can create docker volumes using the `create` command. 

```
$ docker volume create website

DRIVER    VOLUME NAME
local     1ae4ee6124f0bee423bec887931d9d80aec330a50b5e4edbaf4f151a19c2f23a
local            e534041b91b8b520b79a9f42c4f97fa260cae5e25a11517ce2eed0f92faefad2
local     website

```

Step 02: Put data in the docker volumes. So we are going to put our web app codes under the _website_ volume

```
$  sudo cp -r /home/ec2-user/content-widget-factory-inc/web/* /var/lib/docker/volumes/website/_data/
```

To see the files

```
$ sudo ls -l /var/lib/docker/volumes/website/_data
total 16
drwxr-xr-x. 2 root root   76 Feb  9 16:51 img
-rw-r--r--. 1 root root 3276 Feb  9 16:51 index.html
-rw-r--r--. 1 root root 2910 Feb  9 16:51 quote.html
-rw-r--r--. 1 root root 2611 Feb  9 16:51 support.html
-rw-r--r--. 1 root root 2645 Feb  9 16:51 widgets.html

```

**Remove Unused Docker Volumes**

Run the `docker volume prune` command. 

```
$ docker volume prune

WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
1ae4ee6124f0bee423bec887931d9d80aec330a50b5e4edbaf4f151a19c2f23a
b61fc8243670de085a3fb120fceec864893ec097838e8be67c05b2e47daaef99
107c5e2f43fb50271133c7df2868716f2f4080e65dcc5cf2367581be6425149e

Total reclaimed space: 0B

```

**Backup and Restore data from Docker Volumes**

To do this we need to switch to the root. 

```
# docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED        STATUS        PORTS                                   NAMES
77fc2b4639f4   postgres:12.1   "docker-entrypoint.s…"   4 hours ago    Up 4 hours    5432/tcp                                db2
dfa30582e464   postgres:12.1   "docker-entrypoint.s…"   4 hours ago    Up 4 hours    5432/tcp                                db1
53aace921e70   httpd:latest    "httpd-foreground"       12 hours ago   Up 12 hours   0.0.0.0:8080->80/tcp, :::8080->80/tcp   httpd


# docker volume ls
DRIVER    VOLUME NAME
local     3dd9a005bb83fc892e2098237fa2bb3ad26407b82bb28096b64a327f26f4eb89
local     e534041b91b8b520b79a9f42c4f97fa260cae5e25a11517ce2eed0f92faefad2
local     website

```

We need to inspect the volume. 

```
# docker volume inspect website
[
    {
        "CreatedAt": "2024-02-09T16:48:21Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/website/_data",
        "Name": "website",
        "Options": null,
        "Scope": "local"
    }
]
```

To get the backup we need to follow the below instructions. We are storing the backup under/tmp/ dir. 

```
# tar czf /tmp/website_$(date +%Y-%m-%d-%H%M).tgz -C /var/lib/docker/volumes/website/_data .
```

```
# ls -l /tmp/website_2024-02-09-1708.tgz
-rw-r--r--. 1 root root 11385 Feb  9 17:08 /tmp/website_2024-02-09-1708.tgz
```

Now we see how we are doing this using non-privileged users. 

```
$ docker run -it --rm -v website:/website -v /tmp:/backup bash tar czf /backup/website_$(date +%Y-%m-%d-%H%M).tgz -C /website .

$ ls -l /tmp/website_*
-rw-r--r--. 1 root root 11385 Feb  9 17:08 /tmp/website_2024-02-09-1708.tgz
-rw-r--r--. 1 root root 11375 Feb  9 17:13 /tmp/website_2024-02-09-1713.tgz

```