To copy a file or folder from your Windows device into a Dev Container running on WSL2 ( Windows Subsystem for Linux 2 ) using Docker, use the WSL command line to execute the code below:
docker cp "/mnt/<drive letter>/source/folder/My First Copy Command/foldername" <CONTAINER ID>:/workspace/copy/destination/folder
This process assumes you're using Docker to run your containers, that you have WSL set up on your device, and that you have your remotely connected through VS Code (Visual Studio Code) container running.
You may have tried to run this Docker copy command before, and gotten copying between containers is not supported
while running a copy command from a Windows stlye path like C:\source\folder
, or such file or directory
while guessing it may be a path formatting issue.
In WSL, your drives and folders in Windows are exposed to the Linux subsystem, with a drive like C:\
instead becoming /mnt/c/
.
Opening up the WSL terminal, whether using WSL directly or something like Windows Terminal, we can navigate to the Windows drives and folders from linux using commands like CD
to change directory, LS
to list the contents, and PWD
to print the full path of the current directory.
To find the content I downloaded and want to copy to my running Docker Devcontainer, I used the CD command in the WSL terminal to navigate to the right folder. This looks like CD /mnt/e/training/source
, although if your folder name has spaces, your navigation may instead look like:
CD "/mnt/e/training/source/My First Copy Command"`
After getting the path of the content we want to copy, next we need to identify the ID of the Devcontainer, and the folder path inside the container where we want to copy our content.
To get the Docker container ID, in your WSL terminal, run
docker ps
to provide a list of running containers. You may see multiple listed, and hopefully you've given your Devcontainer a meaningful name.
If you want to rename your container, update the devcontainer.json
file property of name
. You can also rename the service
property from app
, but make a corresponding change in docker-compose.yml
as well under services:
. After making these changes, press F1 to open the Command Palette, and run Remote-Containers: Rebuild Container
to recreate the container with the new name. In my experience this has not dropped any files that were not committed to source control. Other content that you may have installed in your container that are not defined as part of the image build may or may not be preserved.
Once you have gotten your container ID, determine the final location you want to copy your content to inside your container. Unless you've changed the default cofiguration of your container, the path will look like /workspace/<your location>
.
Now create the final command you'll run for your copy statement. We'll use Docker's copy command to copy from a Source to a Destination, like
docker cp </mnt/c/source/path> <container_id:/workspace/destination/path>
Mine ended up looking like this:
docker cp "/mnt/e/Training/Vue/Vue - The Complete Guide/gs-03-rebuilding-the-app-with-vue/gs-03-rebuilding-the-app-with-vue" 6392bf562adf:/workspace/training
I avoided having to worry about the formatting of the line endings differing between Windows and Linux by having a .gitattributes
file defined to auto format the text.
# Set the default behavior for line endings
* text=auto
If this is not accounted for either in your original files or through correcting it when the files are loaded, you may run into issues. Stack Overflow has some good information on the differences in how Windows and Linux / Unix derived systems (*nix) handle line termination.