# 바인드 마운트 :

- 호스트의 특정 디렉터리를 직접 연결

- 호스트와 컨테이너를 연결하려면 연결 대상이 되는 컨테이너 내부의 디렉터리 구조를 먼저 알아야함.

- 호스트 디렉터리의 내용을 그대로 컨테이너 디렉터리에 덮어쓴다는점(★★★★★)

 

# docker cp : 호스트에 위치한 파일을구동중인 컨테이너 내부에 복사(단편적)

- dockerfile에 add

- 바인드 마운트

- 볼륨

 

mkdir -p /root/html (경로 모두 생성) 
-v 호스트 디렉터리와 컨테이너 디렉터리를 연결하는 옵션

docker run -p 로컬 포트:컨테이너 포트

*컨테이너는 포트,이름 등이 겹치면 생성이 안되므로 주의

[root@m-k8s ~]# ls
[root@m-k8s ~]# mkdir -p /root/html
[root@m-k8s ~]# systemctl start docker
[root@m-k8s ~]#
[root@m-k8s ~]# docker run -d -p 8081:80 -v /root/html:/usr/share/nginx/html --restart always --name nbm nginx
f50fee33e9d7c5a0f1cd9d9449b3e096a4ecf4e700d3bba15efb27104fd10808
[root@m-k8s ~]#
[root@m-k8s ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
f50fee33e9d7   nginx          "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   0.0.0.0:8081->80/tcp, :::8081->80/tcp   nbm
[root@m-k8s ~]#

바인드 마운트는 로컬와 컨테이너 를 연결하는 것으로 

바운드 마운트 설정(-v)에 따라 nginx 컨테이너 내부 경로를 호스트 디렉터리 /root/html에 동기화 시킴

-> index.html을 호출하려하지만 파일이 없음

[root@m-k8s ~]# cd /root/html
[root@m-k8s html]# ls
[root@m-k8s html]# vim index.html
[root@m-k8s html]# cat index.html
<!DOCTYPE html>
<html> <head> <h1> Company Registration Form</h1>
</head>
<body>
<form>
<table> <tr> <td> Email Address: </td> <td> <input type=”text” email=””> </td> </tr> <tr> <td> Password: </td> <td> <input type=”Password” name=””> </td> </tr> </table>
</form>
</body>
</html>
[root@m-k8s html]#

로컬 경로인 /root/html에 index.html을 작성하여 nginx컨테이너로 부터 마운트되어 접속 성공.

 

 

[root@m-k8s html]# docker exec 4c0a ls /usr/share/nginx/html
index.html

docker exec -it 컨테이너ID /bin/bash

exec -> 조건을 가지고 찾은 파일들을 대상으로 다음 명령어를 실행

i -> interactive

t -> tty pseudo(TTY 가상콘솔)

 

평시 상호간에 동기화는 이루어지고,  최초설정(OverWrite)시에는 컨테이너->로컬 작성시 기존 로컬내용이 삭제

 

 

 

 

# 볼륨으로 호스트와 컨테이너 연결하기

[root@m-k8s html]# docker volume create nginx-volume
nginx-volume # 
[root@m-k8s html]#
[root@m-k8s html]# docker volume inspect nginx-volume
[
    {
        "CreatedAt": "2023-05-11T05:44:54Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/nginx-volume/_data",
        "Name": "nginx-volume",
        "Options": null,
        "Scope": "local"
    }
]
[root@m-k8s html]#
[root@m-k8s html]# ls /var/lib/docker/volumes/nginx-volume/_data/
[root@m-k8s html]#
# 마운트포인트 경로는 비워져있음
[root@m-k8s html]# docker run -d -v nginx_volume:/usr/share/nginx/html -p 8082:80 \
--restart always --name nginx-volume nginx # nginx를 컨테이너로 올리기 위해 nginx_volume에 nginx측 경로로 넣어줌
92fcd8bad8c9b5435cb9408abee841950af3a609a3546b7caeee4e3661c9e1ae
[root@m-k8s html]#
[root@m-k8s html]#
[root@m-k8s html]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
92fcd8bad8c9   nginx          "/docker-entrypoint.…"   3 seconds ago    Up 2 seconds    0.0.0.0:8082->80/tcp, :::8082->80/tcp   nginx-volume
f50fee33e9d7   nginx          "/docker-entrypoint.…"   30 minutes ago   Up 30 minutes   0.0.0.0:8081->80/tcp, :::8081->80/tcp   nbm
[root@m-k8s html]#

 

Lab_8083포트, nginx 컨테이너 생성, 단 볼륨은 앞과 다른 nginx_volume

[root@m-k8s html]# docker run -d -v nginx_volume:/usr/share/nginx/html -p 8083:80 --restart always --name LAB nginx
33dae2eab8657031ef8a46960dced28412e871935961c85bd1bff3d5c202956d
[root@m-k8s html]#
[root@m-k8s html]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
33dae2eab865   nginx          "/docker-entrypoint.…"   10 seconds ago   Up 8 seconds    0.0.0.0:8083->80/tcp, :::8083->80/tcp   LAB
92fcd8bad8c9   nginx          "/docker-entrypoint.…"   5 minutes ago    Up 4 minutes    0.0.0.0:8082->80/tcp, :::8082->80/tcp   nginx-volume
f50fee33e9d7   nginx          "/docker-entrypoint.…"   35 minutes ago   Up 35 minutes   0.0.0.0:8081->80/tcp, :::8081->80/tcp   nbm
[root@m-k8s html]# docker volume inspect nginx_volume
[
    {
        "CreatedAt": "2023-05-11T05:59:08Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/nginx_volume/_data",
        "Name": "nginx_volume",
        "Options": null,
        "Scope": "local"
    }
]
[root@m-k8s html]# ls /var/lib/docker/volumes/nginx_volume/_data/
50x.html  index.html
[root@m-k8s html]#

 

 

볼륨, 바인드 마운트 모두 컨테이너가 중지 되더라도 파일이 유지된다.

도커 컨테이너 생성 시 권장되는 방법은 volume이다

 

 

[root@m-k8s html]# docker ps -f ancestor=nginx
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS             PORTS                                   NAMES
33dae2eab865   nginx     "/docker-entrypoint.…"   49 minutes ago      Up 49 minutes      0.0.0.0:8083->80/tcp, :::8083->80/tcp   LAB
92fcd8bad8c9   nginx     "/docker-entrypoint.…"   54 minutes ago      Up 54 minutes      0.0.0.0:8082->80/tcp, :::8082->80/tcp   nginx-volume
f50fee33e9d7   nginx     "/docker-entrypoint.…"   About an hour ago   Up About an hour   0.0.0.0:8081->80/tcp, :::8081->80/tcp   nbm

nginx 이미지를 가진 컨테이너 찾기

 

[root@m-k8s html]# docker stop $(docker ps -a -q)

모든 컨테이너 중지(-a 전부, -q 컨테이너 ID만 표시)

[root@m-k8s html]# docker ps -q -f ancestor=nginx

-f 필터링 옵션 ancestor필드가 nginx 인거만 보여줘라

 

 

[root@m-k8s html]# docker stop $(docker ps -a -q)
2a6337f7827c
2efb2952c9ae
[root@m-k8s html]# docker volume prune -a
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Volumes:
nginx_volume
qwervolume
volume1
volume2
[root@m-k8s html]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
2a6337f7827ca1cf1edfa7675d401956b49924a3f22c428aec915aae0631e1a0
2efb2952c9ae6e942328c53347f1d995b0546728fc9c01f765ef730824e99fe1
[root@m-k8s html]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: nginx:latest
untagged: nginx@sha256:480868e8c8c797794257e2abd88d0f9a8809b2fe956cbfbc05dcc0bca1f7cd43
deleted: sha256:448a08f1d2f94e8db6db9286fd77a3a4f3712786583720a12f1648abb8cace25
deleted: sha256:6b33c8bf5207fd88b6e0f942c230c59477990205dbed0ae41d54b5b29ed1051d
deleted: sha256:a673eda43a02c5a8218e8be171c43912dc9646d588a881a463be970b7f06abf0
deleted: sha256:e22652bd991fd7a83155d12651d319458cb233d428ca769323ecb0b1d6549844
deleted: sha256:77350fbf9b519374ed1eee1c2387b1c9af0c7f048d11794fe172006323834954
deleted: sha256:556cbc099a5c304d0f2fed44d6d153b7d74be08fce2b4ffe74b1183b75c5cae6
deleted: sha256:8553b91047dad45bedc292812586f1621e0a464a09a7a7c2ce6ac5f8ba2535d7

전체 삭제

+ Recent posts