14. DevOps: Creating a data volume with couchdb

Docker-logo

How to create Data Volumes using docker containers?:

In this exercise I would like to create couchdb data volume as below under docker container
Use the below command to make a couchdb volume

Step1:
Make directory data-volume1
===== Commands output =====>
vskumar@ubuntu:~$
vskumar@ubuntu:~$ pwd
/home/vskumar
vskumar@ubuntu:~$ ls
Desktop Downloads Music Public Videos
Documents examples.desktop Pictures Templates
vskumar@ubuntu:~$ mkdir data-volume1
vskumar@ubuntu:~$ ls
data-volume1 Documents examples.desktop Pictures Templates
Desktop Downloads Music Public Videos
vskumar@ubuntu:~$ cd data-volume1
vskumar@ubuntu:~/data-volume1$
=======================================>

Step2: I need to pull the couchdb latest image from the docker hub as below:

docker pull couchdb Using default tag:latest

=== Output =======>
vskumar@ubuntu:~/data-volume1$ sudo docker pull couchdb
Using default tag: latest
latest: Pulling from library/couchdb
4176fe04cefe: Pull complete
9f0a7c716711: Pull complete
796517a7b990: Pull complete
003491b79092: Pull complete
1502aa8b5925: Pull complete
d4017d9fa68f: Pull complete
30bc291a9bfe: Pull complete
4018e1354d8f: Pull complete
ebef40645ea4: Pull complete
f11931e5cbae: Pull complete
Digest: sha256:b95dce63ab64991640e5c9d4cc1597055690b1c1bb79ab30829d498f5f2301fcStatus: Downloaded newer image for couchdb:latest
vskumar@ubuntu:~/data-volume1$
=============================>

Step3:

Now, use the below command to create the data instance
==================>
vskumar@ubuntu:~/data-volume1$ sudo docker run -d –name my-couchdb couchdb
f84f95c5c9d2bdcabfb0ef796cb3e9b3bef0cec64ef4349d46f250a9065aa399
vskumar@ubuntu:~/data-volume1$
===================>
The above image includes EXPOSE 5984 (the CouchDB port),
so standard container linking will make it automatically available to the
linked containers.

Now, let me check the running containers:

====== List of containers along with couchdb ===>
vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7cba170d39a couchdb “tini — /docker-ent…” 5 minutes ago Up 5 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb-app
f84f95c5c9d2 couchdb “tini — /docker-ent…” 6 minutes ago Up 6 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb
10ffea6140f9 ubuntu “bash” 2 months ago Exited (0) 2 months ago quizzical_lalande
b2a79f8d2fe6 ubuntu “/bin/bash -c ‘while…” 2 months ago Exited (255) 2 months ago goofy_borg
155f4b0764b1 ubuntu:16.04 “/bin/bash” 2 months ago Exited (0) 2 months ago zen_volhard
vskumar@ubuntu:~$

vskumar@ubuntu:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7cba170d39a couchdb “tini — /docker-ent…” 6 minutes ago Up 6 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb-app
f84f95c5c9d2 couchdb “tini — /docker-ent…” 8 minutes ago Up 8 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb
vskumar@ubuntu:~$
==============================>

Step4:

Now, we need to use the couchdb instance:
sudo docker run –name my-couchdb-app –link my-couchdb:couch couchdb

============>
Please note when I executed the above command in CLI, it started the DB server and
working as a dedicated terminal with its continuous display.
Now, we can not use the same terminal.
Hence I opened another terminal

===============>

From another terminal:
======= Current status ====>
vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7cba170d39a couchdb “tini — /docker-ent…” 10 minutes ago Up 9 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb-app
f84f95c5c9d2 couchdb “tini — /docker-ent…” 11 minutes ago Up 11 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb
10ffea6140f9 ubuntu “bash” 2 months ago Exited (0) 2 months ago quizzical_lalande
b2a79f8d2fe6 ubuntu “/bin/bash -c ‘while…” 2 months ago Exited (255) 2 months ago goofy_borg
155f4b0764b1 ubuntu:16.04 “/bin/bash” 2 months ago Exited (0) 2 months ago zen_volhard
vskumar@ubuntu:~$

zen_volhard
vskumar@ubuntu:~$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7cba170d39a couchdb “tini — /docker-ent…” 10 minutes ago Up 10 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb-app
f84f95c5c9d2 couchdb “tini — /docker-ent…” 12 minutes ago Up 12 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb
vskumar@ubuntu:~$
===========================>

Step5:

Now, we need to attach this couchdb volumes to a local directory:

sudo docker run -d -v $(pwd):/opt/couchdb/data –name my-couchdb couchdb
To execute the above command, I need to remove the existing container and re-execute.

======= Removing the container forcedly ====>
vskumar@ubuntu:~$ sudo docker rm -f b7cba170d39a
b7cba170d39a
vskumar@ubuntu:~$
vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f84f95c5c9d2 couchdb “tini — /docker-ent…” 19 minutes ago Up 19 minutes 4369/tcp, 5984/tcp, 9100/tcp my-couchdb
10ffea6140f9 ubuntu “bash” 2 months ago Exited (0) 2 months ago quizzical_lalande
b2a79f8d2fe6 ubuntu “/bin/bash -c ‘while…” 2 months ago Exited (255) 2 months ago goofy_borg
155f4b0764b1 ubuntu:16.04 “/bin/bash” 2 months ago Exited (0) 2 months ago zen_volhard
vskumar@ubuntu:~$ sudo docker rm -f f84f95c5c9d2
f84f95c5c9d2
vskumar@ubuntu:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10ffea6140f9 ubuntu “bash” 2 months ago Exited (0) 2 months ago quizzical_lalande
b2a79f8d2fe6 ubuntu “/bin/bash -c ‘while…” 2 months ago Exited (255) 2 months ago goofy_borg
155f4b0764b1 ubuntu:16.04 “/bin/bash” 2 months ago Exited (0) 2 months ago zen_volhard
vskumar@ubuntu:~$
============ You can see there are no couchdb containers now=====>
Let us recreate it with the below command:
sudo docker run -d -v $(pwd):/opt/couchdb/data –name mytest-couchdb couchdb

=== re-creating a couchdb instance ===================>

vskumar@ubuntu:~/data-volume1$ sudo docker run -d -v $(pwd):/opt/couchdb/data –name mytest-couchdb couchdb
[sudo] password for vskumar:
ac849b4905d712740b7f5972e13836552914e7fdfd37e06dc2ecb6697a22c7dc
vskumar@ubuntu:~/data-volume1$

vskumar@ubuntu:~/data-volume1$ ls
_dbs.couch _nodes.couch _replicator.couch _users.couch
vskumar@ubuntu:~/data-volume1$ ls -l
total 44
-rw-r–r– 1 vskumar 999 4240 Feb 17 06:10 _dbs.couch
-rw-r–r– 1 vskumar 999 8368 Feb 17 06:10 _nodes.couch
-rw-r–r– 1 vskumar 999 8374 Feb 17 06:10 _replicator.couch
-rw-r–r– 1 vskumar 999 8374 Feb 17 06:10 _users.couch
vskumar@ubuntu:~/data-volume1$
=================================?

Step5:

Now we need to Specify the admin user in the DB environment:

We can use the two environment variables COUCHDB_USER and COUCHDB_PASSWORD to
set up the admin user.

$ sudo docker run -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -d couchdb

===== Creating User and PWD =========>
vskumar@ubuntu:~/data-volume1$ sudo docker run -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -d couchdb
4af58b1eed0e71ec11e0ea4cdf918ff657c36d312cfbf32ea0e2a7d7e9e23ee5
vskumar@ubuntu:~/data-volume1$
================================>

Step6:

Now let us create the below files:

======== .ini file ====>
vskumar@ubuntu:~/data-volume1$
vskumar@ubuntu:~/data-volume1$ ls -l
total 52
-rw-r–r– 1 vskumar 999 4240 Feb 17 06:10 _dbs.couch
-rw-rw-r– 1 vskumar vskumar 49 Feb 17 06:32 dockerfile
-rw-rw-r– 1 vskumar vskumar 49 Feb 17 06:29 local.ini
-rw-r–r– 1 vskumar 999 8368 Feb 17 06:10 _nodes.couch
-rw-r–r– 1 vskumar 999 8374 Feb 17 06:10 _replicator.couch
-rw-r–r– 1 vskumar 999 8374 Feb 17 06:10 _users.couch
vskumar@ubuntu:~/data-volume1$ cat local.ini

writer = file
file = /opt/couchdb/log/couch.log
=========== Creating a dockerfile in the current dir =====>
vskumar@ubuntu:~/data-volume1$ cat dockerfile

FROM couchdb

COPY local.ini /opt/couchdb/etc/

vskumar@ubuntu:~/data-volume1$
========================>
Now, we need to tag the db as below:

=====================>
vskumar@ubuntu:~/data-volume1$ sudo docker build -t mytest-couchdb .
Sending build context to Docker daemon 40.45kB
Step 1/2 : FROM couchdb
—> af415fd5efda
Step 2/2 : COPY local.ini /opt/couchdb/etc/
—> b88156204d48
Successfully built b88156204d48
Successfully tagged mytest-couchdb:latest
vskumar@ubuntu:~/data-volume1$
=========================>

Now, let us map the port as below:
The default port of couchdb is 5984.
$ sudo docker run -d -p 5984:5984 mytest-couchdb

=================================>
vskumar@ubuntu:~/data-volume1$
vskumar@ubuntu:~/data-volume1$ sudo docker run -d -p 5984:5984 mytest-couchdb
a9e5e6e6abc30c32830a0d3b70e7fe203d63dbd2de974d0dd02d1ccf0b53232e
vskumar@ubuntu:~/data-volume1$
==============>
My current docker host ip is 172.17.0.1
I would like to curl to the cocuchdb port 5984 as below to test its availability:

sudo curl -X PUT http://172.17.0.1:5984/db
=========================>
vskumar@ubuntu:~/data-volume1$ sudo curl -X PUT http://172.17.0.1:5984/db
{“ok”:true}
vskumar@ubuntu:~/data-volume1$
===== The couchdb data volume is available to use =================>

Step7:

Now, let me test this db by adding a document as below:

sudo curl -H ‘Content-Type: application/json’ -X POST http://172.17.0.1:5984/db -d ‘{“value”: “Hello I am Shanthi Kumar V, Testing my couchdb instance in a docker container”}’

============== Adding document into couchdb =======>
vskumar@ubuntu:~/data-volume1$ ls -l
total 52
-rw-r–r– 1 vskumar 999 4240 Feb 17 06:10 _dbs.couch
-rw-rw-r– 1 vskumar vskumar 49 Feb 17 06:32 dockerfile
-rw-rw-r– 1 vskumar vskumar 49 Feb 17 06:29 local.ini
-rw-r–r– 1 vskumar 999 8368 Feb 17 06:10 _nodes.couch
-rw-r–r– 1 vskumar 999 8374 Feb 17 06:10 _replicator.couch
-rw-r–r– 1 vskumar 999 8374 Feb 17 06:10 _users.couch
vskumar@ubuntu:~/data-volume1$ date
Sat Feb 17 06:52:48 PST 2018
vskumar@ubuntu:~/data-volume1$ sudo curl -H ‘Content-Type: application/json’ -X POST http://172.17.0.1:5984/db -d ‘{“value”: “Hello I am Shanthi Kumar V, Testing my couchdb instance in a docker container”}’
{“ok”:true,”id”:”4c3e6b9ece5a89445768618cad000ebc”,”rev”:”1-3514063c0977a3ab2a955a8498db6460″}
vskumar@ubuntu:~/data-volume1$
==================================>
Please note
“id”:”4c3e6b9ece5a89445768618cad000ebc” is the document id in couchdb.
Now let us check in the db as below:
I am using the document id:4c3e6b9ece5a89445768618cad000ebc

sudo curl http://172.17.0.1:5984/db/4c3e6b9ece5a89445768618cad000ebc

You can see the db output:
============= couch db ouput =====>
vskumar@ubuntu:~/data-volume1$
vskumar@ubuntu:~/data-volume1$ sudo curl http://172.17.0.1:5984/db/4c3e6b9ece5a89445768618cad000ebc
{“_id”:”4c3e6b9ece5a89445768618cad000ebc”,”_rev”:”1-3514063c0977a3ab2a955a8498db6460″,”value”:”Hello I am Shanthi Kumar V, Testing my couchdb instance in a docker container”}
vskumar@ubuntu:~/data-volume1$
===================================>

Step8:

Let us experiment it with one more new container:

===== Current containers =====>
vskumar@ubuntu:~/data-volume1$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9e5e6e6abc3 mytest-couchdb “tini — /docker-ent…” 21 minutes ago Up 21 minutes 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp compassionate_goldwasser
4af58b1eed0e couchdb “tini — /docker-ent…” About an hour ago Up About an hour 4369/tcp, 5984/tcp, 9100/tcp cocky_lamarr
ac849b4905d7 couchdb “tini — /docker-ent…” About an hour ago Up About an hour 4369/tcp, 5984/tcp, 9100/tcp mytest-couchdb
vskumar@ubuntu:~/data-volume1$
================================>
Now, let us try to kill the above containers one by one and check the status:

======= Kill all couchdb containers ====>
vskumar@ubuntu:~/data-volume1$ sudo docker kill a9e5e6e6abc3
a9e5e6e6abc3
vskumar@ubuntu:~/data-volume1$ sudo docker kill 4af58b1eed0e
4af58b1eed0e
vskumar@ubuntu:~/data-volume1$ sudo docker kill ac849b4905d7
ac849b4905d7
vskumar@ubuntu:~/data-volume1$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
vskumar@ubuntu:~/data-volume1$

============No active containers ===========>

sudo docker build -t vskumar-couchdb .

=========== Tagging to new container ======>
vskumar@ubuntu:~/data-volume1$ sudo docker build -t vskumar-couchdb .
Sending build context to Docker daemon 40.45kB
Step 1/2 : FROM couchdb
—> af415fd5efda
Step 2/2 : COPY local.ini /opt/couchdb/etc/
—> Using cache
—> b88156204d48
Successfully built b88156204d48
Successfully tagged vskumar-couchdb:latest
vskumar@ubuntu:~/data-volume1$
===========================>

sudo docker run -d -p 5984:5984 vskumar-couchdb

====== We can see the current active container as vskumar-couchdb =====>
vskumar@ubuntu:~/data-volume1$ sudo docker run -d -p 5984:5984 vskumar-couchdb
0f53e91f24950966ad03f2d9c0cefa97119e3a7321e947e53ed1f6a245e4e9a7
vskumar@ubuntu:~/data-volume1$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53e91f2495 vskumar-couchdb “tini — /docker-ent…” 11 seconds ago Up 8 seconds 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp nervous_spence
vskumar@ubuntu:~/data-volume1$
========================>

Now we can insert a new document as below into this new container:
==================>
vskumar@ubuntu:~/data-volume1$
vskumar@ubuntu:~/data-volume1$ sudo curl -H ‘Content-Type: application/json’ -X POST http://172.17.0.1:5984/db -d ‘{“value”: “Hello I am Shanthi Kumar V, Testing my couchdb 2nd—>instance in a new docker container”}’
{“ok”:true,”id”:”c683de9d91bb7628c5428e521e00047a”,”rev”:”1-beade1bc7636c3b41126b9c867a47028″}
vskumar@ubuntu:~/data-volume1$
=================>
The document “id”:”c683de9d91bb7628c5428e521e00047a”.
Let us check in db:

sudo curl http://172.17.0.1:5984/db/c683de9d91bb7628c5428e521e00047a
============= Results ====>
vskumar@ubuntu:~/data-volume1$ sudo curl http://172.17.0.1:5984/db/c683de9d91bb7628c5428e521e00047a
{“_id”:”c683de9d91bb7628c5428e521e00047a”,”_rev”:”1-beade1bc7636c3b41126b9c867a47028″,”value”:”Hello I am Shanthi Kumar V, Testing my couchdb 2nd—>instance in a new docker container”}
vskumar@ubuntu:~/data-volume1$
===========================>
So, we found the inserted record.

At this point I want to stop this couchdb container exercise.

Vcard-Shanthi Kumar V-v3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s