簡體   English   中英

無法使用 Go Docker 引擎 ZDB974238714CA81DE634A7C2Z 的容器在 Docker 中安裝文件夾

[英]Unable to mount a folder in Docker from container with Go Docker Engine API

我正在嘗試使用 Go Docker 引擎 ZDB974238714CA8DE634A7CE1D08 在容器內運行 docker。 我能夠將一個文件夾從主機系統安裝到容器,但只有空目錄被復制到容器內的 docker 中。 如果有任何替代方法,請幫助我。 我正在使用以下命令啟動我的容器。

docker 運行 --rm -v C:\Users\user\source\repos\game:/app/myrepo -v /var/run/docker.sock:/var/run/docker.sock testimage

附上一段代碼。 Go Docker SDK 代碼啟動容器

resp, err := cli.ContainerCreate(ctx, &container.Config{
    Image: "hello-image",
    Cmd:   []string{"ls"}, #the actual cmd would look different
    Tty:   true,
}, &container.HostConfig{
    Binds: []string{
        "/app/myrepo:/myrepo",
    },
}, nil, nil, containername)
if err != nil {
    panic(err)
}

更新了與絕對路徑綁定的代碼

resp, err := cli.ContainerCreate(ctx, &container.Config{
    Image: "hello-image",
    Cmd:   []string{"ls"}, #the actual cmd would look different
    Tty:   true,
}, &container.HostConfig{
    Mounts: []mount.Mount{
        {
            Type:   mount.TypeBind,
            Source: "/app/myrepo",
            Target: "/myrepo",
        },
    },
}, nil, nil, containername)
if err != nil {
    panic(err)
}

正如評論中所討論的,OP 正在容器中運行應用程序。 該應用程序正在連接到主機上的 docker 守護程序(通過共享/var/run/docker.sock )並嘗試創建容器。 問題是請求包含一個掛載點,源是/app/myrepo ,它是一個在容器內有效但在主機上無效的源路徑。

為了幫助理解為什么這是一個問題,您需要考慮如何提出 API 請求。 您的代碼將生成一個 JSON 格式的請求; 這將包括以下內容:

...
"HostConfig": {
   ...
   "Mounts": [
       {
           "Source": "/app/myrepo",
           "Destination": "/myrepo",        
       }
   ]
}

重要的是要注意Source路徑作為字符串傳遞,Docker 守護程序將在主機(例如 windows 框)上下文中解釋這一點。 當它試圖定位請求的路徑 ( /app/myrepo ) 時,它不會找到它,因為該路徑在主機上不存在。 要更正此問題,您需要發送有效路徑,例如

Mounts: []mount.Mount{
   {
      Type:   mount.TypeBind,
      Source: "c:/Users/user/source/repos/game",
      Target: "/myrepo",
    },
}

一個注意事項; 以這種方式訪問 Docker API (綁定掛載/var/run/docker.sock: )很方便,但如果有人獲得對容器的訪問權限,那么他們也可以獲得對所有容器的完全控制權(因為他們可以訪問 ZC5FD214CDD22E3B4B27 API)。 您可能需要考慮使用代理(例如)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM