Docker.DotNet icon indicating copy to clipboard operation
Docker.DotNet copied to clipboard

Mounting a specific host directory

Open rmacri opened this issue 7 years ago • 6 comments

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

rmacri avatar Jul 11 '17 13:07 rmacri

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

  Mounts = new List<Mount>
                            {
                                new Mount
                                {
                                    Source =storagePath,
                                    Target = "/data",
                                    Type = "bind"
                                }
                            },

ikkentim avatar Jul 11 '17 18:07 ikkentim

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.

galvesribeiro avatar Jul 11 '17 19:07 galvesribeiro

@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.

zafields avatar Sep 19 '17 22:09 zafields

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

stesee avatar Sep 09 '20 17:09 stesee

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.

luisgepeto avatar Jan 15 '21 18:01 luisgepeto

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.

zakzak1978 avatar Feb 22 '22 11:02 zakzak1978