'How to copy files from local machine to docker container on windows

I have to import data files from a user local file C:/users/saad/bdd to a docker container (cassandra), I didn't find how to proceed using docker commands. I'm working on windows 7.



Solution 1:[1]

Use docker cp.

docker cp c:\path\to\local\file container_name:/path/to/target/dir/

If you don't know what's the name of the container, you can find it using:

docker ps --format "{{.Names}}"

Solution 2:[2]

When using docker toolbox, there seems to be another issue related to absolute paths.

I am communicating with the containers using the "Docker Quickstart Teminal" which essentially is a MINGW64 environment.

If I try to copy a file with an absolute path to a container, I receive the error message.

$ docker cp /d/Temp/my-super-file.txt container-name:/tmp/
copying between containers is not supported

If I use a relative path, it simply works.

$ cd /d/
$ docker cp Temp/my-super-file.txt container-name:/tmp/

P.S.: I am posting this as an answer because of missing reputation for a comment.

Solution 3:[3]

If you are using docker-toolbox on windows, use the following syntax

docker cp /C/Users/Saad/bdd-restaurants cassandra:/var/lib/docker/containers

Solution 4:[4]

It is not as straight-forward when using docker toolbox. Because docker toolbox has only access to C:\Users\ folder and there is a Oracle Virtual Box Manager in between, when you do get to copy the folder it is not directly copied to the container but instead to a mounted volume handle by Oracle VM machine. Like so:

/mnt/sda1/var/lib/docker/volumes/19b65e5d9f607607441818d3923e5133c9a96cc91206be1239059400fa317611/_data

How I got around this is just editing my DockerFile:

FROM cassandra:latest

ADD cassandra.yml /etc/cassandra/
ADD import.csv /var/lib/cassandra/
EXPOSE 9042

And building it.

Solution 5:[5]

Use this command will help to copy files from host machine to docker container.

docker cp c:\abc.doc <containerid> :C:\inetpub\wwwroot\abc.doc

Solution 6:[6]

Simple way:

From DockerContainer To LocalMachine

$docker cp containerId:/sourceFilePath/someFile.txt C:/localMachineDestinationFolder

From LocalMachine To DockerContainer

$docker cp C:/localMachineSourceFolder/someFile.txt containerId:/containerDestinationFolder

Solution 7:[7]

if you are trying to copy file from windows to an EC2 instance use the following in cmd (Putty enabled):

pscp -i "D:\path_to_ppk_key" c:\file_name ubuntu@**.***.**.*:/home/ubuntu/file 

Then you can copy to docker in EC2 using

docker cp /home/ubuntu/file_name Docker_name:/home/ 

Solution 8:[8]

You can also use volume to mount the file to container on the run:

docker run -v /users/saad/bdd:/myfiles/tmp/

Solution 9:[9]

For those who are using WSL (Windows Subsystem for Linux), Docker, and DevContainers from VSCode (Visual Studio Code), I was able to make this work by using the WSL command line.

docker cp "/mnt/<drive letter>/source/My First Copy Command"  <container id>:/workspace/destination/path

I also wrote it up in more detail.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alvaro Carvajal
Solution 2 DwightKendall
Solution 3 Aparna
Solution 4 mel3kings
Solution 5 Pramod Lawate
Solution 6 Imranmadbar
Solution 7 Sincole Brans
Solution 8 ymichajlow
Solution 9 ADataGMan