본문 바로가기
가상화/docker

[Docker] 컨테이너 연동( 호스트간 파일 복사, 볼륨 마운트)

by JINJINC 2023. 5. 11.
728x90
반응형

 컨테이너와 호스트 간에 파일 복사하기

항목
윈도우 원본 경로 C:\Users\사용자명\Documents\index.html
macOS 원본 경로 /Users/사용자명/Documents/index.html
리눅스 원본 경로 /home/사용자명/index.html
컨테이너 경로 /usr/local/apache2/htdocs

 

호스트의 파일  ->  컨테이너 속으로 복사

docker run --name httpd01 -d -p 8089:80 httpd 

http://192.168.1.104:8089 -> It works! 확인

vi index.html
hello!
docker cp index.html httpd01:/usr/local/apache2/htdocs/

http://192.168.1.104:8089 -> hello! 확인

1. 처음 확인하기

index 파일 만든 후 확인 

# docker cp index.html httpd01:/usr/local/apache2/htdocs/ 

=> httpd01안으로 복사하기 

컨테이너의 파일  ->  호스트로 복사하기

docker run --name httpd02 -d -p 8090:80 httpd 
http://192.168.1.104:8090 -> hello! 확인

docker cp httpd02:/usr/local/apache2/htdocs/index.html ./index2.html
ls
호스트에서 확인

cat < index2.html

 

 

 볼륨 마운트

스토리지 영역 만드는 방법

볼륨 생성 => docker volume create 볼륨_이름

볼륨 삭제 => docker volume rm 볼륨_이름

 

스토리지를 마운트하는 커맨드

바인드 마운트 커맨드 

docker run (생략) -v 스토리지_실제경로:컨테이너마운트 경로

 

볼륨 마운트 커맨드 예 

docker run (생략) -v 볼륨이름:컨테이너 마운트 경로

 

< 폴더에 마운트하기 >

mkdir /dir1
docker run --name httpd01 -d -p 8090:80 -v /dir1:/usr/local/apache2/htdocs httpd
vi /dir1/index.html
hello

<확인1> http://192.168.1.104:8090 -> hello

vi /dir1/index.html
hello2

<확인2> http://192.168.1.104:8090 -> hello2
touch /dir1/1.txt

docker exec -it httpd01 /bin/bash
$ cd /usr/local/apache2/htdocs
$ ls -> 1.txt 보임
$ exit

확인 1
확인 2

 

< 볼륨에 마운트 하기>

docker volume create vol01
docker run --name httpd02 -d -p 8091:80 -v vol01:/usr/local/apache2/htdocs httpd
docker volume inspect vol01 
[
	{
		"CreatedAt": "2022-09-21T00:30:09Z",
		"Driver": "local",
		"Labels": {},
		"Mountpoint": "/var/lib/docker/volumes/vol01/_data",
		"Name": "vol01",
		"Options": {},
		"Scope": "local"
	}
]
docker container inspect httpd02 | grep vol01
	"vol01:/usr/local/apache2/htdocs"
	"Name": "vol01",
	"Source": "/var/lib/docker/volumes/vol01/_data",

docker volume rm vol01

# cd /var/lib/docker/volumes/vol01/_data

# ls

# cat > index.html hello^^

 

<확인> http://192.168.1.104:8091 -> hello^^

 

 

728x90
반응형

댓글