Siva
Siva

Reputation: 65

Copying a file from container to locally by using volume mounts

Trying to copy files from the container to the local first

So, I have a custom Dockerfile, RUN mkdir /test1 && touch /test1/1.txt and then I build my image and I have created an empty folder in local path /root/test1

and docker run -d --name container1 -v /root/test1:/test1 Image:1

I tried to copy files from containers to the local folder, and I wanted to use it later on. but it is taking my local folder as a preceding and making my container empty.

Could you please someone help me here?

For example, I have built my own custom Jenkins file, for the first time while launching it I need to copy all the configurations and changes locally from the container, and later if wanted to delete my container and launch it again don't need to configure from the scratch.

Thanks,

Upvotes: 0

Views: 989

Answers (2)

Casper Dijkstra
Casper Dijkstra

Reputation: 1885

The relatively new --mount flag replaces the -v/--volume mount. It's easier to understand (syntactically) and is also more verbose (see https://docs.docker.com/storage/volumes/).

You can mount and copy with:

docker run -i \
           --rm \
           --mount type=bind,source="$(pwd)"/root/test1,target=/test1 \
            /bin/bash << COMMANDS
                cp <files> /test1
            COMMANDS

where you need to adjust the cp command to your needs. I'm not sure if you need the "$(pwd)" part.

Upvotes: 1

Robert Butler
Robert Butler

Reputation: 11

Off the top, without testing to confirm, i think it is

docker cp container1:/path/on/container/filename /path/on/hostmachine/

EDIT: Yes that should work. Also "container1" is used here because that was the container's name provided in the example

In general it works like this

container to host

docker cp containername:/containerpath/ /hostpath/ 

host to container

docker cp /hostpath/ containername:/containerpath/

Upvotes: 0

Related Questions