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.

8
  • 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? Commented May 15, 2019 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. Commented May 15, 2019 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 Commented May 15, 2019 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? Commented May 15, 2019 at 10:46
  • How did you start dummy container? It should have entrypoint sleep 1000 or something Commented May 15, 2019 at 10:49

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.