How to Create and Use Container Volumes within Portainer
Portainer has become my favorite tool for managing containers. With this web-based GUI, you can do just about anything you need to work with your Docker containers. It’s powerful, flexible, and very user-friendly.
With Portainer, you can manage nearly any aspect of your containers, from building and deploying single apps/services and even full-stack applications. Another area that Portainer really shines in is the creation and usage of volumes.
What are Docker volumes? Simple. Imagine you deploy an application or service that stores data. If you deploy the container without a volume, the data will be stored within the container, and accessing that data from another container might be challenging. Even worse, should something happen to that container, the data it housed could get hosed.
That’s where volumes come into play by creating persistent storage for your containers. That way, you can create a container that makes use of an external volume. Should something happen to the container, the data is still present in the volume.
I’m going to show you just how easy it is to use volumes in Portainer by way of deploying a simple NGINX container that is mapped to a persistent volume hosted on a Linux server.
To accomplish this, you’ll need a running instance of Portainer, a Linux server, and a user with sudo privileges on the Linux machine.
Are you ready? Let’s get to work.
Install Samba on the Linux Server
To make this interesting, we’re going to use a Samba share, an open source Microsoft Windows file-sharing system, as the persistent volume. To make that work, log into your Linux server (I’m using Ubuntu Server 22.04) and install the necessary dependencies with:
sudo apt-get install samba cifs-utils -y
Next, create a directory to house the share (I’m going to use /home/jack/data, but you can use whatever you like) with the command:
Next, create a password for your Linux user with:
sudo smbpasswd -a USER
Where USER is your Linux username.
You’ll be prompted to type and verify the new password.
Enable the Samba user with:
sudo smbpasswd -e USER
Where USER is your Linux username.
Next, create the new share by editing the Samba config file with the command:
sudo nano /etc/samba/smb.conf
At the bottom of that file paste the following (editing to match the directory you created for the share):
path = /home/jack/data
browsable = yes
writable = yes
read only = no
Save and close the file. Restart Samba with:
sudo systemctl restart smbd
Creating a Volume in Portainer
Next, log into your Portainer instance (If you’ve not already deployed it check out this New Stack tutorial to find out how) and click Volumes from the left sidebar. In the resulting window (Figure 1), give the new volume a name.
Next, click the ON/OFF slider for Use CIFS volume until it’s in the ON position. Under CIFS Settings, type the IP address for your Linux server, add the share name (for this example, it’s /data), make sure to select CIFS 3.0 from the version drop-down, type the username you added to Samba, and then type the password.
After you’ve filled out the necessary information, click Create the volume.
Groovy. You’ve created your first volume.
Deploying NGINX with the Volume
Now for the fun part. Click Containers from the left sidebar and then click Add Container. In the resulting window (Figure 2), name the container (something like nginxvolumetest) and type nginx in the Image field.
Next, click publish a new network port and, in the resulting window (Figure 3), type 8081 for the host and 80 for the container.
Next, click on the Volumes tab under Advanced container settings and click map additional volumes. In this new section (Figure 4), you’ll type /usr/share/nginx/html (which is the document root for the NGINX Docker container) in the container field. From the volume drop-down, select the new volume you just created (in my case, it’s DATA).
Scroll back up and click Deploy container. The NGINX container should deploy without a problem.
Creating a Sample NGINX Index File
Go back to the Linux server that’s hosting the volume. We’re going to create a sample index.html file for NGINX. Create the file with (making sure to edit the command to reflect the location of the share you created):
In that file, paste the following content:
<p>Isn't this cool?</p>
Save and close the file. Now, open a web browser and point it to http://SERVER:8081 (Where SERVER is either the IP address or domain of the hosting server). You should see the contents of the index.html we created (Figure 5).
Congratulations, you’ve just deployed an NGINX container that uses persistent volumes from a Samba share on a Linux server, all from the user-friendly goodness that is Portainer.