Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating a volume using a container does not work in Docker on Windows #40771 #369

Closed
windymindy opened this issue May 30, 2023 · 14 comments
Assignees
Labels
documentation Need improvements or additions to docs question Further information is requested

Comments

@windymindy
Copy link

Hello.
Could this possibly be in scope of this project?
moby/moby#40771

When using Docker Desktop in the Windows Containers mode named volumes render directories empty.

I found it very confusing that named volumes prepopulating behaviour is Linux only.
And I did not seem to find it mentioned anywhere in the documentation.

Thank you.

@windymindy windymindy added the question Further information is requested label May 30, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the triage New and needs attention label May 30, 2023
@zylxjtu
Copy link

zylxjtu commented Jun 2, 2023

There is already a document link for this in the original thread?

https://docs.docker.com/engine/reference/builder/#notes-about-specifying-volumes

@ntrappe-msft
Copy link
Contributor

FYI @ritikaguptams

@ntrappe-msft ntrappe-msft added documentation Need improvements or additions to docs and removed triage New and needs attention labels Jun 8, 2023
@ritikaguptams
Copy link
Contributor

ritikaguptams commented Jun 21, 2023

@windymindy
Copy link
Author

Hi @windymindy, These links might be helpful: https://learn.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/persistent-storage#named-volumes and https://techcommunity.microsoft.com/t5/itops-talk-blog/containers-basics-providing-persistent-storage-to-containers/ba-p/1200050 . If that doesn't help, could you share the steps and expected vs actual behavior you are seeing?

Hey.
Ty for the links.

The steps are described in
moby/moby#40771
and
https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container

The idea, once again, is that one is able to pick out data from the container. Maybe, override some of it, for instance a configuration file, and put it back. This is how named docker volumes are mostly used on linux.

But as the note warns (thanks for finding it), the target inside the container should be empty.
https://docs.docker.com/engine/reference/builder/#notes-about-specifying-volumes

The reason I made this ticket is that I was wondering if there is some major limitation that prevents from implementing the behaviour.
Or perphaps there is a place for a storage plugin, that would do exactly that akin the local-persist plugin.

@microsoft-github-policy-service
Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

3 similar comments
@microsoft-github-policy-service
Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

@microsoft-github-policy-service
Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

@microsoft-github-policy-service
Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

@microsoft-github-policy-service
Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

3 similar comments
Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

Copy link
Contributor

This issue has been open for 30 days with no updates.
@zylxjtu, @ritikaguptams, please provide an update or close this issue.

@ntrappe-msft
Copy link
Contributor

ntrappe-msft commented Feb 5, 2024

I've added "pre-populating named volumes" to our backlog of feature requests. If we have a chance in the future, we'll try to incorporate this ask into our plans. Since we don't have a current timeline to share, I'll close this Issue.

@ntrappe-msft
Copy link
Contributor

ntrappe-msft commented Feb 5, 2024

To provide additional context to future viewers, you can pre-populate volumes in both Linux and Windows Docker containers but you'll have to approach them in different ways.

Linux Docker Containers

For Linux Docker containers, we can create a volume, use a temporary container to populate it, and hook it up to the container. That, or any other container, can have access to the contents of the volume. We do this through the following commands:

  1. Create a volume.
docker volume create my-vol
  1. Check that the volume exists and lives in .../docker/volumes.
docker volume ls
docker volume inspect my-vol
  1. Create a file a.txt in the folder foo/ on our Desktop.
cd foo
echo hi > a.txt
  1. Populate the contents of the volume using a temporary container. Here, we're populating my-vol with the contents of foo/.
tar -C /Users/Desktop/foo -c -f- . | docker run --rm -i -v my-vol:/data alpine tar -C /data -xv -f-

Important

Make sure you specify my-vol:/data not my-vol. my-vol is the name of the volume that lives on the host. :/data is the mount point and where in the container's file system you want to access the data in my-vol.

  1. Run the container with that volume. We're using the alpine image in this case.
docker run -v my-vol:/data -it --name bar alpine

Note

Make sure Docker Desktop is set to "Use Linux Containers" and you've pulled the image alpine.

  1. In the container, check the contents of data/ and you should see a.txt.
$> data ls

a.txt

Note

If data/ has no files, either you have not pre-populated the volume or you did not provide the correct mount point. On Docker Desktop, you can check the contents of my-vol. If it has a.txt, then check your container start up code. If it's empty, try populating the volume again.

Windows Docker Containers

For Windows Docker containers, we can only create an empty volume and attach it to a container. The container can populate it but it does not start out pre-populated like Linux containers can do. There is a way to emulate this behavior however.

  1. Create a volume.
docker volume create my-vol
  1. Check that the volume exists and lives in ...\\docker\\volumes.
docker volume ls
docker volume inspect my-vol
  1. Create a file a.txt in the folder foo\ on our Desktop.
cd foo
echo hi > a.txt
  1. Here is the magic. We'll use a temporary container to copy over the files from foo\ into my-vol.
docker run --rm -v C:\Users\Desktop\foo:C:\foo -v my-vol:C:\data -it mcr.microsoft.com/windows/servercore:ltsc2022

Note

C:\Users\Desktop\foo has all the files we want for our volume. We'll mount it to C:\foo in the temporary container. my-vol is the name of our volume and C:\data is the mount point.

  1. Inside the container, check that foo\ and data\ exist.
$> dir

02/05/2024    02:26 PM      <DIR>      data
02/05/2024    01:51 PM      <DIR>      foo
... other folders
  1. Now, copy all the contents of foo\ into data\. You can then delete foo\.
$> Xcopy C:\foo C:\data /E /H /C /I

1 File(s) copied
  1. Shut down the container.
$> exit
  1. Now, run the container you wanted with the volume my-vol.
docker run -v my-vol:C:\data -it mcr.microsoft.com/windows/servercore:ltsc2022
  1. Inside the container, you should see the folder data\ with all the files we wanted.
$> dir

02/05/2024    02:26 PM      <DIR>      data

$> dir data

02/05/2024    02:23 PM                 a.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Need improvements or additions to docs question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants