πŸ’Ύ μ—¬λŸ¬ 개의 μ»¨ν…Œμ΄λ„ˆλ‘œ κ΅¬μ„±λœ Docker Compose μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ‚΄μ—μ„œ μ»¨ν…Œμ΄λ„ˆ κ°„μ˜ 톡신을 μ„€λͺ…ν•©λ‹ˆλ‹€.


λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬

기본적으둜 Docker ComposeλŠ” ν•˜λ‚˜μ˜ λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬μ— λͺ¨λ“  μ»¨ν…Œμ΄λ„ˆλ₯Ό μ—°κ²°ν•©λ‹ˆλ‹€. λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬μ˜ 이름은 docker-compose.ymlκ°€ μœ„μΉ˜ν•œ 디렉토리 이름 뒀에 _defaultκ°€ λΆ™μŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, 디렉토리 이름이 our_app라면 λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬ 이름은 our_app_defaultκ°€ λ©λ‹ˆλ‹€.

## /path/our_app 경둜
$ docker-compose up -d

Creating network "our_app_default" with the default driver
Creating our_app_db_1 ... done
Creating our_app_web_1 ... done

Docker Compose둜 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ 내릴 λ•ŒλŠ” λ°˜λŒ€ μˆœμ„œλ‘œ λ¨Όμ € μ»¨ν…Œμ΄λ„ˆλ₯Ό μ’…λ£Œ/μ œκ±°ν•΄λ†“κ³  제일 λ§ˆμ§€λ§‰μ— λ„€νŠΈμ›Œν¬λ₯Ό μ œκ±°ν•©λ‹ˆλ‹€.

$ docker-compose down
Stopping our_app_web_1 ... done
Stopping our_app_db_1  ... done
Removing our_app_web_1 ... done
Removing our_app_db_1  ... done
Removing network our_app_default



μ»¨ν…Œμ΄λ„ˆ κ°„ 톡신

같은 λ„€νŠΈμ›Œν¬ κ°„ 톡신
같은 λ„€νŠΈμ›Œν¬ μ•ˆμ—μ„œ μ»¨ν…Œμ΄λ„ˆ κ°„μ˜ 톡신은 호슀트λͺ…을 μ‚¬μš©ν•©λ‹ˆλ‹€. λ§Œμ•½ 호슀트λͺ…을 μ§€μ •ν•˜μ§€ μ•Šμ•˜λ‹€λ©΄ μ„œλΉ„μŠ€μ˜ 이름이 호슀트λͺ…이 λ©λ‹ˆλ‹€.

$ docker-compose exec web ping db

PING db (192.168.48.2) 56(84) bytes of data.
64 bytes from our_app_db_1.our_app_default (192.168.48.2): icmp_seq=1 ttl=64 time=0.094 ms
64 bytes from our_app_db_1.our_app_default (192.168.48.2): icmp_seq=2 ttl=64 time=0.162 ms


호좜 μœ„μΉ˜μ— λ”°λ₯Έ 톡신
μ»¨ν…Œμ΄λ„Œ κ°„ ν†΅μ‹ μ—μ„œ μ£Όμ˜ν•  점은 μ ‘μ†ν•˜λŠ” μœ„μΉ˜κ°€ λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬ 내뢀냐 외뢀냐에 λ”°λΌμ„œ 포트(port)κ°€ λ‹¬λΌμ§ˆ 수 μžˆλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

services:
  web:
    build: .
    ports:
      - "8001:8000"
# 호슀트 μ»΄ν“¨ν„°μ—μ„œ web μ„œλΉ„μŠ€ μ»¨ν…Œμ΄λ„ˆ 접속
$ curl -I localhost:8001

HTTP/1.1 200 OK
Date: Fri, 05 Jun 2020 02:05:10 GMT
Server: WSGIServer/0.2 CPython/3.8.2
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 16351
X-Content-Type-Options: nosniff
# 같은 λ„€νŠΈμ›Œν¬ λ‚΄μ˜ λ‹€λ₯Έ μ»¨ν…Œμ΄λ„ˆμ—μ„œ web μ„œλΉ„μŠ€ μ»¨ν…Œμ΄λ„ˆ 접속
$ docker-compose exec alpine curl -I web:8000

HTTP/1.1 200 OK
Date: Fri, 05 Jun 2020 02:13:46 GMT
Server: WSGIServer/0.2 CPython/3.8.2
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 16351
X-Content-Type-Options: nosniff



μ»€μŠ€ν…€ λ„€νŠΈμ›Œν¬ μΆ”κ°€

Docker ComposeλŠ” λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬ 뿐만 μ•„λ‹ˆλΌ λ‹€λ₯Έ λ„€νŠΈμ›Œν¬λ„ ν•„μš”μ— 따라 μΆ”κ°€ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

services:
  web:
    build: .
    ports:
      - "8000:8000"
    networks: # μ„œλΉ„μŠ€μ—μ„œ μ—°κ²°ν•  λ„€νŠΈμ›Œν¬ μž‘μ„±
      - default
      - our_net

  db:
    image: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

# λ„€νŠΈμ›Œν¬ μΆ”κ°€
networks:
  our_net:
    driver: bridge

Docker Compose둜 μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μ‹€ν–‰ν•˜λ©΄ λ‘κ°œμ˜ λ„€νŠΈμ›Œν¬κ°€ μƒμ„±λ˜λŠ”κ±Έ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

$ docker-compose up -d
Creating network "our_app_default" with the default driver
Creating network "our_app_our_net" with driver "bridge"
...

$ our_app docker network ls
NETWORK ID          NAME                   DRIVER              SCOPE
f1859120a0c3        bridge                 bridge              local
95b00551745b        host                   host                local
1f7202baa40a        none                   null                local
2682634e6535        our_app_default        bridge              local
525403b38bbe        our_app_our_net        bridge              local

our_net은 Docker Compose λ‚΄λΆ€μ—μ„œ μ •μ˜λœ λ„€νŠΈμ›Œν¬ μ΄λ―€λ‘œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ 내릴 λ•Œ λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬μ™€ ν•¨κ»˜ μ‚­μ œλ©λ‹ˆλ‹€.

$ docker-compose down

Stopping our_app_web_1 ... done
Stopping our_app_db_1  ... done
Removing our_app_web_1 ... done
Removing our_app_db_1  ... done
Removing network our_app_default
Removing network our_app_our_net



μ™ΈλΆ€ λ„€νŠΈμ›Œν¬ μ‚¬μš©

Docker Composeκ°€ μ œκ³΅ν•˜λŠ” λ””ν΄νŠΈ λ„€νŠΈμ›Œν¬ λŒ€μ‹ μ— μ™ΈλΆ€μ—μ„œ 미리 생성해놓은 λ‹€λ₯Έ λ„€νŠΈμ›Œν¬λ₯Ό μ‚¬μš©ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

our_netμ΄λΌλŠ” λ„€νŠΈμ›Œν¬ μƒμ„±ν•©λ‹ˆλ‹€.

$ docker network create our_net
6d791b927c8c151c45a10ac13c62f3571ecf38a90756fd2ca1c62b7d3de804e8

docker-compose.ymlμ—μ„œΒ λ„€νŠΈμ›Œν¬μ˜ μ˜΅μ…˜μ—Β our_netλ„€νŠΈμ›Œν¬μ— external을 μ„€μ •ν•©λ‹ˆλ‹€.

# 1번 λ°©λ²™
version: '3.7'

services:
  web:
    build: .
    ports:
      - "8000:8000"
    networks:
      - our_net

networks:
  our_net:
    driver: bridge
    external: true

κ²½κ³ 
μ™ΈλΆ€μ—μ„œ μƒμ„±λœ λ„€νŠΈμ›Œν¬μ΄λ―€λ‘œ Docker Compose μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ 내릴 λ•Œ ν•΄λ‹Ή λ„€νŠΈμ›Œν¬κ°€ ν•¨κ»˜ μ‚­μ œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

$ docker-compose down

Stopping our_app_web_1 ... done
Stopping our_app_db_1  ... done
Removing our_app_web_1 ... done
Removing our_app_db_1  ... done
Network our_net is external, skipping

레퍼런슀 참고

πŸ™‡πŸ»β€β™‚οΈ μ°Έκ³ μ‚¬μ΄νŠΈ

λŒ“κΈ€λ‚¨κΈ°κΈ°