ํ’€์Šคํƒ ์›น๐ŸŒ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ ๐Ÿง‘๐Ÿฝโ€๐Ÿ’ป
โž• ์ธ๊ณต์ง€๋Šฅ ๊ด€์‹ฌ ๐Ÿค–


Categories


Recent views

  • 1
  • 2
  • 3
  • 4
  • 5

Docker ๊ธฐ๋ณธ

Docker๋กœ ์„œ๋ฒ„ ๊ฐ€์ƒํ™”ํ•ด๋ณด๊ธฐ

์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ํ™˜๊ฒฝ์ด๋‚˜ ์ปค๋„์„ ํ˜ธ์ŠคํŠธ OS๋‚˜ ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์™€ ๊ณต์œ ํ•จ์œผ๋กœ, ๊ธฐ์กด์˜ ํ•˜์ดํผ ๋ฐ”์ด์ € ๊ธฐ๋ฐ˜ ๊ฐ€์ƒํ™”์— ๋น„ํ•ด ๋†’์€ ์„ฑ๋Šฅ์„ ๋‹ฌ์„ฑํ•œ ๊ฐ€์ƒํ™” ํ”Œ๋žซํผ.

Docker ์„ค์น˜

Linux

๋”์šฑ ์ž์„ธํ•œ ๊ฒƒ์€ ๋„์ปค ๊ณต์‹ ๋ฌธ์„œ ์ฐธ์กฐ

์„ค์น˜ ํ™•์ธ

# ๋ณดํ†ต ์•ˆํ•ด๋„ ๋œ๋‹ค
systemctl enable docker
service docker start
service docker status 
# ์•ˆ๋  ๊ฒฝ์šฐ /etc/init.d/docker start
# ์ฃผ๋กœ WSL์ด๋‚˜ Docker container์˜ linux์—์„œ ์ผ์–ด๋‚˜๋Š” ํ˜„์ƒ
docker version

AWS EC2 linux images

1. apt-get ์—…๋ฐ์ดํŠธ ๋ฐ ๋„์ปค ์„ค์น˜

sudo apt-get update # ubuntu ๊ณ„์—ด
sudo apt-get install docker 

sudo yum -y upgrade # red hat ๊ณ„์—ด
sudo yum install docker 

2. ๋„์ปค ์‹คํ–‰

sudo systemctl start docker # docekr ์„œ๋น„์Šค์˜ ์‹œ์ž‘์ด ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
sudo service docker start

docker -v 
Docker version 20.10.13, build a224086

์ดํ›„ docker๋ฅผ ์œ„์™€ ๊ฐ™์ด ์„ค์น˜ํ•œ๋‹ค.

sudo usermod -aG docker $USERNAME

์ดํ›„ ์œ„์™€ ๊ฐ™์ด ํŠน์ • ์‚ฌ์šฉ์ž์—๊ฒŒ ๋„์ปค์˜ ์‹คํ–‰ ๊ถŒํ•œ์„ ์ค€๋‹ค.

Dockerfile ์„ค์ • ๋ฐ ์ด๋ฏธ์ง€ ๋นŒ๋“œ

FROM python:3.10.5

ENV PYTHONUNBUFFERED 1
ENV ON_PRODUCTION FALSE
ENV BACKEND_SECRET_KEY TEMP // ์ž„์‹œ ํ‚ค์„ค์ • ๋‚˜์ค‘์— ๊ผญ ์ œ๋Œ€๋กœ๋œ ํ‚ค๊ด€๋ฆฌ ํˆด๋กœ ๋ฐ”๊พธ๊ธฐ

RUN apt-get -y update
RUN apt-get -y install vim

RUN mkdir /srv/docker-server
ADD . /srv/docker-server

WORKDIR /srv/docker-server

RUN pip install --upgrade pip
RUN pip install -r requirements.txt

EXPOSE 8000 
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

์œ„์™€ ๊ฐ™์ด Dockerfile์„ ํ”„๋กœ์ ํŠธ ํด๋” ๋‚ด์— ์ž‘์„ฑํ•œ ํ›„, sudo docker build -t [repository_name]/[tag_name]:[version] $project_directory์„ ์ด์šฉํ•ด ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•œ๋‹ค.

  • -t: ํƒœ๊ทธ ์‚ฌ์šฉ
sudo docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
server_dev/django   latest    68e495e3bc5b   17 seconds ago   1.04GB

์˜ค๋ฅ˜ ์—†์ด ์ข…๋ฃŒ๋˜๋ฉด ์œ„์™€ ๊ฐ™์ด sudo docker images๋ฅผ ํ†ตํ•ด ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ธฐํƒ€ ์œ ์šฉํ•œ ์ปค๋งจ๋“œ

sudo docker image rm [--force] ImageID : ํŠน์ • ์ด๋ฏธ์ง€ ์ง€์šฐ๊ธฐ

  • --force, -f : ๊ฐ€๋” ์„œ๋กœ ์ฐธ์กฐ ๊ด€๊ณ„๋กœ ์ง€์›Œ์ง€์ง€ ์•Š์„ ๋•Œ ๊ฐ•์ œ๋กœ ์ง€์›€, ๋‹ค๋งŒ ์ด ๊ฒฝ์šฐ ์บ์‰ฌ๊ฐ€ ์ง€์›Œ์ง€๋ฏ€๋กœ ์ด๋ฏธ์ง€ ๋นŒ๋“œ๊ฐ€ ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Œ
    sudo docker image prune : ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์žˆ๋Š” ์ด๋ฏธ์ง€๋“ค ์ผ๊ด„ ์‚ญ์ œ
    sudo docker rm CONTAINERID : ํŠน์ • ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ
    sudo docker stop CONTAINERID : ํŠน์ • ์ปจํ…Œ์ด๋„ˆ ์ค‘๋‹จ
    sudo docker container prune : ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ผ๊ด„ ์‚ญ์ œ

๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰

sudo docker run -p 80:8000 -d server_dev/django

์œ„์˜ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€์˜ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ž…๋ ฅํ•ด ์‹คํ–‰๋œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • -p ์„œ๋ฒ„์ธก ์—ด๋ฆฐ ํฌํŠธ: ์ปจํ…Œ์ด๋„ˆ์ธก ์—ด๋ฆฐ ํฌํŠธ : ์„œ๋ฒ„๋กœ ๋“ค์–ด์˜ค๋Š” ์—ฐ๊ฒฐ์˜ ํฌํŠธ ๋ฒˆํ˜ธ์™€ ์ปจํ…Œ์ด๋„ˆ ์ธก์˜ ํฌํŠธ๋ฅผ ์„œ๋กœ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

  • -d : detached ๋ชจ๋“œ๋กœ, ์„œ๋ฒ„์˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ํ•œ๋‹ค.

sudo docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED         STATUS                     PORTS                                   NAMES
c93671164afa   server_dev/django   "python manage.py ruโ€ฆ"   7 seconds ago   Up 5 seconds               0.0.0.0:80->8000/tcp, :::80->8000/tcp   amazing_pike

์ƒ๊ธฐ ์ปค๋งจ๋“œ๋ฅผ ํ†ตํ•ด ๋Œ์•„๊ฐ€๊ณ  ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋„์ปค ํ—ˆ๋ธŒ

๋„์ปค ํ—ˆ๋ธŒ ๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ์„œ๋ฒ„์™€ ์ด๋ฏธ์ง€๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋„์ปค ํ—ˆ๋ธŒ์˜ ๊ฒฝ์šฐ Repository๋Š” [docker_hub_id]/[ํ—ˆ๋ธŒ ๋‚ด repo ์ด๋ฆ„]์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

  1. docker login -u [id]๋กœ ๋กœ๊ทธ์ธ
  2. docker push [dockerhub_id]/[dockerhub repository]:[push ํƒœ๊ทธ๋ช…]์œผ๋กœ push
  3. docker pull [dockerhub_id]/[dockerhub repository]:[pull ํƒœ๊ทธ๋ช…]์œผ๋กœ pull.
โœจ ๋กœ๊ทธ์ธ์ด ์•ˆ๋œ๋‹ค๋ฉด

gnupg2 : ๋””์ง€ํ„ธ ์„œ๋ช…๊ณผ ์ธ์ฆ์„œ ์•”ํ˜ธํ™” ํˆด

pass: ์Šคํƒ ๋”๋“œ ์œ ๋‹‰์Šค ํŒจ์Šค์›Œ๋“œ ๋งค๋‹ˆ์ €

sudo apt install gnupg2 pass
# ํ˜น์€
{: #ํ˜น์€}
sudo yum install gnupg2 pass

docker-compose

docker-compose๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ปจํ…Œ์ด๋„ˆ์˜ ์—ฐ๊ณ„์™€ ์„ค์ •์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
docker-compose ๊ฐœ๋… ์ฐธ์กฐ