Docker Volume 명령어(볼륨mount, 디렉토리 방식) 및 실습

2023. 5. 17. 16:10·Container
728x90
반응형

볼륨 mount 방식

 

## 도커에서 볼륨 생성 (이름이 test인 볼륨 생성)

[root@localhost ~]# docker volume create test
test

 

## 도커 볼륨 경로 확인
[root@localhost ~]# docker volume ls
DRIVER    VOLUME NAME
local     test

 

 

## 컨테이너에서 볼륨 연결 

[root@localhost ~]# docker run -itd -v test:/test --name c1 centos:7
e4765396f0c5762949db19459042a9152290eae887a2915887572ca57580c849

 

 

## C1 컨테이너에서 test볼륨에 어떤 파일 있는지 확인 

[root@localhost ~]# docker exec c1 ls -al /test
total 0
drwxr-xr-x. 2 root root 25 May 16 12:45 .
drwxr-xr-x. 1 root root 18 May 16 12:47 ..
-rw-r--r--. 1 root root  0 May 16 12:45 volume.test

 

 

## 컨테이너에서 볼륨 연결 

[root@localhost ~]# docker run -itd -v test:/test1 --name c2 centos:7
1886d202e032b167fe33c49c392ee5cf80d65d1a6edb1bd01fecd1d2fa53cbb9

 

 

## C2 컨텡너 test1 디렉토리 확인

[root@localhost ~]# docker exec c2 ls -al /test1
total 0
drwxr-xr-x. 2 root root 25 May 16 12:45 .
drwxr-xr-x. 1 root root 19 May 16 12:49 ..
-rw-r--r--. 1 root root  0 May 16 12:45 volume.test

 

 

 

## volume.test 파일 수정

[root@localhost ~]# cat >> /var/lib/docker/volumes/test/_data/volume.test 
hello_tea^H^H^H^H^Hsung
hello----
^Z
[1]+  Stopped                 cat >> /var/lib/docker/volumes/test/_data/volume.test
[root@localhost ~]# docker exec c1 cat /
bin/   dev/   home/  lib64/ mnt/   proc/  run/   srv/   test/  usr/   
boot/  etc/   lib/   media/ opt/   root/  sbin/  sys/   tmp/   var/   

 

## c1컨테이너 volume.test파일 읽기

[root@localhost ~]# docker exec c1 cat /test/volume.test
hellsunga
hello----

 

 


디렉토리 연결 방식

 

## 연결할려는 디렉토리 생성

[root@localhost ~]# mkdir /vol

 

## /vol:/vol 절대 경로의 디렉토리 경로를 적어줘야 합니다. 앞에 /vol은 로컬에 생성한 디렉토리이며,

뒤의 :/vol은 컨테이너의 디렉토리. 하지만 컨테이너의 디렉토리 명을 모르기 때문에 자동으로 임의로 지정해준 것.

 

어떤 이름으로 하던 상관 없으며, 없는 디렉토리 명이면 컨테이너에서 생성 해줍니다.

결론 : 뒤에 :/vol은 컨테이너의 /vol 디렉토리.

## vol이 없을 시 Docker에서 볼륨을 자동으로 생성합니다. 따라서 절대 경로를 적어줘야 합니다. 
[root@localhost ~]# docker run -itd -v /vol:/vol --name al alpine
774d37d2109b696f3638f9982b115b1094cae3d261b2722ee26cf9e8ead4770

 

 

## /vol 디렉토리의 al로 alpine 컨테이너 실행

[root@localhost ~]# docker run -itd -v /vol:/vol --name al alpine
774d37d2109b696f3638f9982b115b1094cae3d261b2722ee26cf9e8ead47700
[root@localhost ~]# 
[root@localhost ~]# 

 

##컨테이너 al 실행 
[root@localhost ~]# docker exec -it al /bin/sh
/ # ls 
bin    etc    lib    mnt    proc   run    srv    tmp    var
dev    home   media  opt    root   sbin   sys    usr    vol

 

 

## cat으로 test.txt 생성 후 간단한 글 입력 후 출력

/ # cat > vol/test.txt << END
> HI TAESUNG
> TEST
> TEST3
> END
/ # ls /vol
test.txt
/ # cat /v
var/  vol/
/ # cat /vol/test.txt 
HI TAESUNG


실습

1. 최상위 디렉토리에 /html 디렉토리 생성 후 index.html 파일 생성

index.html 내용은 DOCKER-VOLUME-TEST가 출력되도록 생성

2. httpd 이미지를 이용해서 h1 container를 생성하면서 /html - /usr/local/apache2/htdocs/html 디렉토리를 마운트해서 해당 페이지 출력

3. nginx 이미지를 이용해서 n1 container를 생성하면서 /html - /usr/share/nginx/html 디렉토리와 마운트해서 해당 페이지 출력

4. 확인 후 host의 index.html 파일 내용을 변경해 h1, n1에 즉각 반영되는지 확인

 


## 1번 예제 실행

[root@localhost ~]# mkdir /html
[root@localhost ~]# cd /html/
[root@localhost html]# vi index.html
[root@localhost html]# 
[root@localhost html]# 
[root@localhost html]# cat index.html 
<html><body><h1>DOCKER-VOLUME-TEST</h1></body></html>

 

## 2번 예제 실행

docker run -itd -p 61089:80 -v /html:/usr/local/apache2/htdocs/ --name h4 httpd
62c06ab6f434c874e469046de54826cf602543da293860a7f9940e914b9a131d

 

## 3번 예제 실행

[root@localhost html]# docker run -itd -p 61090:80 -v /html:/usr/share/nginx/html --name n5 nginx
57d891eb5c24583a91503cb4649d3c3d07ff32f062d32b537cee0384cd448de9

728x90
반응형

'Container' 카테고리의 다른 글

Docker Network 명령어  (0) 2023.05.18
Docker MySQL 설치 및 실행  (0) 2023.05.17
Docker import, export, scp 명령어 실습  (0) 2023.05.17
Docker save, load, tar, scp 명령어 실습  (0) 2023.05.17
Docker 복사(CP) 실습 예제  (0) 2023.05.17
'Container' 카테고리의 다른 글
  • Docker Network 명령어
  • Docker MySQL 설치 및 실행
  • Docker import, export, scp 명령어 실습
  • Docker save, load, tar, scp 명령어 실습
Royal!
Royal!
Bachelor of Arts. Bachelor of Science in Engineering. Master of Science in Engineering (In Progress). Job Title: Infrastructure & Security & Cloud Engineer. Etc: Mentor at Fourth Industrial Revolution Center.
  • Royal!
    Security
    Royal!
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 운영체제
      • 네트워크
      • 클라우드
      • 서버
      • Container
      • 프로그래밍
        • Python_혼자 끄적끄적
        • Python_AI(영상처리)
        • 빅데이터
        • C_정보올림피아드 Language_Coder
        • C_QnA
      • 자격증
        • 정보보안기사
        • 정보처리기사
        • ADsP
        • CPPG
        • 보안법률
        • NCA~NCP
        • AWS Certified Solutions Arc..
      • 리버싱
        • 리버싱 걸음마
      • Wargame & CTF
        • Hackerschool FTZ
      • 기타 교육
        • KISA 정보보호제품실습군
        • 빅데이터_분석실무
        • 시큐어코딩진단전문교육(호남정보보호센터)
        • SW테스트 전문가 과정(ISTQB CTFL자격)
        • 정보보호 컨설팅 전문가 양성과정
      • 기타
        • 면접일지
        • 기타
        • 인턴활동
        • 논문 요약
      • K-Shield Jr
        • 악성코드분석
        • 침해사고분석
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    nce덤프
    restful api 개념
    euid가 중요한 이유
    redirect 304
    상태코드 304
    스위치 프레임 동작 방식
    geteuid
    kernel 사용자 모드
    kernel 구성요소
    304 code
    euid란
    304코드
    rest api 제약조건
    fragment-free
    getuid()
    rest api 공부
    l2 스위치 프레임 전달 방식
    swtich frame
    getuid
    스위치 프레임 포워딩 방식
    rest api 아키텍처
    switch forwarding mode
    uid란
    geteuid()
    네이버클라우드 ftp 설정
    kernel모드
    nce dump
    rest api 정리
    rest api 문법
    rest api 논문
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Royal!
Docker Volume 명령어(볼륨mount, 디렉토리 방식) 및 실습
상단으로

티스토리툴바