
Background
I don’t know if this happens to you sometimes, but for testing purposes, I sometimes want to grab certain folders from my Docker container by creating a volume in the “docker-compose.yml” file, for example, so I can easily retrieve them.
Yes, I know there’s the “docker exec” command to get inside the container from the command line, but in some cases I find it isn’t always convenient.
Problem
I don’t know the reason, but some of a container’s folders shouldn’t be put in volumes.
I’m not talking about the security aspect, just the practical aspect.
That is, if you do this with the “var” folder, for example:
volumes:
- ./var:/var
When the container starts, it will indeed create a “var” folder on your server, next to your “docker-compose.yml”.
However, instead of copying all the container’s files to the server, it does things the other way around.
That is, since our “var” folder is newly created, it’s empty and will overwrite the contents of the container’s “var” folder, which consequently prevents it from working normally.
Solution
You can retrieve the container’s folder with this command:
docker cp my_container:/app/myfolder /home/user/myfolder
This is just an example, but suppose I want to retrieve the “/lib/jellyfin” folder, I can do this:
docker cp jellyfin:/lib/jellyfin .
I don’t see the point yet, but you can also do the opposite, that is, copy a folder/file from the server into a container:
docker cp /home/user/myfolder my_container:/app/myfolder