dotnet / Docker.DotNet

:whale: .NET (C#) Client Library for Docker API
https://www.nuget.org/packages/Docker.DotNet/
MIT License
2.26k stars 382 forks source link

Mounting a specific host directory #226

Open rmacri opened 7 years ago

rmacri commented 7 years ago

I can mount a volume in a container by including in Config:

Volumes = new Dictionary<string,object>
{  { "/stage", new object() } }

but is there any way to replicate mounting a specific directory from the docker host like the docker client can using: docker run -it -v /home/files:/stage ubuntu /bin/bash

I've tried passing a MountPoint as the object but its ignored. I can retrieve the MountPoint using InspectContainerAsync() so guessing that is all its intended for.

The host is ubuntu 16.04 running docker 1.12.6. I'm running my test under Visual Studio 2017 using NetCore v2.0.0preview2 and Docker.DotNet v2.124.3

ikkentim commented 7 years ago

Set the mounts in the ContainerSpec when you create the container:

  Mounts = new List<Mount>
                            {
                                new Mount
                                {
                                    Source =storagePath,
                                    Target = "/data",
                                    Type = "bind"
                                }
                            },
galvesribeiro commented 7 years ago

Just a quick note... Remember that if you are using Swarm, mounting a path like that, will require that you have this mount point on all Swarm nodes otherwise, it will not start the Task and you will see 0/X in your docker service ls. It is not meant to be used in a distributed system.

zafields commented 7 years ago

@rmacri It looks like you can update Docker.DotNet.Models.CreateContainerParameters.HostConfig.Bind property, with a list of volumes you wish to mount.

The HostConfig class appears to have been generated from the Docker API.

stesee commented 4 years ago

As @zafields suggests:

var response = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters
      {
        Image = "imagename",
        HostConfig = new HostConfig
        {
          Binds = new[] { @"c:/temp:/data" }
        }
      });
      _containerId = response.ID;

      await _dockerClient.Containers.StartContainerAsync(_containerId, new ContainerStartParameters());

is the equivalent to the cli docker run -v c:/temp:/data

luisgepeto commented 3 years ago

Thanks. Passing the volume argument inside the HostConfig.Binds property did the trick for me. Just wanted to point out that this is specially useful when the application that uses the Docker API does not have permissions to access the docker file system. In my case Docker is being controlled by a Service Fabric application, so changing this made it work.

zakzak1978 commented 2 years ago

Can I add a bind mount to an existing container ? I ran into this requirement, where I have to create a bind mount for each folder I create inside the container. I was looking at UpdateConfig model class, it seems to not have option to add extra mounts. I was thinking of using "UpdateContainerAsync" API.

All I want is stop the existing container, update the mount config with new mounts, and start the container again. Is it possible ? Any information would be helpful.