๐Ÿ”ฅ TIL#15 Docker ์‚ฌ์šฉ๋ฐฉ๋ฒ• ์ •๋ฆฌ

4784 ๋‹จ์–ด dockerdocker

Today I learned

  1. ubuntu์—์„œ Docker ์„ค์น˜
  2. Docker ๋ช…๋ น์–ด
  3. Docker image ๋งŒ๋“ค๊ธฐ
  4. EC2์—์„œ Docker image download ๋ฐ ์‹คํ–‰ํ•˜๊ธฐ

1. ubuntu์—์„œ Docker ์„ค์น˜ํ•˜๊ธฐ.

์•„๋ž˜ ๋ช…๋ น์„ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰.

sudo apt update

sudo apt install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

sudo apt update

apt-cache policy docker-ce

sudo apt install docker-ce

์œ„ ์ ˆ์ฐจ๊ฐ€ ๋ชจ๋‘ ์„ฑ๊ณตํ•˜๋ฉด ๋‹ค์Œ ๋ช…๋ น์„ ํ†ตํ•ด ์„ค์น˜์—ฌ๋ถ€ ํ™•์ธ

Docker version

Docker ์„ค์น˜์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด Client / Server๊ฐ€ ๋ชจ๋‘ ์„ค์น˜๋˜์–ด์•ผ ํ•จ์„ ์œ ์˜ํ•˜์ž.

2. Docker ๋ช…๋ น์–ด

  1. docker ps
    ์‹คํ–‰์ค‘์ธ container๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” command
  1. docker ps -a
    ์‹คํ–‰์ด ์ข…๋ฃŒ๋œ ๊ฒƒ์„ ํฌํ•จ, ๋ชจ๋“  container๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” command ๋ฐ option
  1. docker images
    (์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ download๋œ iamge๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” command)
  1. docker images -a
    (๋ชจ๋“  image ๋ณด์—ฌ์ฃผ๋Š” command ๋ฐ option)
  1. docker build -t {dockerHub์— ๊ฐ€์ž…ํ•œ ๊ณ„์ •๋ช…}/{์ด๋ฏธ์ง€๋ช…(ํ”„๋กœ์ ํŠธ๋ช… ๊ถŒ์žฅ)}:{๋ฒ„์ „}
    (docker image๋ฅผ buildํ•˜๋Š” ๋ช…๋ น์–ด๋กœ Dockerfile ์‚ฌ์šฉ)
  1. docker run -d -p 8000:8000 --name {container name} {image name}
    (docker container ์‹คํ–‰ ๋ช…๋ น์–ด๋กœ ์™„์„ฑ๋œ ์ด๋ฏธ์ง€ ์‚ฌ์šฉ)
    (-d : Deamon ์„ ์˜๋ฏธํ•˜๋ฉฐ ์ง€์†์„ฑ์„ ์˜๋ฏธ(๋งŒ์•ฝ ๋น„์ •์ƒ ์‹คํ–‰์‹œ zombie๊ฐ€ ๋จ)
    (-p 8000:8000: Port-fowarding ์˜๋ฏธ, local port 8000 ์ ‘๊ทผ์‹œ docker container ํฌํŠธ 8000 ์œผ๋กœ ์—ฐ๊ฒฐ ์˜๋ฏธ)
    (--name 'container name': ์ด๋ฆ„ ๋ถ€์—ฌ์˜ต์…˜(์ด๋ฆ„์€ uniqueํ•ด์•ผ ํ•จ))
  1. docker run -it 'image name' /bin/bash
    (docker container๋ฅผ ์‹คํ–‰์‹œํ‚ด๊ณผ ๋™์‹œ์— container์— ์ ‘์†, shell์„ ์‹คํ–‰์‹œํ‚ค๋Š” ๋ช…๋ น)
  1. docker stop {container name}
    (docker container๋ฅผ ์ค‘์ง€)
  1. docker rm {container name}
    (docker container ์ œ๊ฑฐ)
  1. docker rm 'docker os -a -q'
    (docker container ๋ชจ๋‘ ์ œ๊ฑฐ)
  1. docker rmi {image name}
    (docker image ์ œ๊ฑฐ)

3. Docker image ๋งŒ๋“ค๊ธฐ

์‹ค์ œ๋กœ django project๋ฅผ Docker image๋กœ ๋งŒ๋“ค์–ด ๋ณด์ž. image๋กœ ๋งŒ๋“ค์–ด์งˆ ๋Œ€์ƒ์€ ์•„๋ž˜ ๊ณผ์ •์ด ํ•„์ˆ˜์ด๋‹ค.

  1. requirements.txt ์— gunicorn ํŒจํ‚ค์ง€ ๊ธฐ์žฌ.
    • EC2์—์„œ Service ์‹คํ–‰์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๋ชจ๋“ˆ
  2. Dockerfile
    • Dockerfile์€ ์ผ์ข…์˜ ๊ณ„ํš์„œ๋กœ project์˜ ์ตœ์ƒ์œ„์— ์กด์žฌํ•ด์•ผ ํ•˜๋ฉฐ ์•„๋ž˜์™€ ๊ฐ™์€ ๋‚ด์šฉ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
# ./Dockerfile
FROM python:3 
#๊ธฐ๋ฐ˜์ด ๋  ์ด๋ฏธ์ง€(์œ„๋Š” python 3 version์˜ ubuntu๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋ช…๋ น)

# ์ž‘์—…๋””๋ ‰ํ† ๋ฆฌ(default)์„ค์ • (์ฃผ์˜:๊ฐ€์ƒ์ปจํ…Œ์ด๋„ˆ ๋‚ด์˜ ์ฃผ์†Œ์ž„)
WORKDIR /usr/src/app 

## Install packages
#ํ˜„์žฌ ํŒจํ‚ค์ง€ ์„ค์น˜ ์ •๋ณด๋ฅผ ๋„์ปค ์ด๋ฏธ์ง€์— ๋ณต์‚ฌ
COPY requirements.txt ./ 
#์„ค์น˜์ •๋ณด๋ฅผ ์ฝ์–ด ๋“ค์—ฌ์„œ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜
RUN pip install -r requirements.txt

## Copy all src files
#ํ˜„์žฌ๊ฒฝ๋กœ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์†Œ์ŠคํŒŒ์ผ์„ ์ด๋ฏธ์ง€์— ๋ณต์‚ฌ
COPY . . 

## Run the application on the port 8080
#8000๋ฒˆ ํฌํŠธ๋ฅผ ์™ธ๋ถ€์— ๊ฐœ๋ฐฉํ•˜๋„๋ก ์„ค์ •
EXPOSE 8000   

#CMD ["python", "./setup.py", "runserver", "--host=0.0.0.0", "-p 8080"]
#gunicorn์„ ์‚ฌ์šฉํ•ด์„œ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "docker_train.wsgi:application"] 

์œ„ ๊ณผ์ •์ด ์ค€๋น„๋˜๋ฉด ์•„๋ž˜ ์ ˆ์ฐจ๋กœ image ์ƒ์„ฑ

  1. sudo docker build -t [docker id]/[image์ด๋ฆ„]:[๋ฒ„์ ผ]
    (ํ˜„์žฌ ๊ฒฝ๋กœ์— ์žˆ๋Š” Dockerfile์„ ์ฝ์–ด build ํ•œ๋‹ค. docker id๋Š” docker ๊ณ„์ • id)
  1. sudo docker images -a
    (์ƒ์„ฑ๋œ image ํ™•์ธ)

    ์œ„ ์‚ฌ์ง„์—์„  'jinybear/docker_train:0.1' image๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์žˆ๋‹ค.
  1. sudo docker run -d -p 8000:8000 --name docker_train jinybear/docker_train:0.1
    (image ์‹คํ–‰)
  1. sudo docker ps -a
    (์‹คํ–‰์ค‘์ธ container ํ™•์ธ)

    - ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธ์„ ์œ„ํ•ด 'http' ๋˜๋Š” 'postman'์„ ์ด์šฉํ•ด request๋ฅผ ํ•ด ๋ณธ๋‹ค.
  1. sudo docker stop [container id]
    (์‹คํ–‰์ค‘์ธ container ์ค‘์ง€)
  1. sudo docker login
    (dockerHub์— image ๋“ฑ๋ก์„ ์œ„ํ•ด login)
  1. docker push {docker id}/{image์ด๋ฆ„}:{๋ฒ„์ ผ}
    (dockerHub์— image ์˜ฌ๋ฆฌ๊ธฐ)

4. EC2์—์„œ Docker image download ๋ฐ ์‹คํ–‰ํ•˜๊ธฐ

์ด์ œ EC2๋กœ ์ ‘์†ํ•ด Docker image๋ฅผ ๋‹ค์šด๋ฐ›์•„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ์ ˆ์ฐจ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค
1. EC2 ์— Docker ์„ค์น˜ (local ์„ค์น˜๊ณผ์ •๊ณผ ๋™์ผ)
2. Docker login
3. Docker pull {image name}
4. Docker images -a ๋กœ ๋‹ค์šด๋ฐ›์€ ์ด๋ฏธ์ง€ ํ™•์ธ
5. Docker run ์œผ๋กœ ์‹คํ–‰ ํ›„ ๊ธฐ๋Šฅ ํ™•์ธ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ