[Docker] Volumes Lab Docker (Part 3)

2 minute read

بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم

Lab kali ini kita akan mencoba Volumes, seperti mount file static kedalam container yang sedang berjalan di node local maupun beda node yang berbeda.

Eksekusi di node pod-pod0


1. Membuat volume & Melihat Detail nya

[email protected]:~# sudo docker volume create my-vol
[email protected]:~# docker volume ls
local               my-vol
[email protected]:~# docker inspect my-vol
        "CreatedAt": "2019-10-06T14:42:33Z",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": {},
        "Scope": "local"
[email protected]:~# 

2. Jalankan container dengan volume dan uji coba image nya via browser

[email protected]:~# sudo docker run -d --name=nginxtest -v my-vol:/usr/share/nginx/html nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
b8f262c62ec6: Pull complete 
e9218e8f93b1: Pull complete 
7acba7289aa3: Pull complete 
Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1
Status: Downloaded newer image for nginx:latest
[email protected]:~# sudo docker inspect nginxtest | grep -i ipaddress
            "SecondaryIPAddresses": null,
            "IPAddress": "",
                    "IPAddress": "",
[email protected]:~# curl
<!DOCTYPE html>
<title>Welcome to nginx!</title>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
[email protected]:~# 

3. Buat file index.html dan pindahkan ke direktori source volume

[email protected]:~# sudo echo "This is from my-vol source directory." > index.html
[email protected]:~# sudo mv index.html /var/lib/docker/volumes/my-vol/_data
[email protected]:~# curl
This is from my-vol source directory.

4. Jalankan container dengan read-only volume

[email protected]:~# sudo docker run -d --name=nginxtest-rovol -v my-vol:/usr/share/nginx/html:ro nginx:latest
[email protected]:~# sudo docker inspect nginxtest-rovol | grep -i RW
                "RW": false

Volume Driver

5. SSH ke pod-pod1. Buat folder /share dan exit kembali

ssh -l ubuntu
sudo mkdir /share
sudo chmod 777 /share

6. Instal plugin sshfs

sudo docker plugin install --grant-all-permissions vieux/sshfs
sudo docker plugin ls
sudo docker plugin disable [PLUDIN ID]
sudo docker plugin set vieux/sshfs sshkey.source=/root/.ssh/
sudo docker plugin enable [PLUDIN ID]
sudo docker plugin ls
[email protected]:~# docker plugin ls
ID                  NAME                 DESCRIPTION               ENABLED
b0e65502c3d3        vieux/sshfs:latest   sshFS plugin for Docker   true

7. Membuat volume dengan driver sshfs & Uji Jalankan

sudo docker volume create --driver vieux/sshfs -o sshcmd=[email protected]:/share  -o allow_other sshvolume
[email protected]:~# sudo docker run -d --name=nginxtest-ssh -p 8090:80 -v sshvolume:/usr/share/nginx/html nginx:latesta3b6f5ea53b4768802bbcd6480a1206eece95e5f2de6295ee4b07f743d9bb194

8. SSH ke pod-pod1.

ssh -l root
sudo sh -c "echo 'Hello, I am palopalepalo' > /share/index.html"
sudo cat /share/index.html

9. Eksekusi di pod-pod0

sudo docker ps
curl http://localhost:8090

Hello, I am palopalepalo

Enjoy Happy, Ngoprek ~