The challenge of file access from Docker containers
As someone used to traditional virtual machines, navigating the peculiarities of Linux and Dockers can be a challenge. I am used to being able to copy files between machines, so whenever I want to transfer a file from a Docker container, I often struggle to know what to do.
This happened again this week when I wanted to backup my Wiki.js config files. When you run the backup, you nominate a local folder. The challenge is then gaining access to those files. I spent an hour or two trawling Google search results trying to understand how I could access these files, but the results all too often assumed prior knowledge that I simply did not possess.
How do you find the right answer when you don't fully understand the question?
In the end, I managed to pick up enough pieces of information to come up with a solution that I will now share. This is mainly so I remember myself when I come to do this next time, but I hope that someone else in a similar position will stumble across it and hopefully spare themselves some pain. I am no pro at Docker or Linux having 'grown up' in the Windows world, so if anyone has any tips or suggestions that would make this easier, I am all ears!
What I did was use the docker cp
command to copy the files from within the container into a directory I could then access via WinSCP. The base command is as follows.
docker cp <containerID>:/file/path/within/container/ /host/path/target/
Getting your container ID is easy if you use Portainer to manage your containers. Open the specific container and the ID is displayed at the top in the 'Container status' box. I assume any Docker management tool will prominently display the ID, but as I have never used anything other than the CLI and Portainer, I cannot say for sure.
If you use the CLI, getting the ID is also pretty easy. Use the below command, replacing containerName
with the name of your container.
docker ps -aqf "name=<containerName>"
Now that you have the container ID, you can confirm that the files have been backed up by Wiki.js by going back into the container in Portainer and going into the console. For my install, backups are saved to '.data/backups', so browse into that directory to confirm that the files are present. Run the command pwd
to get the full path, which you'll soon need.
You're now ready to copy the files from the Docker container to a location you can easily access. Going back to our base docker cp
command, we have the container ID and the path to the files we wish to access. Now use mkdir
to create a directory in your home directory to act as a save location. For me, I did cd ~
to make sure I was in my home directory and then mkdir wiki.js
.
Now it is time to copy. Going back to the base command, input your container ID, Docker container backup save file path, and the location where you wish to copy the files.
docker cp <containerID>:/wiki/backups/ /home/user/wiki.js/
Make sure that the file copy worked as expected and then you're ready to use something like WinSCP or rsync to move the files wherever you want.
Subscribe to my newsletter
Read articles from Firmware Bytes directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by