Docker Community Forums

Share and learn in the Docker community.

Need help mounting a local directory when creating a container with golang SDK

(Yukiromiyatoshi) #1

I would like to mount a local directory when using the golang SDK

I’m following the documentation

resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: “alpine”,
Cmd: []string{“echo”, “hello world”},
}, nil, nil, “”)

Volumes is part of the containerConfig but I’m not sure how to define the source directory (from the local host) to the target directory (on the container)

type Config struct {

Volumes map[string]struct{} // List of volumes (mounts) used for the container


Can someone share an example of how to create a container and mount a local volume?

resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: “alpine”,
Cmd: []string{“echo”, “hello world”},
Volumes: map[string]struct{}{"/opt/": {}}, #???
}, nil, nil, “”)


(Gimil) #2

You should use Binds in the HostConfig.

Volume is designed to work with VolumeFrom. It is just the same thing as “VOLUME” in a Dockerfile. If you create a container A with "Volume"s, then you can create a container B with VolumeFrom A to mount all those "Volume"s in A.

(X4mp) #3

@gimil I guess he was asking for mounting a local directory on an exposed volume.
If I have container A exposing /var/log/ as an volume, what do I need to append to HostConfig.Binds to map my local /var/log/ to the volume in the container?