I am trying to build a backup and restore solution for the Docker containers that we work with.
I have Docker base image that I have created, ubuntu:base
There are good answers, but too specific. I find out docker ps
is good way to get container id you're interested in. Then do
mount | grep <id>
to see where the volume is mounted. That's
/var/lib/docker/devicemapper/mnt/<id>/rootfs/
for me, but it might be a different path depending on the OS and configuration. Now simply copy files to that path.
Using -v
is not always practical.
My favorite method:
CONTAINER_ID=$(docker ps | grep <string> | awk '{ print $1 }' | xargs docker inspect -f '{{.Id}}')
mv -f file.txt /var/lib/docker/devicemapper/mnt/$CONTAINER_ID/rootfs/root/file.txt
or
mv -f file.txt /var/lib/docker/aufs/mnt/$CONTAINER_ID/rootfs/root/file.txt
The best way for copying files to the container I found is mounting a directory on host using -v
option of docker run command.
I just started using docker to compile VLC, here's what you can do to copy files back and forth from containers:
su -
cd /var/lib/docker
ls -palR > /home/user/dockerfilelist.txt
Search for a familiar file in that txt and you'll have the folder, cd to it as root and voila! copy all you want.
There might be a path with "merged" in it, I guess you want the one with "diff" in it.
Also if you exit the container and want to be back where you left off:
docker ps -a
docker start -i containerid
I guess that's usefull when you didn't name anything with a command like
docker run -it registry.videolan.org:5000/vlc-debian-win64 /bin/bash
Sure the hacker method but so what!
Create a new dockerfile and use the existing image as your base.
FROM myName/myImage:latest
ADD myFile.py bin/myFile.py
Then build the container:
docker build .
This is a direct answer to the question 'Copying files from host to Docker container' raised in this question in the title.
Try docker cp
. It is the easiest way to do that and works even on my Mac. Usage:
docker cp /root/some-file.txt some-docker-container:/root
This will copy the file some-file.txt
in the directory /root
on your host machine into the Docker container named some-docker-container
into the directory /root
. It is very close to the secure copy syntax. And as shown in the previous post, you can use it vice versa. I.e., you also copy files from the container to the host.
And before you downlink this post, please enter docker cp --help
. Reading the documentation can be very helpful, sometimes...
If you don't like that way and you want data volumes in your already created and running container, then recreation is your only option today. See also How can I add a volume to an existing Docker container?.