Windows 10의 Docker에서 현재 디렉터리를 볼륨으로 탑재
기술
Hyper-V를 통해 Windows 10에서 Docker 버전 1.12.5를 사용하고 있으며 컨테이너 실행 파일을 현재 경로의 명령으로 사용하고 싶습니다. 잘 실행되는 Docker 이미지를 만들었지 만 현재 경로를 마운트하는 데 문제가 있습니다. 아이디어는 docker run --rm [...]
현재 디렉토리에서 시스템 전체에서 사용할 수 있도록 별칭을 만들고 명령을 수행하는 것입니다.
설정
"test"폴더가있는 드라이브 E가 있고 그 안에 "folder on windows host"라는 폴더가있어 명령이 작동하고 있음을 보여줍니다. Dockerfile은 디렉토리를 만들고 /data
VOLUME 및 WORKDIR로 정의합니다.
데 E:\test
PowerShell의 현재 디렉토리로와 절대 경로로 도커 명령을 실행, 나는의 내용을 볼 수 있습니다 E:\test
:
PS E:\test> docker run --rm -it -v E:\test:/data mirkohaaser/docker-clitools ls -la
total 0
drwxr-xr-x 2 root root 0 Jan 4 11:45 .
drwxr-xr-x 2 root root 0 Jan 5 12:17 folder on windows host
문제
절대 표기법이 아닌 현재 디렉토리를 사용하고 싶습니다. 다른 오류 메시지로 인해 볼륨에서 pwd를 사용할 수 없습니다.
($ pwd)로 시도
PS E:\test> docker run --rm -it -v ($pwd):/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error parsing reference: ":/data" is not a valid repository/tag.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
/ ($ pwd) 시도
PS E:\test> docker run --rm -it -v /($pwd):/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error parsing reference: "E:\\test" is not a valid repository/tag.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
\ ´pwd \ ´로 시도
PS E:\test> docker run --rm -it -v ´$pwd´:/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: Invalid bind mount spec "´E:\\test´:/data": invalid mode: /data.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
`pwd`로 시도
PS E:\test> docker run --rm -it -v `$pwd`:/data mirkohaaser/docker-clitools ls -la
C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: create $pwd: "$pwd" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'.
Windows 10의 Docker에서 현재 디렉터리를 볼륨으로 마운트하는 올바른 구문은 무엇입니까?
Windows 명령 줄 ( cmd
)에서 다음과 같이 현재 디렉토리를 마운트 할 수 있습니다.
docker run --rm -it -v %cd%:/usr/src/project gcc:4.9
PowerShell에서는 ${PWD}
현재 디렉터리를 제공하는를 사용 합니다.
docker run --rm -it -v ${PWD}:/usr/src/project gcc:4.9
Linux의 경우 :
docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
크로스 플랫폼
다음 옵션은 PowerShell과 Linux (최소 Ubuntu)에서 모두 작동합니다.
docker run --rm -it -v ${PWD}:/usr/src/project gcc:4.9
docker run --rm -it -v $(pwd):/usr/src/project gcc:4.9
이것은 PowerShell에서 나를 위해 작동합니다.
docker run --rm -v ${PWD}:/data alpine ls /data
Windows 용 Git Bash (ConEmu)의 경우 다음이 나에게 적합합니다 (Docker Windows 컨테이너의 경우).
docker run --rm -it -v `pwd -W`:c:/api microsoft/dotnet:2-runtime
주변 의 역 따옴표 / 역 따옴표에 주의하십시오 pwd -W
!
다른 모든 PWD 변형으로 시도해 보았습니다. "Error response from daemon: invalid volume specification: ..."
업데이트 : 위의 내용은 Docker Windows 컨테이너 용이며 Linux 컨테이너 용은 다음과 같습니다.
docker run --rm -it -v `pwd -W`:/api -p 8080:80 microsoft/aspnetcore:2
다음은 Win10 docker-ce 및 Win7 docker-toolbox와 호환되는 내 것입니다. 당시 las에서 나는 이것을 쓰고 있습니다 :).
c : / 대신 / host_mnt / c를 사용하는 것을 선호하는 것을 알 수 있습니다. 가끔 c : /로 docker-ce Win 10에서 문제가 발생했기 때문입니다.
$WIN_PATH=Convert-Path .
#Convert for docker mount to be OK on Windows10 and Windows 7 Powershell
#Exact conversion is : remove the ":" symbol, replace all "\" by "/", remove last "/" and minor case only the disk letter
#Then for Windows10, add a /host_mnt/" at the begin of string => this way : c:\Users is translated to /host_mnt/c/Users
#For Windows7, add "//" => c:\Users is translated to //c/Users
$MOUNT_PATH=(($WIN_PATH -replace "\\","/") -replace ":","").Trim("/")
[regex]$regex='^[a-zA-Z]/'
$MOUNT_PATH=$regex.Replace($MOUNT_PATH, {$args[0].Value.ToLower()})
#Win 10
if ([Environment]::OSVersion.Version -ge (new-object 'Version' 10,0)) {
$MOUNT_PATH="/host_mnt/$MOUNT_PATH"
}
elseif ([Environment]::OSVersion.Version -ge (new-object 'Version' 6,1)) {
$MOUNT_PATH="//$MOUNT_PATH"
}
docker run -it -v "${MOUNT_PATH}:/tmp/test" busybox ls /tmp/test
- Docker Desktop (Windows 용 Docker)에서 설정 을 엽니 다 .
- 공유 드라이브를 선택합니다 .
- 컨테이너 내부에서 사용할 드라이브 (예 : C)를 선택합니다.
이제 아래 명령이 PowerShell에서 작동합니다 (명령 프롬프트는를 지원하지 않음
${PWD}
).docker run --rm -v ${PWD}:/data alpine ls /data
IMPORTANT: if/when you change your Windows domain password, the mount will stop working silently, that is, -v
will work but the container will not see your host folders and files. Solution: go back to Settings, uncheck the shared drives, Apply, check them again, Apply, and enter the new password when prompted.
This command should fix it.
docker run --rm -it -v ${PWD}:c:\data
mirkohaaser/docker-clitools
{PWD} is the host current folder. after the :
is the container folder. If the mounting is correct then files will be listed in the director c:\data
in the container.
You need to swap all the back slashes to forward slashes so change
docker -v C:\my\folder:/mountlocation ...
to
docker -v C:/my/folder:/mountlocation ...
I normally call docker from a cmd script where I want the folder to mount to be relative to the script i'm calling so in that script I do this...
SETLOCAL
REM capture the path to this file so we can call on relative scrips
REM without having to be in this dir to do it.
REM capture the path to $0 ie this script
set mypath=%~dp0
REM strip last char
set PREFIXPATH=%mypath:~0,-1%
echo "PREFIXPATH=%PREFIXPATH%"
mkdir -p %PREFIXPATH%\my\folder\to\mount
REM swap \ for / in the path
REM because docker likes it that way in volume mounting
set PPATH=%PREFIXPATH:\=/%
echo "PPATH=%PPATH%"
REM pass all args to this script to the docker command line with %*
docker run --name mycontainername --rm -v %PPATH%/my/folder/to/mount:/some/mountpoint myimage %*
ENDLOCAL
'Program Tip' 카테고리의 다른 글
SDDL 생성 실패, 오류 : 1332 (0) | 2020.10.13 |
---|---|
React Native에서 부모 너비의 80 %보기 (0) | 2020.10.13 |
목록을 바인딩하는 방법 (0) | 2020.10.13 |
각 요소의 길이를 기준으로 배열을 정렬하는 방법은 무엇입니까? (0) | 2020.10.13 |
jQuery로 키 누르기 시뮬레이션 (0) | 2020.10.13 |