Using Docker Volumes

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
336
Reaction score
361
Credits
11,504
When using Docker it is sometimes necessary to update files quite often. Updating those files can be a big hassle since the whole container must be recreated each time.
For instance, if you set up a website in Docker you would have to rebuild the container every time you wanted to change any of the HTML files. I will not make a Web Server in this article but create an FTP Server to share files from the local hard disk.
On an FTP Server you may need to easily add or remove the files you want to share with others.

Setting up an FTP Server

The File Transfer Protocol (FTP) Server allows you to share files on the Internet or Local Network.
Setting up an FTP Server in Docker is very simple. The code you need in the Docker file is as follows:

Code:
FROM metabrainz/docker-anon-ftp

That is all that is needed in the ‘dockerfile’ to create an FTP Server. I created the dockerfile in a folder called ‘ftptest’.
Use the following two commands to build and run the FTP Server using your ‘<id>’:

Code:
docker build -t <id>/ftp .
docker run -p 20-21:20-21 <id>/ftp

From the build and run code we are giving the container a name of ‘ftp’ under the DockerID you specify to prevent having to use an address. To connect to the FTP Server you can use an FTP Client such as FileZilla found at ‘https://filezilla-project.org/download.php?platform=linux’.
Once you have FileZilla installed and running, the HOST name is 172.17.0.1. You can find the IP Address by running ‘ifconfig’ and finding the section labeled ‘docker0’ as shown in Figure 1. Set the Port to 21 and press the ‘Quickconnect’ button and you should see something similar to Figure 2.

Figure 01.jpg

FIGURE 1

Figure 02.jpg

FIGURE 2

You may notice that the right side is the FTP Server and there aren’t any files listed. The reason for there being no shared files is that we placed no file in the share folder.
Let’s look at adding a file for sharing.
In the folder ‘ftptest’ I created a blank document file called ‘blank.txt’. I edited ‘dockerfile’ and added the following line at the end:

Code:
COPY ./blank.txt /var/ftp/blank.txt

The command will copy the ‘blank.txt’ file to the container in the folder ‘/var/ftp/’ and name the file ‘blank.txt’.
If we rebuild the container and run it again then connect using FileZilla you should see a window similar to Figure 3.

Figure 03.jpg

FIGURE 3

The shared file is now available for download. If you attempt to perform an upload to the container it will fail.
You should be able to notice the main issue fo using the Docker Container as an FTP Server. Every time you want to change a file in the shared list you need to rebuild and restart the container which can be a hassle.
To make this process easier we can set up a volume which is a pointer back to a folder on the local hard drive. The folder being used is not part of the container and can exist to any drive to which you have mounted.
Before I go on to set this up we need to make a change to the image. We need to look at the code that made the ‘metabrainz/docker-anon-ftp’ located at ‘https://github.com/metabrainz/docker-anon-ftp/blob/master/Dockerfile’. To find the location of the the code you need to first go to ‘hub.docker.com’ and search for the image you require. In this case I searched for ‘metabrainz’ and found ‘ker-anon-ftp’. Click on the link and once opened you can see a tab for ‘Dockerfile’. The new page shows the code, but it does not include any scripts or other files which may be needed. To the right-side of the page you will see a link for the Source Repository. Click on it and once loaded you can then select the button for ‘Clone or Download’ to get all of the needed files.
Looking back at the ‘dockerfile’ code you can see it is as follows:

Code:
FROM metabrainz/base-image
RUN apt-get update \
   && apt-get install -y --no-install-recommends vsftpd \
   && apt-get clean \
   && rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/run/vsftpd/empty \
   && mkdir -p /etc/vsftpd \
   && mkdir -p /var/ftp \
   && mv /etc/vsftpd.conf /etc/vsftpd.orig \
   && mkdir /etc/service/vsftpd
ADD vsftpd.sh /etc/service/vsftpd/run
VOLUME ["/var/ftp"]
EXPOSE 20-21
EXPOSE 65500-65515

    You see that there is a line setting up a VOLUME. The volume is for the folder ‘var/ftp’ inside the image.  We can leave the line as is and create the pointer from the RUN command or create both the volume and pointer from the RUN command.

NOTE:  In the code you can also see that the ‘vsftpd.sh’ file is being added to run at startup.

    If you wanted you can download the files from the link on Github.  And create the image from the included dockerfile.  If you also wanted you can remove the VOLUME line and we can perform setting up the volume from the command line.
    Once I have removed the ‘VOLUME’ line from the dockerfile I rebuild and re-run it.  I can connect to it with Filezilla as before and there are no files found in the FTP Server container.  Let’s look at changing the run commnd as well as setting up a local folder to share.
    In my folder with my dockerfile and ‘vsftpd.sh’ file I will create a folder called FTP.  Inside the FTP folder I will place a dummy file so I can see that the connection is being made properly to the volume being created.
    Since the image was rebuilt we can simply re-run the image with new parameters to create the volume.  The two parameters are as follows:
 
[code]-v /var/ftp
-v $(pwd)/ftp:/var/ftp/:ro

The first line will produce the mount point inside the container at ‘/var/ftp’ which is where the files are located inside the container.
The second line maps the outside folder to the inside (container) folder. So when the FTP Server looks at the folder ‘/var/ftp’ it will actually see the system folder ‘ftp’ in the Present Working Directory (pwd). The ‘:ro’ makes the mapping a read-only mounting.
Once you add any files into the ftp folder you will need to refresh your file list in FileZilla to see any changes. Right-click on the ‘/’ in the right pane and select ‘Refresh’ whenever you make changes to the ftp folder.
If you wanted to leave the ‘VOLUMES’ line in the dockerfile or simply load the image as normal with ‘FROM metabrainz/docker-anon-ftp’ then the RUN command is simply:

Code:
docker run -d -p 20-21:20-21 -p 65500-65515:65500-65515 -v /tmp:/var/ftp:ro metabrainz/docker-anon-ftp

The command simply maps the local ‘/tmp’ folder as the ftp shared volume. You can change the share when you run the command after building it.
You should have an understanding of creating volumes for your docker images. Have fun!
 

Members online


Top