본문 바로가기
요즘공부

[도커&쿠버네티스] 교육 1일차_ 컨테이너 생성시 삭제되지 않도록 하는 법

by 게으른 피글렛 2025. 2. 21.
반응형

 

0. 컨테이너를 삭제하고 다시 만들경우 내용이 초기화됨

컨테이너를 삭제 시키게 된다면, 컨테이너에 올라가 있던 내용이 초기화됨

#컨테이너 생성, 이름을 지정하지 않을 경우 자동으로 이름이 생성됩니다.
vagrant@ubuntu2204:~$ docker run -d httpd
9785a233bd893fee3f44d8453378e193a1e66edb9f15704ac28d0f636c6de281
vagrant@ubuntu2204:~$ docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         POR      TS     NAMES
9785a233bd89   httpd     "httpd-foreground"   6 seconds ago   Up 5 seconds   80/      tcp    intelligent_lalande

#컨테이너 접속, index.html 내용 변경
vagrant@ubuntu2204:~$ docker exec -it intelligent_lalande /bin/bash
root@9785a233bd89:/usr/local/apache2# cd htdocs
root@9785a233bd89:/usr/local/apache2/htdocs# vi index.html

#현재 저장된 내용
vagrant@ubuntu2204:~$ curl 172.17.0.2
<html><body><h1>hello docker</h1></body></html>

#삭제 후 재 실행
vagrant@ubuntu2204:~$ all_containers_rm
9785a233bd89

#httpd 컨테이너 생성
vagrant@ubuntu2204:~$ docker run -d --name intelligent_lalande httpd
fb3ba503ebab531dc19187d5d3ce641183ee538bfea9ea8a9de383854790c3fb

#기록이 남지 않음
vagrant@ubuntu2204:~$ crul 172.17.0.2
Command 'crul' not found, did you mean:
  command 'curl' from snap curl (8.12.1)
  command 'curl' from deb curl (7.81.0-1ubuntu1.18)
  command 'crun' from deb crun (0.17+dfsg-1.1ubuntu0.1)
See 'snap info <snapname>' for additional versions.

 

 

1. volume 생성

#기존 볼륨삭제
vagrant@ubuntu2204:~$ docker volume prune
WARNING! This will remove anonymous local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
127142e0bdfe9df24b9962866ffbc925b2c204a2e5a57ae0c122f5c887a5cba3
Total reclaimed space: 209.1MB

#볼륨생성
vagrant@ubuntu2204:~$ docker volume create myvolume
myvolume

#생성된 볼륨 확인
vagrant@ubuntu2204:~$ docker volume ls
DRIVER    VOLUME NAME
local     myvolume

 

 

2. 컨테이너 생성

#볼륨을 컨테이너 내 /usr/local/apache2/htdocs 디렉토리에 마운트
vagrant@ubuntu2204:~$ docker run -d --name apachex --volume myvolume:/usr/local/apache2/htdocs  httpd
67e72496b1ab1bb44ebb32f34c0a2b4674cb452c30f4d1c8c4d93ea1fb2f5040

#컨테이너 접속
vagrant@ubuntu2204:~$ docker exec -it apachex /bin/bash
root@67e72496b1ab:/usr/local/apache2# cd htdocs

#컨테이너에 생성된 볼륨 확인
root@67e72496b1ab:/usr/local/apache2/htdocs# df -h
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv   62G  7.7G   51G  14% /etc/hosts

#echo 명령어로 파일 수정
root@67e72496b1ab:/usr/local/apache2/htdocs# echo hello apache > index.html
root@67e72496b1ab:/usr/local/apache2/htdocs# cat index.html
hello apache

#변경된 값 반영
vagrant@ubuntu2204:~$ curl 172.17.0.3
hello apache

#index.html에서 포함된 위치 확인
vagrant@ubuntu2204:~$ sudo find /var/lib/docker/ -name index.html
/var/lib/docker/overlay2/fa06453f60fc778eb0a57b0bec487e11c9678ee091284cfaa167af9032112ab0/diff/usr/share/doc/cyrus-sasl-lib/index.html
/var/lib/docker/overlay2/d12cf4be05b9559f11cde2aa8759b3a1ad7fe21f2bd8eadfe13e962568b276ff/diff/usr/local/apache2/htdocs/index.html
/var/lib/docker/overlay2/075218e6299492148f7bc9042f5a7e75316ed602962022a087a58d5e146b8f08/diff/usr/share/nginx/html/index.html
/var/lib/docker/overlay2/03cb88ced268589d056f5ead1644a08fae0fd1641566c158d6ff420bdb53330b/merged/usr/local/apache2/htdocs/index.html
/var/lib/docker/overlay2/6a359c927a6f403efbbd98f79aa7f84dff4d394a52a31131890e9d7ead83f475/diff/usr/lib/mysqlsh/lib/python3.13/site-packages/setuptools/tests/indexes/test_links_priority/simple/foobar/index.html
/var/lib/docker/overlay2/6fd2270e6a4e5bdc2d2f0a61ed52bcb62533503ad0c51c93cead7ca7b5cd5337/merged/usr/local/apache2/htdocs/index.html
/var/lib/docker/volumes/myvolume/_data/index.html
#myvolume에 생성된 내용 확인

#값이 저장되어있음을 확인 가능
vagrant@ubuntu2204:~$ sudo cat /var/lib/docker/volumes/myvolume/_data/index.html
hello apache

 

3. 컨테이너 삭제

#삭제 명령어 ailes 생성
$ alias all_containers_rm='docker stop $(docker ps -q) ; docker rm $(docker ps -aq)'

#생성된 컨테이너 없음
vagrant@ubuntu2204:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

4. 컨테이너 재생성

#동일한 컨테이너 생성
vagrant@ubuntu2204:~$ docker run -d --name apachex --volume myvolume:/usr/local/apache2/htdocs  httpd
5c9ade151d95156c8700509e257d9042ed9b4dd8521ad18b9e8f58da420a61f3

#저장된 값 그대로표기됨
vagrant@ubuntu2204:~$ curl 172.17.0.2
hello apache

#파일명을 변경해도 같은 값을 불러옴
vagrant@ubuntu2204:~$ docker run -d --name apache001 --volume myvolume:/usr/local/apache2/htdocs  httpd
3aeaa1ff328e6a5130d3e8214b85056bebeaf17ac6e2945aa38797b2324e12e7
vagrant@ubuntu2204:~$ curl 172.17.0.3
hello apache

 

5. 볼륨 삭제

#볼륨 삭제(실행중인 상태에서는 삭제가 되지 않음)
vagrant@ubuntu2204:~$  docker volume rm myvolume
Error response from daemon: remove myvolume: volume is in use - [5c9ade151d95156c8700509e257d9042ed9b4dd8521ad18b9e8f58da420a61f3, 3aeaa1ff328e6a5130d3e8214b85056bebeaf17ac6e2945aa38797b2324e12e7]

#컨테이너 삭제
vagrant@ubuntu2204:~$ all_containers_rm
3aeaa1ff328e
5c9ade151d95
3aeaa1ff328e
5c9ade151d95

#볼륨삭제
vagrant@ubuntu2204:~$ docker volume rm myvolume
myvolume
vagrant@ubuntu2204:~$ docker volume ls
DRIVER    VOLUME NAME

 

 

반응형