본문 바로가기
DevOps/Docker

[DevOps] Docker Container Network(2) - 호스트와 컨테이너 간 파일 전송하기

by 가영리 2024. 6. 11.
728x90

호스트에서 컨테이너로 파일 전송하기

출처 : 한 권으로 배우는 도커 & 쿠버네티스

터미널1 = 호스트

터미널2 = 컨테이터 내부

터미널1을 통해 호스트에 존재하는 파일을 컨테이너로 전송하고 터미널 2를 통해 컨테이너에 전송된 파일을 확인해보자.

터미널1

work/ch04/ex01 디렉토리에 test01.txt 파일을 만들고 현재 위치의 절대경로를 파악한다.

cd work
mkdir ch04
ls
cd ch04
mkdir ex01
ls
cd ex01
vim test01.txt
Hello, I am Gayeong.
cat test01.txt
pwd

터미널2

docker container run -it ubuntu
cd home
ls

터미널1

터미널1에서 실행중인 컨테이너 목록을 확인하고

호스트에 존재하는 파일을 컨테이너 내부로 복사한다.

gayeong@myserver01:~/work/ch04/ex01$ docker container ls
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
4b534e04dc95   ubuntu    "/bin/bash"   2 minutes ago   Up 2 minutes             quizzical_mahavira

gayeong@myserver01:~/work/ch04/ex01$ docker container cp ./test01.txt 4b534e04dc95:/home
Successfully copied 2.05kB to 4b534e04dc95:/home

컨테이너에서 호스트로 파일 전송하기

터미널2

컨테이너의 홈디렉토리에서 확인해보니 test01.txt파일이 복된 것을 확인할 수 있다.

root@4b534e04dc95:/home# ls
test01.txt  ubuntu
root@4b534e04dc95:/home# cat test01.txt

Hello, I am Gayeong.

터미널2

test01.txt의 내용을 복사해 test02.txt 파일을 생성한다.

root@4b534e04dc95:/home# ls
test01.txt  ubuntu
root@4b534e04dc95:/home# cp test01.txt test02.txt
root@4b534e04dc95:/home# ls
test01.txt  test02.txt  ubuntu
root@4b534e04dc95:/home# cat test02.txt

Hello, I am Gayeong.
주의할 점

컨테이너에서 호스트로 파일을 전송하는 것은 컨테이너가 실행 중인 터미널2에서 명령어를 입력하는 것이 아니라 터미널1에서 명령한다. WHY? 컨테이너 2에는 도커가 설치되어 있지 않아 도커 명령어를 실행할 수 없기 때문이다.

터미널1

gayeong@myserver01:~/work/ch04/ex01$ pwd
/home/gayeong/work/ch04/ex01
gayeong@myserver01:~/work/ch04/ex01$ ls
test01.txt
gayeong@myserver01:~/work/ch04/ex01$ docker cp 4b534e04dc95:/home/test02.txt /home/gayeong/work/ch04/ex01
Successfully copied 2.05kB to /home/gayeong/work/ch04/ex01
gayeong@myserver01:~/work/ch04/ex01$ ls
test01.txt  test02.txt
gayeong@myserver01:~/work/ch04/ex01$ cat test02.txt

Hello, I am Gayeong.