In this tutorial we’re going to explore how to view and edit the contents of a Volume mounted in a running container.
This tutorial is based on the container created in the Introduction:
some-nginx
http_data
Verify that the container is running
1
hakuna ps
And verify that the contaienr has a volume attached
1
hakuna ps --json
--json
return a JSON-formatted output that can be easily managed with jqA Volume is a persistent disk mounted in a running container. A volume can be mounted over multiple containers; we’re going to exploit this feature to mount service container on an already mounted volumes to have access to the files.
We need an nginx web server with avolume
1
2
hakuna volume create http_data --size 5
hakuna run --name some-nginx -p 80:80 -v http_data:/usr/share/nginx/html nginx
NGINX container does not expose SSH for security reasons. To inspect the files we can run a container that expose SSH and attach it to the http_data
volume.
We can use bubuntu, a customized Ubuntu image provided by us that comes with an sshd daemon and some tools to edit files.
Choose a password for ssh and run the container
1
hakuna run --name ssh -p 46587:22 -v http_data:/usr/share/nginx/html -e SSH_ROOT_PASS=<password> beekube/bubuntu
Command breakdown:
--name ssh
: this is the name for the container-p 46587:22
: expose the port 22 of the container (SSH) on internet using a different port - bogon networks scan the internet for opened ssh port - this is not secure, it just reduce the failed logins-v http_data:/usr/share/nginx/html
: we mount the same volume used by nginx-e SSH_ROOT_PASS=<password>
: set an environment variable with the root passwordTo retrieve the container hostname, run hakuna ps
. Now we can ssh in our new container:
1
ssh root@<ssh-publish> -p 46587
Now it is possible to edit the files using vim or nano, or by scp!