1

I want to create docker volume and add directory and files into it without creating extra container/images or within minimal time. That thing would go into script, so interactions like -it bash won't do.

I can copy files with:

docker container create --name dummy -v myvolume:/root hello-world
docker cp c:\myfolder\myfile.txt dummy:/root/myfile.txt
docker rm dummy

How do I create an empty dir?:

attempt 1

mkdir lol; docker cp ./lol dummy:/root/lol # cannot copy directory

attempt 2

docker commit [CONTAINER_ID] temporary_image
docker run --entrypoint=bash -it temporary_image

This thing requires to pull image with bash.

deathangel908
  • 8,601
  • 8
  • 47
  • 81
  • So you're trying to copy local files into a running docker container's volume? Why not copy it in during build using your dockerfile? – Shardj May 15 '19 at 10:32
  • Docker container should be built without those files. Each user has different settings, and would run creating volume script copying own files. The image from Dockerfile would go to public dockerhub and can't contain sensitive data. Those data would be created by user into volume and will be mounted to container. – deathangel908 May 15 '19 at 10:39
  • If you just want to create an empty directory, why not use `docker exec dummy mkdir /root/lol/`, I just tried this on a container of mine and it worked no problem – Shardj May 15 '19 at 10:45
  • I also just tried your "attempt 1" which worked fine for me. "cannot copy directory" isn't really a proper error message, what does running `docker logs dummy` give you? – Shardj May 15 '19 at 10:46
  • How did you start dummy container? It should have entrypoint `sleep 1000` or something – deathangel908 May 15 '19 at 10:49
  • It's just a regular alpine container, entrypoint/cmd running bash, no sleep 1000, why would you want that in there? – Shardj May 15 '19 at 10:52
  • Why not use an ordinary bind mount `-v /host/path:/container/path`? Especially for pushing in configuration that’s a very reasonable use of it. – David Maze May 15 '19 at 12:20
  • @DavidMaze https://stackoverflow.com/a/49173474/3872976 . Only a few files are copied another are created within running container, e.g. `mysql`, `redis` data, `uploaded files` to server, `migrations` etc – deathangel908 May 15 '19 at 12:32

1 Answers1

0

this worked for me, you can try it out. I am doing the exact thing running from script

VOL_NAME=temp_vol
docker volume create $VOL_NAME
docker run -v $VOL_NAME:/root --name helper busybox true
mkdir tmp
docker cp tmp helper:/root/dir0
docker cp tmp helper:/root/dir1
docker cp tmp helper:/root/dir2
rm -rf tmp
docker rm helper
# check volume
sudo ls /var/lib/docker/volumes/$VOL_NAME/_data
dir0 dir1 dir2
Thesane
  • 1,358
  • 10
  • 18