Docker 머신에 로컬 볼륨을 마운트하는 방법
docker-compose와 함께 docker-machine을 사용하려고합니다. docker-compose.yml 파일에는 다음과 같은 정의가 있습니다.
web:
build: .
command: ./run_web.sh
volumes:
- .:/app
ports:
- "8000:8000"
links:
- db:db
- rabbitmq:rabbit
- redis:redis
실행할 때 docker-compose up -d
모든 잘 명령을 실행하려고 할 때까지 이동하고 오류가 생성됩니다
컨테이너 b58e2dfa503b696417c1c3f49e2714086d4e9999bd71915a53502cb6ef43936d : [8] 시스템 오류 : exec : "./run_web.sh": stat ./run_web.sh : 해당 파일 또는 디렉토리가 없습니다.
로컬 볼륨은 원격 시스템에 마운트되지 않습니다. 웹앱 코드로 로컬 볼륨을 마운트하는 데 권장되는 전략은 무엇입니까?
또한이 문제가 발생했으며 docker-machine을 사용할 때 로컬 볼륨이 마운트되지 않은 것처럼 보입니다. 해킹 솔루션은
docker-machine 인스턴스의 현재 작업 디렉토리를 가져옵니다.
docker-machine ssh <name> pwd
rsync
원격 시스템에 폴더 복사 와 같은 명령 줄 도구 사용rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:<result _of_pwd_from_1>.
기본 pwd는 / root이므로 위의 명령은 다음과 같습니다. rsync -avzhe ssh --progress <name_of_folder> username@remote_ip:/root
주의 : 원격 시스템에 대한 암호를 제공해야합니다. 원격 시스템에 ssh하고 암호를 생성하여 신속하게 생성 할 수 있습니다.
볼륨은 마운트 지점을 달라
docker-compose.yml
에서 파일.:/app
을/root/<name_of_folder>:/app
운영
docker-compose up -d
NB 로컬에서 변경이 이루어지면 변경 rsync
사항을 원격 시스템에 푸시 하기 위해 다시 실행 하는 것을 잊지 마십시오 .
완벽하지는 않지만 작동합니다. 문제가 진행 중입니다. https://github.com/docker/machine/issues/179
이를 해결하려는 다른 프로젝트로는 docker-rsync가 있습니다.
Docker-machine은 사용자 디렉토리를 자동 마운트합니다 ...하지만 때로는 그것만으로는 충분하지 않습니다.
도커 1.6에 대해서는 잘 모르겠지만 1.8에서는 도커 머신에 추가 마운트를 추가 할 수 있습니다.
가상 머신 마운트 지점 추가 (1 부)
CLI : (기계가 정지 된 경우에만 작동)
VBoxManage sharedfolder add <machine name/id> --name <mount_name> --hostpath <host_dir> --automount
따라서 Windows의 예는 다음과 같습니다.
/c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe sharedfolder add default --name e --hostpath 'e:\' --automount
GUI : (기계를 중지 할 필요가 없습니다)
- "Oracle VM VirtualBox Manager"를 시작합니다.
- 오른쪽 클릭
<machine name>
(기본값) - 설정 ...
- 공유 폴더
- 오른쪽의 폴더 + 아이콘 (공유 추가)
- 폴더 경로 :
<host dir>
(e :) - 폴더 이름 :
<mount name>
(e) - "Auto-mount"및 "Make Permanent"(원하는 경우 읽기 전용 ...)를 확인합니다 (자동 마운트는 현재 일종의 무의미합니다 ...).
boot2docker에 장착 (2 부)
boot2docker에 수동으로 마운트하십시오 .
- 로그인, "Oracle VM VirtualBox Manager"에서 "Show"를 사용하거나 IP 주소로 docker에 ssh / putty를 사용하는 등 다양한 방법이 있습니다
docker-machine ip default
. sudo mkdir -p <local_dir>
sudo mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` <mount_name> <local_dir>
그러나 이것은 컴퓨터를 다시 시작하고 마운트가 손실 될 때까지만 좋습니다.
boot2docker에 자동 마운트 추가 :
기기에 로그인되어있는 동안
- (루트로) 편집 / 만들기
/mnt/sda1/var/lib/boot2docker/bootlocal.sh
, sda1은 당신을 위해 다를 수 있습니다 ... 더하다
mkdir -p <local_dir> mount -t vboxsf -o defaults,uid=`id -u docker`,gid=`id -g docker` <mount_name> <local_dir>
이러한 변경으로 새 마운트 지점이 있어야합니다. 이것은 부팅시 호출되고 영구적 인 몇 안되는 파일 중 하나입니다. 더 나은 솔루션이있을 때까지 작동합니다.
이전 방법 : 덜 권장 되지만 대안으로 남음
- 편집 (루트)
/mnt/sda1/var/lib/boot2docker/profile
, sda1은 다를 수 있습니다 ... 더하다
add_mount() { if ! grep -q "try_mount_share $1 $2" /etc/rc.d/automount-shares ; then echo "try_mount_share $1 $2" >> /etc/rc.d/automount-shares fi } add_mount <local dir> <mount name>
A와 최후의 수단 , 당신은 약간 지루한 대안을 취할 수 있고, 당신은 단지 부팅 이미지를 수정할 수 있습니다.
git -c core.autocrlf=false clone https://github.com/boot2docker/boot2docker.git
cd boot2docker
git -c core.autocrlf=false checkout v1.8.1
# 또는 적절한 버전- 편집하다
rootfs/etc/rc.d/automount-shares
try_mount_share <local_dir> <mount_name>
끝에 fi 바로 앞에 줄을 추가 합니다. 예를 들면try_mount_share /e e
/ bin 등과 같이 os에 필요한 것으로 설정하지 마십시오.
docker build -t boot2docker .
# 처음에는 약 1 시간 정도 소요됩니다 :(docker run --rm boot2docker > boot2docker.iso
- 이전 boot2docker.iso를 백업하고 ~ / .docker / machine / machines /의 그 자리에 새 파일을 복사하십시오.
이것은 작동합니다. 길고 복잡합니다.
docker 버전 1.8.1, docker-machine 버전 0.4.0
현재로서는 머신에 볼륨을 마운트하는 방법을 실제로 볼 수 없으므로 지금까지 필요한 파일을 머신에 복사하거나 동기화하는 것이 접근 방식입니다.
docker-machine의 github repo에서이 문제를 해결하는 방법에 대한 대화 가 있습니다 . 누군가 docker-machine에서 scp 를 구현 하는 pull request를 만들었고 이미 master에 병합되었으므로 다음 릴리스에 포함될 가능성이 큽니다.
아직 출시되지 않았기 때문에 지금까지 코드가 github에 호스팅 된 경우 앱을 실행하기 전에 저장소를 복제하는 것이 좋습니다.
web:
build: .
command: git clone https://github.com/my/repo.git; ./repo/run_web.sh
volumes:
- .:/app
ports:
- "8000:8000"
links:
- db:db
- rabbitmq:rabbit
- redis:redis
업데이트 : 더 자세히 살펴보면이 기능이 이미 최신 바이너리 에서 사용 가능하다는 것을 알았습니다.이 기능 을 받으면 다음과 같은 명령을 실행하는 로컬 프로젝트를 복사 할 수 있습니다.
docker-machine scp -r . dev:/home/docker/project
이것이 일반적인 형태이기 때문에 :
docker-machine scp [machine:][path] [machine:][path]
따라서 컴퓨터간에 파일을 복사 할 수 있습니다.
건배! 1
docker-machine과 함께 rsync 옵션을 선택하면 다음 docker-machine ssh <machinename>
과 같은 명령과 결합 할 수 있습니다 .
rsync -rvz --rsh='docker-machine ssh <machinename>' --progress <local_directory_to_sync_to> :<host_directory_to_sync_to>
이 명령 형식의 rsync를 사용하고 HOST
비워 둡니다 .
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
( http://linuxcommand.org/man_pages/rsync1.html )
Since October 2017 there is a new command for docker-machine that does the trick, but make sure there is nothing in the directory before executing it, otherwise it might get lost:
docker-machine mount <machine-name>:<guest-path> <host-path>
Check the docs for more information: https://docs.docker.com/machine/reference/mount/
PR with the change: https://github.com/docker/machine/pull/4018
Finally figured out how to upgrade Windows Docker Toolbox to v1.12.5 and keep my volumes working by adding a shared folder in Oracle VM VirtualBox
manager and disabling path conversion. If you have Windows 10+ then you're best to use the newer Docker for Windows.
1st the upgrade Pain:
- Uninstall VirtualBox first.
- Yep that may break stuff in other tools like Android Studio. Thanks Docker :(
- Install new version of Docker Toolbox.
Redis Database Example: redis: image: redis:alpine container_name: redis ports: - "6379" volumes: - "/var/db/redis:/data:rw"
In Docker Quickstart Terminal ....
- run
docker-machine stop default
- Ensure VM is haulted
In Oracle VM VirtualBox Manager ...
- Added a shared folder in
default
VM via or command lineD:\Projects\MyProject\db
=>/var/db
In docker-compose.yml
...
- Mapped redis volume as:
"/var/db/redis:/data:rw"
In Docker Quickstart Terminal ....
- Set
COMPOSE_CONVERT_WINDOWS_PATHS=0
(for Toolbox version >= 1.9.0) - run
docker-machine start default
to restart the VM. cd D:\Projects\MyProject\
docker-compose up
should work now.
Now creates redis database in D:\Projects\MyProject\db\redis\dump.rdb
Why avoid relative host paths?
I avoided relative host paths for Windows Toolbox as they may introduce invalid '\' chars. It's not as nice as using paths relative to docker-compose.yml
but at least my fellow developers can easily do it even if their project folder is elsewhere without having to hack the docker-compose.yml
file (bad for SCM).
Original Issue
FYI ... Here is the original error I got when I used nice clean relative paths that used to work just fine for older versions. My volume mapping used to be just "./db/redis:/data:rw"
ERROR: for redis Cannot create container for service redis: Invalid bind mount spec "D:\\Projects\\MyProject\\db\\redis:/data:rw": Invalid volume specification: 'D:\Projects\MyProject\db\redis:/data
This breaks for two reasons ..
- It can't access
D:
drive - Volume paths can't include
\
charactersdocker-compose
adds them and then blames you for it !!- Use
COMPOSE_CONVERT_WINDOWS_PATHS=0
to stop this nonsense.
I recommend documenting your additional VM shared folder mapping in your docker-compose.yml
file as you may need to uninstall VirtualBox again and reset the shared folder and anyway your fellow devs will love you for it.
I assume the run_web.sh
file is in the same directory as your docker-compose.yml
file. Then the command should be command: /app/run_web.sh
.
Unless the Dockerfile
(that you are not disclosing) takes care of putting the run_web.sh
file into the Docker image.
After summarize posts here, attached updated script, to create additional host mount point and automount when Virtualbox restart. The working environment brief as below: - Windows 7 - docker-machine.exe version 0.7.0 - VirtualBox 5.0.22
#!env bash
: ${NAME:=default}
: ${SHARE:=c/Proj}
: ${MOUNT:=/c/Proj}
: ${VBOXMGR:=C:\Program Files\Oracle\VirtualBox\VBoxManage.exe}
SCRIPT=/mnt/sda1/var/lib/boot2docker/bootlocal.sh
## set -x
docker-machine stop $NAME
"$VBOXMGR" sharedfolder add $NAME --name c/Proj --hostpath 'c:\' --automount 2>/dev/null || :
docker-machine start $NAME
docker-machine env $NAME
docker-machine ssh $NAME 'echo "mkdir -p $MOUNT" | sudo tee $SCRIPT'
docker-machine ssh $NAME 'echo "sudo mount -t vboxsf -o rw,user $SHARE $MOUNT" | sudo tee -a $SCRIPT'
docker-machine ssh $NAME 'sudo chmod +x /mnt/sda1/var/lib/boot2docker/bootlocal.sh'
docker-machine ssh $NAME 'sudo /mnt/sda1/var/lib/boot2docker/bootlocal.sh'
#docker-machine ssh $NAME 'ls $MOUNT'
I am using docker-machine 0.12.2 with the virtualbox drive on my local machine. I found that there is a directory /hosthome/$(user name)
from where you have access to local files.
Just thought I'd mention I've been using 18.03.1-ce-win65 (17513) on Windows 10 and I noticed that if you've previously shared a drive and cached the credentials, once you change your password docker will start having the volumes mounted within containers as blank.
It gives no indication that what is actually happening is that it is now failing to access the shared with the old cached credentials. The solution in this scenario is to reset the credentials either through the UI (Settings->Shared drives) or to disable then renable drive sharing and enter the new password.
It would be useful if docker-compose gave an error in these situations.
All other answers were good for the time but now (Docker Toolbox v18.09.3) all works out of the box. You just need to add a shared folder into VirtualBox VM.
Docker Toolbox automatically adds C:\Users
as shared folder /c/Users
under virtual linux machine (using Virtual Box shared folders feature), so if your docker-compose.yml
file is located somewhere under this path and you mount host machine's directories only under this path - all should work out of the box.
For example:
C:\Users\username\my-project\docker-compose.yml
:
...
volumes:
- .:/app
...
The .
path will be automatically converted to absolute path C:\Users\username\my-project
and then to /c/Users/username/my-project
. And this is exactly how this path is seen from the point of view of linux virtual machine (you can check it: docker-machine ssh
and then ls /c/Users/username/my-project
). So, the final mount will be /c/Users/username/my-project:/app
.
All works transparently for you.
But this doesn't work if your host mount path is not under C:\Users
path. For example, if you put the same docker-compose.yml
under D:\dev\my-project
.
This can be fixed easily though.
- Stop the virtual machine (
docker-machine stop
). Open Virtual Box GUI, open Settings of Virtual Machine named
default
, openShared Folders
section and add the new shared folder:- Folder Path:
D:\dev
- Folder Name:
d/dev
Press
OK
twice and close Virtual Box GUI.- Folder Path:
- Start the virtual machine (
docker-machine start
).
That's all. All paths of host machine under D:\dev
should work now in docker-compose.yml
mounts.
참고URL : https://stackoverflow.com/questions/30040708/how-to-mount-local-volumes-in-docker-machine
'Development Tip' 카테고리의 다른 글
패키지로드시 메시지 비활성화 (0) | 2020.10.07 |
---|---|
누락 된 SDK 플랫폼 Android, API 18 (0) | 2020.10.07 |
함수에서`…`(3 점 또는 점-점-점) 사용 (0) | 2020.10.07 |
GitHub Wiki의 ToC 또는 사이드 바 (0) | 2020.10.07 |
Ruby on Rails에서 헤더 데이터 읽기 (0) | 2020.10.07 |