Golang docker库-装载主机目录卷

Golang docker库-装载主机目录卷,docker,go,Docker,Go,我如何执行以下等效操作: docker run -v /host/path:/container/path image:tag 使用官方docker客户端包从Go开始 我在的HostOption和ConfigOption结构中尝试了不同的and选项,但无法完全理解 特别是,Volumes成员(类型为map[string]struct{})特别难以理解如何使用,而且我找不到任何关于结构中应该存在哪些值的文档 演示我的问题的代码: 主程序包 进口( “上下文” “github.com/docker

我如何执行以下等效操作:

docker run -v /host/path:/container/path image:tag
使用官方docker客户端包从Go开始

我在的HostOption和ConfigOption结构中尝试了不同的and选项,但无法完全理解

特别是,
Volumes
成员(类型为
map[string]struct{}
)特别难以理解如何使用,而且我找不到任何关于结构中应该存在哪些值的文档

演示我的问题的代码:

主程序包
进口(
“上下文”
“github.com/docker/docker/api/types”
“github.com/docker/docker/api/types/container”
//“github.com/docker/docker/api/types/mount”
“github.com/docker/docker/client”
“github.com/docker/docker/pkg/stdcopy”
“日志”
“操作系统”
“路径/文件路径”
)
func getThisDir()字符串{
dir,err:=filepath.Abs(filepath.dir(os.Args[0]))
如果错误!=零{
恐慌(错误)
}
返回目录
}
func main(){
log.Println(“创建客户端”)
cli,err:=client.NewClientWithOpts(client.FromEnv)
如果错误!=零{
恐慌(错误)
}
图片:=“ubuntu:18.04”
主机路径:=getThisDir()
containerPath:=“/host\u文件”
log.Printf(“图像:%s\n”,图像)
log.Printf(“主机路径:%s\n”,主机路径)
log.Printf(“容器路径:%s\n”,容器路径)
ctx:=context.Background()
cli.NegotiateAPIVersion(ctx)
log.Println(“创建容器”)
var cont container.containerCreatedBody
如果继续,err=cli.ContainerCreate(
context.Background(),
&container.Config{
图像:图像,
入口点:[]字符串{“/bin/bash”、“-c”、“ls-la”+containerPath},
卷:映射[字符串]结构{}{
主机路径:{},
},
},
&container.HostConfig{
运行时:“runsc”,
/*
挂载:[]挂载。挂载{
坐骑,坐骑{
类型:mount.TypeVolume,
资料来源:hostPath,
目标:containerPath,
},
},
*/
},
无
“测试容器”,
);呃!=零{
恐慌(错误)
}
延迟函数(){
log.Println(“清理”)
如果错误:=cli.ContainerRemove(
context.Background(),
续,
types.ContainerRemoveOptions{
原力:没错,
RemoveVolumes:对,
},
);呃!=零{
恐慌(错误)
}
}()
log.Println(“起始容器”)
如果err=cli.ContainerStart(
context.Background(),
续,
类型。容器开始选项{},
);呃!=零{
恐慌(错误)
}
log.Println(“等待容器退出”)
waitOk,waitErr:=cli.ContainerWait(
ctx,
续,
container.WaitConditionNotRunning,
)
挑选{

case删除
设置,添加
装载
并将
类型
更改为
装载。TypeBind
使其工作:

2019/04/16 20:53:18 Creating client
2019/04/16 20:53:18              Image: ubuntu:18.04
2019/04/16 20:53:18          Host Path: /home/user/go/src/test
2019/04/16 20:53:18     Container Path: /host_files
2019/04/16 20:53:18 Creating container
2019/04/16 20:53:18 Starting container
2019/04/16 20:53:19 Waiting for container to exit
2019/04/16 20:53:19 Container exited normally!
2019/04/16 20:53:19 Should be done!
2019/04/16 20:53:19 Container output:
total XXXX
drwxr-xr-x  7 1000 1000     4096 Apr 17 03:51 .
drwxr-xr-x 34 root root     4096 Apr 17 03:53 ..
-rw-r--r--  1 1000 1000    10390 Apr 16 12:16 Gopkg.lock
-rw-r--r--  1 1000 1000     1021 Apr 16 12:16 Gopkg.toml
-rwxr-xr-x  1 1000 1000 12433827 Apr 17 03:53 container
-rw-r--r--  1 1000 1000     2421 Apr 17 03:51 container.go
2019/04/16 20:53:19 Cleaning up
2019/04/16 20:53:18 Creating client
2019/04/16 20:53:18              Image: ubuntu:18.04
2019/04/16 20:53:18          Host Path: /home/user/go/src/test
2019/04/16 20:53:18     Container Path: /host_files
2019/04/16 20:53:18 Creating container
2019/04/16 20:53:18 Starting container
2019/04/16 20:53:19 Waiting for container to exit
2019/04/16 20:53:19 Container exited normally!
2019/04/16 20:53:19 Should be done!
2019/04/16 20:53:19 Container output:
total XXXX
drwxr-xr-x  7 1000 1000     4096 Apr 17 03:51 .
drwxr-xr-x 34 root root     4096 Apr 17 03:53 ..
-rw-r--r--  1 1000 1000    10390 Apr 16 12:16 Gopkg.lock
-rw-r--r--  1 1000 1000     1021 Apr 16 12:16 Gopkg.toml
-rwxr-xr-x  1 1000 1000 12433827 Apr 17 03:53 container
-rw-r--r--  1 1000 1000     2421 Apr 17 03:51 container.go
2019/04/16 20:53:19 Cleaning up


我唯一不能100%确定的是
docker-v/host/path:/container/path image:tag
是否实际上是绑定挂载。

删除
设置,添加
挂载
并将
类型
更改为
挂载。TypeBind
成功:

2019/04/16 20:53:18 Creating client
2019/04/16 20:53:18              Image: ubuntu:18.04
2019/04/16 20:53:18          Host Path: /home/user/go/src/test
2019/04/16 20:53:18     Container Path: /host_files
2019/04/16 20:53:18 Creating container
2019/04/16 20:53:18 Starting container
2019/04/16 20:53:19 Waiting for container to exit
2019/04/16 20:53:19 Container exited normally!
2019/04/16 20:53:19 Should be done!
2019/04/16 20:53:19 Container output:
total XXXX
drwxr-xr-x  7 1000 1000     4096 Apr 17 03:51 .
drwxr-xr-x 34 root root     4096 Apr 17 03:53 ..
-rw-r--r--  1 1000 1000    10390 Apr 16 12:16 Gopkg.lock
-rw-r--r--  1 1000 1000     1021 Apr 16 12:16 Gopkg.toml
-rwxr-xr-x  1 1000 1000 12433827 Apr 17 03:53 container
-rw-r--r--  1 1000 1000     2421 Apr 17 03:51 container.go
2019/04/16 20:53:19 Cleaning up
2019/04/16 20:53:18 Creating client
2019/04/16 20:53:18              Image: ubuntu:18.04
2019/04/16 20:53:18          Host Path: /home/user/go/src/test
2019/04/16 20:53:18     Container Path: /host_files
2019/04/16 20:53:18 Creating container
2019/04/16 20:53:18 Starting container
2019/04/16 20:53:19 Waiting for container to exit
2019/04/16 20:53:19 Container exited normally!
2019/04/16 20:53:19 Should be done!
2019/04/16 20:53:19 Container output:
total XXXX
drwxr-xr-x  7 1000 1000     4096 Apr 17 03:51 .
drwxr-xr-x 34 root root     4096 Apr 17 03:53 ..
-rw-r--r--  1 1000 1000    10390 Apr 16 12:16 Gopkg.lock
-rw-r--r--  1 1000 1000     1021 Apr 16 12:16 Gopkg.toml
-rwxr-xr-x  1 1000 1000 12433827 Apr 17 03:53 container
-rw-r--r--  1 1000 1000     2421 Apr 17 03:51 container.go
2019/04/16 20:53:19 Cleaning up

我唯一不能100%确定的是,
docker-v/host/path:/container/path image:tag
是否实际是绑定装载。

是的,它是绑定装载(因为它引用特定的主机端路径),而不是命名卷装载。是的,它是绑定装载(因为它引用特定的主机端路径)而不是命名卷装载。