๐ŸŒฑ Infra/Container_Docker

[Docker-Basic (11)] Docker Compuser ์‚ฌ์šฉํ•˜๊ธฐ :P

mini_world 2020. 10. 5. 10:59
๋ชฉ์ฐจ ์ ‘๊ธฐ


 

Docker Compose๋Š”, ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ •์˜ํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.
์ฆ‰, ํ•œ๋ฒˆ์— 2๊ฐœ ์ด์ƒ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ•˜๋‚˜์˜ ์„ธํŠธ์ฒ˜๋Ÿผ ์‹คํ–‰ํ•ด์•ผ ํ•œ๋‹ค๋ฉด Docker Compose๋ฅผ ์‚ฌ์šฉํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ง›๋ณด๊ธฐ๋กœ ํ•œ๋ฒˆ ์ง„ํ–‰ ํ•ด๋ด…์‹œ๋‹ค!


 

๐Ÿณ step 1.  Compose ์„ค์น˜

๋„์ปค ์„ค์น˜ ๋ช…๋ น์–ด๋ถ€ํ„ฐ ๋น ๋ฅด๊ฒŒ ๋‹ค์‹œ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
๊ณต์‹ํ™ˆํŽ˜์ด์ง€์—์„œ๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. Docker ์„ค์น˜

Docker๊ฐ€ ์„ค์น˜๋  OS๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” Centos7 ์— ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค :)

[root@master /]# rpm -qa | grep release
centos-release-7-8.2003.0.el7.centos.x86_64

yum-utils ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

[root@master /]# yum install -y yum-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do

๋„์ปค ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

[root@master /]# yum-config-manager \
     --add-repo \
     https://download.docker.com/linux/centos/docker-ce.repo

Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

์ด์ œ docker-ce, docker-ce-cli, containerd ๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

[root@master /]#  yum install docker-ce docker-ce-cli containerd.io

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
.. ์ƒ๋žต ..
Installed:
  containerd.io.x86_64 0:1.3.7-3.1.el7              docker-ce.x86_64 3:19.03.13-3.el7            
  docker-ce-cli.x86_64 1:19.03.13-3.el7            

Dependency Installed:
  container-selinux.noarch 2:2.119.2-1.911c772.el7_8                                              

Complete!

docker ์„ค์น˜๊ฐ€ ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด, docker ๋ฐ๋ชฌ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[root@master /]# systemctl start docker
[root@master /]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

 

2. Docker Compose ์„ค์น˜

docker Compose์„ ๋‹ค์šด๋ฐ›์Šต๋‹ˆ๋‹ค.

[root@master /]# sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   651  100   651    0     0   1273      0 --:--:-- --:--:-- --:--:--  1273
100 11.6M  100 11.6M    0     0  3077k      0  0:00:03  0:00:03 --:--:-- 5060k

๋ฐ”์ด๋„ˆ๋ฆฌํŒŒ์ผ์— ์‹คํ–‰ ๊ถŒํ•œ์„ ์ฃผ๊ณ , ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ช…๋ น์–ด์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

[root@master /]# sudo chmod +x /usr/local/bin/docker-compose

[root@master /]# sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

docker-compose ๋ช…๋ น์–ด๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ฒ„์ „์„ ํ™•์ธํ•ด๋ด…์‹œ๋‹ค.

[root@master /]#  docker-compose --version

docker-compose version 1.27.4, build 40524192

 

 

 


 

๐Ÿณ  step2. Compose ์‹œ์ž‘ํ•˜๊ธฐ

์ž, ์ด์ œ ํ…Œ์ŠคํŠธ ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ๋„์ปค ์ปดํฌ์ฆˆ๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
๋ชจ๋“  ๋‚ด์šฉ์€ ๋„์ปค ๊ณต์‹ํ™ˆํŽ˜์ด์ง€์— ์ด๋ฏธ ๋‚˜์™€์žˆ๋Š” ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

1. ๊ธฐ๋ณธ ์„ค์ • 

๋จผ์ €, ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ . ๊ทธ ์•ˆ์— ํ…Œ์ŠคํŠธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฃจํŠธ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์‚ฌ์šฉ๋  ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

[root@master /]# mkdir Docker && cd Docker
[root@master Docker]# mkdir compose_test && cd compose_test/

 

2. Application ์ƒ์„ฑ

๋จผ์ €, app.py ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์„ธ์š”.

[root@master /]# vim Docker/compose_test/app.py 

import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

๋‘๋ฒˆ์งธ๋กœ, requirements.txt ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

[root@master compose_test]# vim requirements.txt 

flask
redis

 

3. Dockerfile ์ƒ์„ฑ

์ด๋ฒˆ์—๋Š”, Dockerfile์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 
Dockerfile ์ด๋ž€, ์ด ์ „ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃจ์—ˆ๋˜ ๋‚ด์šฉ์œผ๋กœ, Docker์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ ˆ์‹œํ”ผ๊ฐ€ ์ ํžŒ ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.

[root@master compose_test]# vim Dockerfile 

FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

๊ฐ ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • Python 3.7 ์ด๋ฏธ์ง€๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ•ฉ๋‹ˆ๋‹ค.
  • ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ๋กœ ์„ค์ •ํ•˜์‹ญ์‹œ์˜ค /code.
  • flask๋ช…๋ น์— ์‚ฌ์šฉ๋˜๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค .
  • gcc ๋ฐ ๊ธฐํƒ€ ์ข…์†์„ฑ ์„ค์น˜
  • requirements.txtPython ์ข…์†์„ฑ์„ ๋ณต์‚ฌ ํ•˜๊ณ  ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋ฏธ์ง€์— ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํฌํŠธ 5000์—์„œ ์ˆ˜์‹  ์ค‘์ž„์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.
  • .ํ”„๋กœ์ ํŠธ ์˜ ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ ๋ฅผ .์ด๋ฏธ์ง€ ์˜ workdir ์— ๋ณต์‚ฌํ•˜์‹ญ์‹œ์˜ค .
  • ์ปจํ…Œ์ด๋„ˆ์˜ ๊ธฐ๋ณธ ๋ช…๋ น์„ flask run.

 

4 . docker-compose.yml ํŒŒ์ผ ์ƒ์„ฑ

docker-compose.yml ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

[root@master compose_test]# vim docker-compose.yml 

version: "3.8"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

๊ฐ ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • version: 
  • services: ์‹คํ–‰ํ•  ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.
  • web ์„œ๋น„์Šค (์ปจํ…Œ์ด๋„ˆ)๋Š” ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” Dockerfile๋กœ ๋นŒ๋“œ๋œ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ, ํฌํŠธ๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•ฉ๋‹ˆ๋‹ค (5000:5000)
  • redis ์„œ๋น„์Šค๋Š” Redis ๊ณต๊ฐœ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.  

์ž, ์ด์ œ 4๊ฐœ์˜ ํŒŒ์ผ์„ ๋ชจ๋‘ ์ƒ์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.
tree ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์„œ ๋ณด๋ฉด ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋ณด์ผ๊ฒ๋‹ˆ๋‹ค. ๋งŒ๋“ค๋•Œ ๋Œ€์†Œ๋ฌธ์ž/์˜คํƒ€์— ์ฃผ์˜ํ•˜์„ธ์š”.

[root@master /]# tree /Docker/compose_test/

/Docker/compose_test/
โ”œโ”€โ”€ Dockerfile
โ”œโ”€โ”€ app.py
โ”œโ”€โ”€ docker-compose.yml
โ””โ”€โ”€ requirements.txt

 

5 .  Compose๋กœ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰

์ด์ œ, ๋Œ€๋ง์˜ ๋„์ปค ์ปดํฌ์ฆˆ ์—…! ์ž…๋‹ˆ๋‹ค.

[root@master compose_test]# docker-compose up

Creating network "compose_test_default" with the default driver
Building web
Step 1/10 : FROM python:3.7-alpine
3.7-alpine: Pulling from library/python
df20fa9351a1: Pull complete
36b3adc4ff6f: Pull complete
4db9de03f499: Pull complete
cd38a04a61f4: Pull complete
9a3838385f13: Pull complete
Digest: sha256:9fbee97d521b846689f4dbf0d5f2770c734d4a09e6d0a0991efc916c58970e99
Status: Downloaded newer image for python:3.7-alpine
 ---> 295b051ee125
Step 2/10 : WORKDIR /code
 ---> Running in 6699ada8855b
Removing intermediate container 6699ada8855b
 ---> 9740cfe088f6
Step 3/10 : ENV FLASK_APP=app.py
 ---> Running in a837433ea544
Removing intermediate container a837433ea544
 ---> dc1486d931c3
Step 4/10 : ENV FLASK_RUN_HOST=0.0.0.0
 ---> Running in f18e93d7fddf
Removing intermediate container f18e93d7fddf
 ---> ff9b48cde18c
Step 5/10 : RUN apk add --no-cache gcc musl-dev linux-headers
 ---> Running in 121818e10c32
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/13) Installing libgcc (9.3.0-r2)
(2/13) Installing libstdc++ (9.3.0-r2)
(3/13) Installing binutils (2.34-r1)
(4/13) Installing gmp (6.2.0-r0)
(5/13) Installing isl (0.18-r0)
(6/13) Installing libgomp (9.3.0-r2)
(7/13) Installing libatomic (9.3.0-r2)
(8/13) Installing libgphobos (9.3.0-r2)
(9/13) Installing mpfr4 (4.0.2-r4)
(10/13) Installing mpc1 (1.1.0-r1)
(11/13) Installing gcc (9.3.0-r2)
(12/13) Installing linux-headers (5.4.5-r1)
(13/13) Installing musl-dev (1.1.24-r9)
Executing busybox-1.31.1-r16.trigger
OK: 153 MiB in 48 packages
Removing intermediate container 121818e10c32
 ---> 6dc320445e5f
Step 6/10 : COPY requirements.txt requirements.txt
 ---> 891a41cc48a5
Step 7/10 : RUN pip install -r requirements.txt
 ---> Running in 6ed6c7af768b
Collecting flask
  Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Collecting redis
  Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
Collecting click>=5.1
  Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
Collecting itsdangerous>=0.24
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2>=2.10.1
  Downloading Jinja2-2.11.2-py2.py3-none-any.whl (125 kB)
Collecting Werkzeug>=0.15
  Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-1.1.1.tar.gz (19 kB)
Building wheels for collected packages: MarkupSafe
  Building wheel for MarkupSafe (setup.py): started
  Building wheel for MarkupSafe (setup.py): finished with status 'done'
  Created wheel for MarkupSafe: filename=MarkupSafe-1.1.1-cp37-cp37m-linux_x86_64.whl size=16913 sha256=fdfbd33f72c6753a24891ac61ed9448a83ca12b7f11fc2248164388cf0dea9ea
  Stored in directory: /root/.cache/pip/wheels/b9/d9/ae/63bf9056b0a22b13ade9f6b9e08187c1bb71c47ef21a8c9924
Successfully built MarkupSafe
Installing collected packages: click, itsdangerous, MarkupSafe, Jinja2, Werkzeug, flask, redis
Successfully installed Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.2 itsdangerous-1.1.0 redis-3.5.3
Removing intermediate container 6ed6c7af768b
 ---> da6859d6dc07
Step 8/10 : EXPOSE 5000
 ---> Running in 856d2ddff942
Removing intermediate container 856d2ddff942
 ---> ccc506260293
Step 9/10 : COPY . .
 ---> c336b88dd79c
Step 10/10 : CMD ["flask", "run"]
 ---> Running in b67224eaff17
Removing intermediate container b67224eaff17
 ---> 2a0a01a5be7b

Successfully built 2a0a01a5be7b
Successfully tagged compose_test_web:latest

 

์ด๋ฏธ์ง€๋Š” ์–ด๋–ค๊ฒƒ์ด ์žˆ๋Š”์ง€, ์ปจํ…Œ์ด๋„ˆ๋Š” ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
ํ„ฐ๋ฏธ๋„ ์ฐฝ์„ ํ•˜๋‚˜ ์ผœ์„œ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ์ž˜ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. :)

๋จผ์ € ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

[root@master compose_test]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
compose_test_web    latest              2a0a01a5be7b        14 minutes ago      196MB
redis               alpine              bd71e6db4a54        3 weeks ago         32.2MB
python              3.7-alpine          295b051ee125        3 weeks ago         41.7MB

์ด์ œ ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

[root@master compose_test]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
18012056d099        compose_test_web    "flask run"              14 minutes ago      Up 14 minutes       0.0.0.0:5000->5000/tcp   compose_test_web_1
05f968a0fc78        redis:alpine        "docker-entrypoint.s…"   14 minutes ago      Up 14 minutes       6379/tcp                 compose_test_redis_1

 

์›น๋ธŒ๋ผ์šฐ์ €์—์„œ๋„ ์ž˜ ๋™์ž‘ํ•˜๋„ค์š” ;)

6 .  Compose ํŒŒ์ผ ์ˆ˜์ •ํ•ด๋ณด๊ธฐ

์ด๋ฒˆ์—๋Š”, docker-compose.ymlํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 9~12์ค„์„ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•˜์„ธ์š” :)

[root@master compose_test]# vim docker-compose.yml 

version: "3.8"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
    environment:
      FLASK_ENV: development
  redis:
    image: "redis:alpine"

๊ฐ ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • volumes์€ ํ˜ธ์ŠคํŠธ์˜ ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์˜ /code ๋””๋ ‰ํ† ๋ฆฌ์— ๋งˆ์šดํŠธํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ ๋นŒ๋“œํ•˜์ง€ ์•Š๊ณ ๋„ ์ฝ”๋“œ๋ฅผ ์ฆ‰์‹œ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • envirenment๋Š” FLASK_ENV ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์ž ๋ชจ๋“œ์—์„œ ์‹คํ–‰ํ•˜๋ฉฐ, ๊ฐœ๋ฐœ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

์•„์ง, ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰์ค‘์ด๋ผ๋ฉด ctrl+C ๋ฅผ ๋ˆŒ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ๋‹ค์‹œํ•œ๋ฒˆ docker-compose up์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

[root@master compose_test]# docker-compose up
Recreating compose_test_web_1 ... done
Starting compose_test_redis_1 ... done
Attaching to compose_test_redis_1, compose_test_web_1
redis_1  | 1:C 05 Oct 2020 02:49:34.173 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 05 Oct 2020 02:49:34.173 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 05 Oct 2020 02:49:34.173 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis_1  | 1:M 05 Oct 2020 02:49:34.175 * Running mode=standalone, port=6379.
redis_1  | 1:M 05 Oct 2020 02:49:34.175 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 05 Oct 2020 02:49:34.175 # Server initialized
redis_1  | 1:M 05 Oct 2020 02:49:34.176 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1  | 1:M 05 Oct 2020 02:49:34.176 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
redis_1  | 1:M 05 Oct 2020 02:49:34.176 * Loading RDB produced by version 6.0.8
redis_1  | 1:M 05 Oct 2020 02:49:34.176 * RDB age 32 seconds
redis_1  | 1:M 05 Oct 2020 02:49:34.176 * RDB memory usage when created 0.79 Mb
redis_1  | 1:M 05 Oct 2020 02:49:34.176 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 05 Oct 2020 02:49:34.176 * Ready to accept connections
web_1    |  * Serving Flask app "app.py" (lazy loading)
web_1    |  * Environment: development
web_1    |  * Debug mode: on
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
web_1    |  * Restarting with stat
web_1    |  * Debugger is active!
web_1    |  * Debugger PIN: 171-548-060

 

7 .  Application ์ˆ˜์ •ํ•˜๊ธฐ

ํ˜ธ์ŠคํŠธ์˜ ๋””๋ ‰ํ† ๋ฆฌ์™€ ์ปจํ…Œ์ด๋„ˆ์˜ /code ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ๊ณต์œ ๋˜์—ˆ์œผ๋ฏ€๋กœ, ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์‹œ ๋นŒ๋“œํ•˜์ง€ ์•Š๊ณ ๋„ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
app.py๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ํ™•์ธํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. (๋งจ ๋งˆ์ง€๋ง‰ ํ•œ์ค„๋งŒ ๋ฐ”๋€Œ์—ˆ์Šต๋‹ˆ๋‹ค)

[root@master compose_test]# vim app.py 

import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()

#    return 'Hello World! I have been seen {} times.\n'.format(count)
    return 'Hello Docker?! I have been seen {} times.\n'.format(count)

 

๋‹ค์‹œ ๋นŒ๋“œํ•  ํ•„์š”์—†์ด, ์ •๋ง ์ฝ”๋“œ๊ฐ€ ๋ฐ”๋กœ ๋ฐ˜์˜์ด ๋์Šต๋‹ˆ๋‹ค.

8 .  ๋‹ค๋ฅธ ๋ช…๋ น์–ด ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ํ•˜๊ธฐ์œ„ํ•ด์„œ -d ์˜ต์…˜์„ ๋„ฃ์–ด ์‹คํ–‰ํ•˜๊ณ , docker-compose ps ๋ช…๋ น์œผ๋กœ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[root@master compose_test]# docker-compose up -d
Starting compose_test_web_1   ... done
Starting compose_test_redis_1 ... done

์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ—ค๋ด…์‹œ๋‹ค.

[root@master compose_test]# docker-compose ps
        Name                      Command               State           Ports         
--------------------------------------------------------------------------------------
compose_test_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp              
compose_test_web_1     flask run                        Up      0.0.0.0:5000->5000/tcp

 

docker-compose run  ๋ช…๋ น์–ด๋Š” ์„œ๋น„์Šค์— ๋Œ€ํ•œ ์ผํšŒ์„ฑ ๋ช…๋ น์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ๋“ค์–ด docker-compuse run web env ๋ช…๋ น์œผ๋กœ web์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[root@master compose_test]# docker-compose run web env

Creating compose_test_web_run ... done
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=6ff69d334701
TERM=xterm
FLASK_ENV=development
LANG=C.UTF-8
GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D
PYTHON_VERSION=3.7.9
PYTHON_PIP_VERSION=20.2.3
PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/fa7dc83944936bf09a0e4cb5d5ec852c0d256599/get-pip.py
PYTHON_GET_PIP_SHA256=6e0bb0a2c2533361d7f297ed547237caf1b7507f197835974c0dd7eba998c53c
FLASK_APP=app.py
FLASK_RUN_HOST=0.0.0.0
HOME=/root

 

docker-compose stop ๋ช…๋ น์–ด๋Š” ํ˜„์žฌ compose๋กœ ์šด์˜์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€ํ•ฉ๋‹ˆ๋‹ค.
์ค‘์ง€ํ•ด๋ด…์‹œ๋‹ค :)

[root@master compose_test]# docker-compose stop
Stopping compose_test_web_1   ... done
Stopping compose_test_redis_1 ... done

์ปจํ…Œ์ด๋„ˆ์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด Exited ์ž…๋‹ˆ๋‹ค. 

[root@master compose_test]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
6ff69d334701        compose_test_web    "env"                    42 minutes ago      Exited (0) 42 minutes ago                       compose_test_web_run_e394dfbcb003
9739bd2f069d        compose_test_web    "flask run"              58 minutes ago      Exited (0) 26 seconds ago                       compose_test_web_1
05f968a0fc78        redis:alpine        "docker-entrypoint.s…"   2 hours ago         Exited (0) 26 seconds ago                       compose_test_redis_1

 

docker-compose down ๋ช…๋ น์–ด๋Š” ๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘๋‹จํ•˜๊ณ  ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. --volumes ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ๋„์ปค ๋ณผ๋ฅจ๋„ ํ•จ๊ป˜ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค.

[root@master compose_test]# docker-compose down --volumes
Removing compose_test_web_run_e394dfbcb003 ... done
Removing compose_test_web_1                ... done
Removing compose_test_redis_1              ... done
Removing network compose_test_default

์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ณด๋‹ˆ  ์ „๋ถ€ ์ž˜ ์ง€์›Œ์กŒ๋„ค์š” :)

[root@master compose_test]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์—ฌ๊ธฐ๊นŒ์ง€ ์ž…๋‹ˆ๋‹ค.
๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š ๋ฟ…โญ๏ธ

 

 

์ฐธ์กฐ ๋งํฌ
www.44bits.io/ko/post/almost-perfect-development-environment-with-docker-and-docker-compose
docs.docker.com/compose/
728x90