내일배움캠프 - 도커 실무 강의 2주차 개발일지

Dongwoo Kim·2022년 7월 13일
0

스파르타 코딩클럽

내일배움캠프 AI 웹개발자양성과정 2회차

도커 실무 강의 2주차 개발일지

1. volume

: docker container가 종료된 후에도 data를 유지하거나 다른 container끼리 data를 공유하기위한 방법 중 하나. 또 다른 방법으로는 bind mount가 있음

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.
- https://docs.docker.com/storage/volumes/

1) create volume

docker volume create {volume_name}
docker volume create memo

2) mongodb와 연결

docker run -p {host_port}:{container_port} -v {volume_name}:{src} {image_name}
docker run -p 27017:27017 -v memo:/data/db mongo

2. bind mount

When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.
- https://docs.docker.com/storage/bind-mounts/

docker run -v {host_path}:{container_path} {image_name}
docker run -v "$(pwd)/templates:/templates" -p 80:5000 docker-memo:version2

※ volume과 bind mount의 차이점

1) bind mount

: 호스트 시스템의 파일 또는 디렉토리가 컨테이너에 직접 마운트됨

2) volume

: 호스트 시스템의 Docker storage 내에 새로운 디렉토리를 생성하여 관리

3) bind mount와 비교하여 volume이 가지는 장점들

Volumes are easier to back up or migrate than bind mounts.
You can manage volumes using Docker CLI commands or the Docker API.
Volumes work on both Linux and Windows containers.
Volumes can be more safely shared among multiple containers.
Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
New volumes can have their content pre-populated by a container.
Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.
- https://docs.docker.com/storage/volumes/

3. Docker Tip

1) Docker layer caching

: Docker image가 빌드될 때 명령어 하나를 실행할 때마다 layer가 새로 생기기때문에 자주 변경되거나 실행시간이 짧은 Dockerfile 명령어는 아래쪽으로 배치하는 것이 좋다.

FROM python:3.8

ADD requirements.txt .

RUN pip install -r requirements.txt

ADD app.py .

ADD templates ./templates/

CMD ["python", "app.py"]

2) dockerignore

: .dockerignore 생성하여 venv와 같은 파일은 image에 담기지 않도록 방지

# .dockerignore

/venv

3) Docker image 축소시키기

: Docker image 크기가 작을수록 빌드 속도가 빨라지고, pull, push 시간이 줄어든다.

python:3.8-alpine

: 작고, 보안에 집중한 image - 16.05MB
하지만 python기준으로 봤을 때, pip install을 할 때 불리한 점이 있음

python:3.8-buster

: python:3.8과 동일 - 332.17MB

python:3.8-slim

: 실행에 필요한 환경만 만들어둔 image - 41.11MB
보통 python 실행환경에서 가장 많이 쓰이는 이미지

4) multistage build

: Dockerfile 한 개에 여러개의 FROM 구문을 두는 방식으로, 각 command를 실행하는 과정에서 생성된 것 중 필요한 것만 가져와서 새 이미지를 생성하게 할 수 있다.

FROM python:3.8-slim AS builder

ADD requirements.txt requirements.txt

RUN pip install -r requirements.txt

FROM python:3.8-slim-buster
COPY --from=builder /usr/local/lib/python3.8/site-packages /usr/local/lib/python3.8/site-packages

ADD templates templates

ADD app.py .

CMD ["python", "app.py"]
profile
kimphysicsman

0개의 댓글