이전 글 👇
2024.02.06 - [IT/DevOps] - [Docker] Container의 filesystem, Database 유지하기: 1. Volume mount
[Docker] Container의 filesystem, Database 유지하기: 1. Volume mount
이전 글 👇 2024.02.05 - [IT/DevOps] - [Docker] Application 공유하기: Docker registry, Docker Hub [Docker] Application 공유하기: Docker registry, Docker Hub 이전 글 👇 2024.02.05 - [IT/DevOps] - [Docker] Application 업데이트하기 [Doc
intoky.tistory.com
Bind mount

bind mount는 host filesystem의 특정 directory와 container를 연결해줍니다.
따라서, host filesystem의 특정 directory에서 소스코드를 수정하여 저장하게 되면, 이러한 변화는 Container에 즉시 반영됩니다.
✅ host filesystem과 container의 directory 공유 실습
getting-started-app 디렉토리 위치에서 아래 명령어를 입력하면,
container filesystem의 root 디렉토리 위치에서 ubuntu의 bash가 시작됩니다.
docker run -it --mount "type=bind,src=%cd%,target=/src" ubuntu bash
src: host machine에서의 현재 작업 디렉토리 (getting-started-app)
target: 위 디렉토리가 container 내에서 나타날 위치 (/src)
아래 명령어를 통해, src 디렉토리로 위치를 변경한 뒤 폴더에 뭐가 들었는지 확인해보면,
host machine의 getting-started-app 폴더 내부와 동일합니다!
root@???:/# cd src root@???:/src# ls
container → host
현재 위치에서, 아래 명령어를 통해 myfile.txt를 생성해봅시다.
root@???:/src# touch myfile.txt
이제 host에서 getting-started-app 폴더를 보면, myfile.txt가 새로 생성되어 있는 것을 확인할 수 있습니다!
host → container
host의 getting-started-app 폴더에서, myfile.txt를 삭제해보세요.
그리고 이번엔 아래 명령어를 통해 다시 container에서 src 폴더 내부를 확인해 보면, myfile.txt가 사라졌음을 확인할 수 있습니다!
root@???:/src# ls
interactive session은 Ctrl + D로 종료할 수 있습니다.
✅ application 업데이트 실습: 소스코드 변경이 container에 즉시 반영됨
getting-started-app 디렉토리 위치에서, 아래 명령어를 통해 bind mount가 된 Container를 시작해 봅시다.
docker run -dp 127.0.0.1:3000:3000 \ -w /app --mount type=bind,src="$(pwd)",target=/app \ node:18-alpine \ sh -c "yarn install && yarn run dev"
host machine의 src/static/js/app.js 파일에서, 109번째 라인을 아래와 같이 수정한 뒤 저장해주세요.
- {submitting ? 'Adding...' : 'Add Item'} + {submitting ? 'Adding...' : 'Add'}
브라우저를 새로고침하면, 위 수정 내용이 즉시 반영되어 있음을 확인할 수 있습니다.
※ host machine에서의 변화가 container에 즉시 반영된 것은 bind mount 덕분이지만,
container의 변화에 따라서 Application이 즉시 업데이트된 것은 Nodemon 덕분입니다.

소스코드를 원하는대로 수정한 후, 마무리되었다면 이것을 이제 새 Image로서 build하면 됩니다.
docker build -t getting-started .
감사합니다~!
'컴퓨터과학 > DevOps' 카테고리의 다른 글
[GitHub] 새 레포지토리에 로컬 폴더 업로드하기 (0) | 2024.02.18 |
---|---|
[Docker] multi-container application: Docker compose (0) | 2024.02.07 |
[Docker] Container의 filesystem, Database 유지하기: 1. Volume mount (0) | 2024.02.06 |
[Docker] Application 공유하기: Docker registry, Docker Hub (0) | 2024.02.05 |
[Docker] Application 업데이트하기 (2) | 2024.02.05 |