๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/TIL(Today I Learned)

TIL_220707_docker

  • -

Docker๋ž€?

ocker ์ปจํ…Œ์ด๋„ˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ชจ๋“  ์ฝ”๋“œ ๋ฐ ์ข…์†์„ฑ์„ ํ‘œ์ค€ ํ˜•์‹์œผ๋กœ ํŒจํ‚ค์ง•ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ปดํ“จํŒ… ํ™˜๊ฒฝ ์ „๋ฐ˜์—์„œ ๋น ๋ฅด๊ณ  ์•ˆ์ •์ ์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์ฃ . Docker ์ปจํ…Œ์ด๋„ˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ์‹œ์Šคํ…œ ๋„๊ตฌ, ์ฝ”๋“œ, ๋Ÿฐํƒ€์ž„ ๋“ฑ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰์— ํ•„์š”ํ•œ ๋ชจ๋“  ๊ฒƒ์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ธ๊ธฐ ์žˆ๋Š” ๊ฒฝ๋Ÿ‰์˜ ๋…๋ฆฝํ˜• ์‹คํ–‰ ์ปจํ…Œ์ด๋„ˆ์ž…๋‹ˆ๋‹ค. Docker๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ปจํ…Œ์ด๋„ˆํ™”๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋น ๋ฅด๊ฒŒ ๋นŒ๋“œ, ํ…Œ์ŠคํŠธ ๋ฐ ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์†Œํ”„ํŠธ์›จ์–ด ํ”Œ๋žซํผ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

[์ถœ์ฒ˜] : https://www.oracle.com/kr/cloud/cloud-native/container-registry/what-is-docker/

 

Docker ๋ช…๋ น์–ด reference

https://docs.docker.com/engine/reference/commandline/cli/

 

Use the Docker command line

 

docs.docker.com

 

Docker container ์ƒํƒœ ํ™•์ธํ•˜๊ธฐ

docker ps
  • - CONTAINER_ID: container์˜ ๊ณ ์œ  ID
  • - IMAGE: container๋ฅผ ๋„์šฐ๋Š”๋ฐ ์‚ฌ์šฉํ•œ image ์ด๋ฆ„
  • - COMMAND: container์—์„œ ์‹คํ–‰ํ•œ command
  • - CREATED: conatiner๊ฐ€ ์ƒ์„ฑ๋œ ์‹œ๊ฐ„
  • - STATUS: container์˜ ์ƒํƒœ
  • - PORT: port๋ฅผ publish ํ•˜๋Š” ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ๋˜์–ด ์žˆ๋Š”์ง€
  • - NAMES: container์˜ ์ด๋ฆ„

 

docker container ๋ฉˆ์ถ”๊ธฐ

docker stop {container_id|conatinaer_name}
docker kill {container_id|conatinaer_name}

- stop์˜ ๊ฒฝ์šฐ์—๋Š” SIGTERM์ด๋ผ๋Š” ์‹ ํ˜ธ๋ฅผ ๋ณด๋ƒ„
- kill์˜ ๊ฒฝ์šฐ์—๋Š” SIGKILL์ด๋ผ๋Š” ์‹ ํ˜ธ๋ฅผ ๋ณด๋ƒ„

 

docker container ๋˜์‚ด๋ฆฌ๊ธฐ

docker restart {container_id|container_name}

 

์‹คํ–‰์ค‘์ธ container์—์„œ ๋ช…๋ น์–ด ์‹คํ–‰ํ•˜๊ธฐ

docker exec [options] {container_id|container_name} [command]

 

docker container ์‚ญ์ œํ•˜๊ธฐ

docker rm {container_id}

 



Docker๋กœ ์‹คํ–‰

1. Dockerfile ์ƒ์„ฑ

FROM python:3.9.13  # docker image ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•  base image ๋ฅผ ์ ์‘ ๋ถ€๋ถ„

ADD requirements.txt .  # ADD src dst ํ˜ธ์ŠคํŠธ ๋จธ์‹ ์— ์žˆ๋Š” ํŒŒ์ผ์ด๋‚˜ ํด๋”๋ฅผ dst ๋ผ๋Š” ์œ„์น˜์— ์ €์žฅ

RUN pip install -r requirements.txt  # RUN script๋Š” script ๋ฅผ ์‹คํ–‰

ADD templates templates

ADD app.py .

CMD ["python", "app.py"]  # ์ƒ์„ฑ๋œ docker image ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์ปค๋งจ๋“œ
 

 

2. Docker image build

  • -t ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด image์— ์›ํ•˜๋Š” ์ด๋ฆ„์„ ๋ถ™์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. {image_name}:{tag} ์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, {tag} ๋ฅผ ๋ถ™์ด์ง€ ์•Š์„๊ฒฝ์šฐ ์ž๋™์œผ๋กœ latest ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
  • . ๋Š” docker build๋ฅผ ์–ด๋Š ์œ„์น˜์—์„œ ์‹คํ–‰ํ•  ๊ฒƒ์ธ์ง€ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ์œ„์น˜์— ๋”ฐ๋ผ ADD ์ปค๋งจ๋“œ์—์„œ ํ˜ธ์ŠคํŠธ์˜ ํŒŒ์ผ ์œ„์น˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ๋ฐ”๋€” ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
docker build [OPTIONS] PATH
=
docker build -t docker_prac:version1 .

 

3. Docker image ๋ชฉ๋ก ํ™•์ธ

docker images

docker images ๋ช…๋ น์–ด ์‹คํ–‰

 

4. Docker image ์‹คํ–‰

docker run docker-memo:version1

 

5. ์ƒˆ๋กœ์šด ๋ฒ„์ „(์ˆ˜์ •์‚ฌํ•ญ ์ ์šฉ๋œ)์˜ Docker image build

  • -t ์˜ต์…˜์„ ์‚ฌ์šฉํ•  ๋•Œ ๋™์ผํ•œ image_name:tag  ๋ฅผ ์‚ฌ์šฉํ• ๊ฒฝ์šฐ override๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์กฐ์‹ฌํ•˜์„ธ์š”!
docker build . -t docker-memo:version2

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.