Sunday, September 8, 2019

docker: Pull mongo image and restore backuped database to container

Let's see how I tried to restore local mongo database to mongo docker container.
'docker cp' command didn't work for host -> container file copy.
I created docker volume and used it to achieve this.

Retrieve mongo image from docker repository
[root@localhost ~]# docker pull mongo

Backup existing database with 'mongodump'
[root@localhost ~]# mongodump --collection testCol --db articles --out ./testCol

Let's create docker volume for passing backup files to docker container
[root@localhost ~]# docker volume create my-vol

Check the full path of created volume
[root@localhost ~]# docker volume inspect my-vol
[
    {
        "CreatedAt": "2019-09-05T00:20:34+09:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": {},
        "Scope": "local"
    }
]

Copy back up files to the volume, so that we can access them from container later
[root@localhost ~]# cp -R ./testCol/* /var/lib/docker/volumes/my-vol/_data/

Run container with mounting created volume
[root@localhost ~]# docker run -itd --mount source=my-vol,target=/app mongo

Identify the container name
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d70f1c3f0101 mongo "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 27017/tcp admiring_bell

Verify if the volume is being mounted properly
[root@localhost ~]# docker inspect admiring_bell | grep -A10 -i mount
            "Mounts": [
                {
                    "Type": "volume",
                    "Source": "my-vol",
                    "Target": "/app"
                }
            ],
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
--
        "Mounts": [
            {
                "Type": "volume",
                "Name": "my-vol",
                "Source": "/var/lib/docker/volumes/my-vol/_data",
                "Destination": "/app",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            },


Now we can see copied directory from the container
[root@localhost ~]# docker exec -it admiring_bell ls -l /app
total 0
drwxr-xr-x. 2 root root 55 Sep 4 15:20 articles

Restore back up files to database inside the container
[root@localhost ~]# docker exec -it admiring_bell mongorestore --db articles /app/articles
2019-09-08T05:51:16.952+0000 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2019-09-08T05:51:16.953+0000 building a list of collections to restore from /app/articles dir
2019-09-08T05:51:16.956+0000 reading metadata for articles.testCol from /app/articles/testCol.metadata.json
2019-09-08T05:51:17.005+0000 restoring articles.testCol from /app/articles/testCol.bson
2019-09-08T05:51:17.022+0000 no indexes to restore
2019-09-08T05:51:17.023+0000 finished restoring articles.testCol (38 documents, 0 failures)
2019-09-08T05:51:17.023+0000 38 document(s) restored successfully. 0 document(s) failed to restore.


Let's check the restored database with interactive console of the container
[root@localhost ~]# docker run -it --link=admiring_bell:mongo mongo /bin/sh

# env
HOSTNAME=d74d7531ee8c
~~~~~~~
MONGO_PORT_27017_TCP=tcp://172.17.0.2:27017
~~~~~~~

# mongo --host 172.17.0.2:27017
MongoDB shell version v4.2.0
connecting to: mongodb://172.17.0.2:27017/?compressors=disabled&gssapiServiceName=mongodb

> show databases
admin 0.000GB
articles 0.000GB
config 0.000GB
local 0.000GB

> use articles

switched to db articles

> show collections
testCol

> db.testCol.find({})
{ "_id" : ObjectId("5d6682061f2af8077f940255"), "name" : "大丈夫", "query" : "%E5%A4%A7%E4%B8%88%E5%A4%AB", "tweet_volume" : 490680, "date" : "8/28/2019, 10:30:45 PM" }
{ "_id" : ObjectId("5d6683097633bc07a7b6f03d"), "name" : "大丈夫", "query" : "%E5%A4%A7%E4%B8%88%E5%A4%AB", "tweet_volume" : 493621, "date" : "8/28/2019, 10:35:04 PM" }
Type "it" for more

> exit


Everything works fine, now we can stop the container
[root@localhost ~]# docker container stop admiring_bell
admiring_bell

Clean it up
[root@localhost ~]# docker system prune

No comments:

Post a Comment