π³ Docker Compose λ€νΈμν¬
πΎ μ¬λ¬ κ°μ 컨ν μ΄λλ‘ κ΅¬μ±λ 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
λκΈλ¨κΈ°κΈ°