簡體   English   中英

Dockerfile:將目錄從 Windows 主機復制到 docker 容器

[英]Dockerfile: Copy directory from Windows host to docker container

我想構建一個 Docker 鏡像,包括我的自定義 Powershell 模塊。 因此,我使用 Microsoft 的microsoft/powershell:latest圖像,我想從中創建自己的圖像,其中包括我的 psm1 文件。

對於簡單的測試,我有以下 docker 文件:

FROM microsoft/powershell:latest
RUN mkdir -p /tmp/powershell
COPY  C:/temp/somedirectory /tmp/powershell

我想將 C:\\temp\\somedirectory 中包含的文件復制到 docker linux 容器。 構建圖像時,我收到以下錯誤:

C:\\temp\\docker_posh> docker build --rm -f Dockerfile -t docker_posh:latest 。

將構建上下文發送到 Docker 守護進程 2.048kB

第 1/3 步:從 microsoft/powershell:latest ---> 9654a0b66645

第 2/3 步:運行 mkdir -p /tmp/powershell ---> 使用緩存 ---> 799972c0dde5

步驟 3/3 : COPY C:/temp/somedirectory /tmp/powershell COPY failed: stat /var/lib/docker/tmp/docker-builder566832559/C:/temp/somedirectory: no such file or directory

我當然知道 Docker 說我找不到文件/目錄。 因此我也嘗試過C:/temp/somedirectory/. C:/temp/somedirectory/*C:\\\\temp\\\\somedirectory\\\\作為 Dockerfile 中的替代源路徑 -> 結果:它們都不起作用。

docker version
    Client:
       Version:       17.12.0-ce
       API version:   1.35
       Go version:    go1.9.2
       Git commit:    c97c6d6
       Built: Wed Dec 27 20:05:22 2017
       OS/Arch:       windows/amd64

    Server:
       Engine:
       Version:      17.12.0-ce
       API version:  1.35 (minimum version 1.12)
       Go version:   go1.9.2
       Git commit:   c97c6d6
       Built:        Wed Dec 27 20:12:29 2017
       OS/Arch:      linux/amd64
       Experimental: true

如何通過 Dockerfile 復制包含子文件夾和文件的文件夾?

構建 docker 鏡像時,您不能復制構建上下文之外的文件。 構建上下文是您為 docker build 命令指定的路徑。 在指導的情況下

C:\temp\docker_posh> docker build --rm -f Dockerfile -t docker_posh:latest .

. 指定構建上下文是C:\\temp\\docker_posh 因此無法訪問C:/temp/somedirectory 您可以將 Dockerfile 移動到 temp,也可以在C:\\temp下運行相同的構建命令。 但請記住修復 Dockerfile 指令以使路徑相對於構建上下文。

如果要將某些內容從主機復制到正在運行的 docker 容器,則可以使用docker cp命令,例如:

docker cp [OPTIONS] CONTAINER_NAME:CONTAINER_SRC_PATH DEST_PATH
docker cp [OPTIONS] SRC_PATH CONTAINER_NAME:CONTAINER_DEST_PATH

選項:

--archive      -a   Archive mode (copy all uid/gid information)
--follow-link  -L   Always follow symbol link in SRC_PATH

如果您不想使用選項,則可以忽略它們。

我發現這個“功能”非常不方便,只是編寫了我自己的構建腳本,將我關心的所有內容復制到一個新的臨時文件夾中,然后在適當的上下文中構建 docker 鏡像。 這是一個 Powershell 腳本,適用於發現此方法有用的任何人。

文件夾結構(隨意更改)對我來說是這樣的:

lib\somefile1.ps1
lib\somefile2.ps1
dockerfolder\Dockerfile
dockerfolder\mybuilderscript.ps1
my-main-file.ps1

這是腳本:

# Docker Image Tag Name
$tagname = "myimage"

#   Generate temp directory
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
$dockerBuildDirectory = New-Item -ItemType Directory -Path (Join-Path $parent $name)

#   Copy things we care about from the parent repo into our build dir

# Copy source folder that has our Dockerfile (and this script) into a folder called "dockerworking" in the temp build directory
Copy-Item -Recurse "../dockerfolder" $dockerBuildDirectory/dockerworking

# Copy directories above our current folder into the dockerworking directory
Copy-Item -Recurse "../lib" $dockerBuildDirectory/dockerworking

# Copy the main script into the working directory
Copy-Item -Recurse "../my-main-file.ps1" $dockerBuildDirectory/dockerworking

# Let the user know where these files are for any troubleshooting that comes up
Write-Host -ForegroundColor Green "Docker files stage at $dockerBuildDirectory"

#   Create the docker image in the "dockerworking" folder where everything resides
docker build --no-cache --tag $tagname $dockerBuildDirectory/dockerworking

Dockerfile 具有“本地”COPY 命令(並且所有文件都預先放置到正確的位置),因此 Docker 很高興。

Dockerfile 片段:

FROM mcr.microsoft.com/windows/servercore:ltsc2019

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]


RUN New-Item -Type Directory -Path "c:\scripts" | Out-Null
COPY "lib" "c:\scripts"
COPY "my-main-file.ps1" "c:\scripts"

ENTRYPOINT ["powershell", "-NoProfile", "-Command", "c:\\scripts\\my-main-file.ps1"]

暫無
暫無

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

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