Playing with docker Volumes

 

Data generated or used by containers are not preserved beyond the life of containers. Volumes are one the of preferred way for persisting these data. 

Let us first check the behavior of a container..

Start an nginx container with below docker command.

 # docker run -p 80:80 nginx

 
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

Access the default page


 

 

 

You will get above page. 

Stop above container and get inside the container by using below command.

# docker run -it nginx /bin/sh
# hostname
7b3fb00899b3

Navigate to the nginx document root. You will see the index page.

# cd /usr/share/nginx/html
# ls -ltr
total 8
-rw-r--r--. 4 root root 612 Aug 11 14:50 index.html
-rw-r--r--. 4 root root 494 Aug 11 14:50 50x.html

Rename or remove the index.html file and create a new file with your content.

# mv index.html index.html.backup

# echo "This is a test index file" > index.html

# ls -ltr
total 12
-rw-r--r--. 1 root root 612 Aug 11 14:50 index.html.backup
-rw-r--r--. 4 root root 494 Aug 11 14:50 50x.html
-rw-r--r--. 1 root root  26 Sep 23 15:53 index.html
#

Here you are ready with your new index.html file.

These changes will be lost if you delete the container.


Now let us see how can we make our changes persistent with volumes.

Create a volume

# docker volume create myvol

Inspect the volume.

# docker volume inspect myvol
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/myvol/_data",
        "Name": "myvol",
        "Options": {},
        "Scope": "local"
    }
]

You can see the mountpoint.

go to the mountpoint and create an index file.

# cd /var/lib/docker/volumes/myvol/_data

created an index file with below content

# cat index.html
This is my test html file

issue below docker command to run the nginx container

# docker run -p 80:80 -v myvol:/usr/share/nginx/html nginx


/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

Try accessing the page again, you will get your new index.html

 

 

If you start the container without -v switch you will again get the nginx default page.