[DOCKER] 스토리지

Posted by Albert 25Day 16Hour 40Min 16Sec ago [2025-03-25]

쌤플로 postgres 로 진행

docker hub image: https://hub.docker.com/_/postgres


1. postgres 이미지 다운로드

root@albert:/etc/apt/sources.list.d' docker image pull postgres
Using default tag: latest
latest: Pulling from library/postgres
root@albert:/etc/apt/sources.list.d' docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nubuntu 0.1 0f79e1b1b241 35 minutes ago 149MB
postgres latest e92968df8375 3 weeks ago 459MB
ubuntu latest c3d1a3432580 8 weeks ago 101MB
python 3.11.6 ef55ff88816b 17 months ago 1.01GB


2. 스토리지 종류

1) volume : container 내부 스토리지에 저장함 (지정된 volume에 저장하여 컨테이너  container 삭제한후 다시 같은 볼륨으로 지정하여 생성하면 data가 유지됨)

volumne 생성

root@albert:/home/albert/Desktop/mvtest' docker volume create myvolume01
myvolume01
root@albert:/home/albert/Desktop/mvtest' docker volume ls
DRIVER VOLUME NAME
local 33e91fbecc2df9f4dfb1fdd3cad6a4e8cdde886584bd0e362a6ffa168c566b5e
local 39b3ea615e16d8e383603589d15a6d0f35a2bc24a2fa2cd86aaa25f9afd0d613
local myvolume01

postgres image를 volumne 방식으로 실행 

root@albert:/home/albert/Desktop/mvtest' docker container run -e POSTGRES_PASSWORD=mysecretpassword --mount type=volume,source=myvolume01,target=/var/lib/postgresql/data -d postgres 
179f9a0c9716805c32efbbd3bc54b560dd3df7cb45e0638cd04cb902fcefe2b7

postgres 컨테이너 접속 및 user01사용자 생성

root@albert:/home/albert/Desktop/mvtest' docker container exec -it 179f9a0c9716 /bin/bash
root@179f9a0c9716:/' psql -U postgres
psql (17.4 (Debian 17.4-1.pgdg120+2))
Type "help" for help.

postgres=' create user user01 password '1234' SUPERUSER;
CREATE ROLE
postgres=' \du
List of roles
Role name | Attributes
-----------+------------------------------------------------------------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
user01 | Superuser

컨테이너 내부 /var/lib/postgresql/data 경로 data정보

root@179f9a0c9716:/var/lib/postgresql/data' ls
base pg_ident.conf pg_serial pg_tblspc postgresql.auto.conf
global pg_logical pg_snapshots pg_twophase postgresql.conf
pg_commit_ts pg_multixact pg_stat PG_VERSION postmaster.opts
pg_dynshmem pg_notify pg_stat_tmp pg_wal postmaster.pid
pg_hba.conf pg_replslot pg_subtrans pg_xact

호스트 서버 myvolume01 주소 /var/lib/docker/volumes/myvolume01/_data  data 정보 (컨테이너 정보와 같은걸 확인가능)

root@albert:/work/storage' docker inspect myvolume01
[
{
"CreatedAt": "2025-03-25T06:17:02Z",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/myvolume01/_data",
"Name": "myvolume01",
"Options": null,
"Scope": "local"
}
]
root@albert:/work/storage' cd /var/lib/docker/volumes/myvolume01/_data
root@albert:/var/lib/docker/volumes/myvolume01/_data' ls
base pg_hba.conf pg_notify pg_stat pg_twophase postgresql.auto.conf
global pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf
pg_commit_ts pg_logical pg_serial pg_subtrans pg_wal postmaster.opts
pg_dynshmem pg_multixact pg_snapshots pg_tblspc pg_xact


2) bind mount: host서버 스토리지와 컨테이너 디렉토리를 연결시켜 최종으로는 host서버에 관련 정보 저장하는 방식

아래 쌤플은 호스트 위치 /work/storage 와 컨테이너 위치 /work를 동기화 시켜 포스트그레스 실행하는 내용임

root@albert:/home/albert/Desktop/mvtest' docker container run -e POSTGRES_PASSWORD=mysecretpassword --mount type=bind,source=/work/storage,target=/work -d postgres
56fd2483aa739fcf8f29cdb19ac135876de592f158a93e2b7bc523d0cc1e2690

컨테이너 접속후 work 폴더안에 abcd파일있는거 확인

root@albert:/home/albert/Desktop/mvtest' docker container exec -it 56fd2483aa73 /bin/bash
root@56fd2483aa73:/' cd work/
root@56fd2483aa73:/work' ls
abcd

실제 호스트의 /work/storage폴더에도 똑같은 abcd파일있는거 확인가능

root@albert:/work/storage' pwd
/work/storage
root@albert:/work/storage' ls
abcd


3) tmpfs mount: 중요한 data를 일시적으로 도커 호스트 메모리에 저장하고 싶을때 사용(컨테이너간 데이터 공유 불가하고 컨테이너가 정지되면 tmpfs mount도 삭제됨)


root@albert:/home/albert/Desktop/mvtest' docker container run -e POSTGRES_PASSWORD=mysecretpassword --mount type=tmpfs,destination=/var/lib/postgresql/data -d postgres
1c3febd3dab38341329f6ee5f184c8a76c330bddb79e8d7ad2502f0758a8f193

정상적으로 지정된 /var/lib/postgresql/data 로 마운트 된부분 확인 할 수 있다.

root@albert:/work/storage' docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c3febd3dab3 postgres "docker-entrypoint.s…" 44 minutes ago Up 44 minutes 5432/tcp fervent_gould

root@albert:/home/albert/Desktop/mvtest' docker inspect fervent_gould --format ''
...
"Mounts": [
{
"Type": "tmpfs",
"Source": "",
"Destination": "/var/lib/postgresql/data",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
...




LIST

Copyright © 2014 visionboy.me All Right Reserved.