Docker Compose λ?
- μμ§λ ₯ μλ μ ν리μΌμ΄μ μΌλ‘ ν¨κ» μλνλ μ¬λ¬ 컨ν μ΄λλ₯Ό μ€ννλ €λ λ‘컬 κ°λ° λ° ν μ€νΈ νκ²½μ μν΄ μ€κ³λ λꡬ
- docker compose yamlμ½λλ μ¬λ¬ κ°μ docker run μ€νκ³Ό μ μ¬νλ©° λ€νΈμν¬, λ³Όλ₯¨ λ±μ νλ²μ μμ±ν μ μλ€.
- docker composeλ‘ μμ±λ 컨ν μ΄λλ€μ λ 립λ λ€νΈμν¬λ‘ ꡬμ±λλ―λ‘ μ»¨ν μ΄λ κ° ν΅μ μ΄ μ½λ€.
- Docker file μμ± -> docker-compose.yml μμ± -> docker compose up
- μ₯μ
- μλ‘ λ€λ₯Έ OSνκ²½μ΄λΌλ λμΌν ν경ꡬμ±μ΄ κ°λ₯νλ€.
- λμΌν νκ²½μ μ¬μ©νκΈ° λλ¬Έμ κ°λ°νκ²½μ μ΄μκ° λ°μν΄λ νκ° μν΅μ΄ μ½λ€.
- 볡μ‘ν νκ²½λ YAML μ½λλ‘ μ€ν¬λ¦½νΈν ν μ μκΈ° λλ¬Έμ μλνκ° κ°λ₯νλ€.
- docker compose CLIλ₯Ό μ΄μ©νμ¬ μ½κ² "λ©ν° 컨ν μ΄λ μ ν리μΌμ΄μ "μ κ΄λ¦¬ν μ μλ€.
- λ¨μ
- λμμ λ€μμ 컨ν μ΄λ μλΉμ€λ₯Ό μννλ κ²½μ° μμ νμ©λ₯ μ΄ μκ° λμμ§ μ μλ€.
Docker compose μ Kubernetesμ μ°¨μ΄
Docker compose νΉμ§
- docker composeλ μ¬λ¬ λ²μ docker cliλ₯Ό μ€ννμ§ μκ³ , νλ²μ κ΄λ ¨ μ ν리μΌμ΄μ λ€μ YAMLνμΌλ‘ ꡬμ±νμ¬ λ΄λΆ νκ²½ ꡬμ±κ³Ό μμ±μ μ€νν μ μλ€.
- μ€μ κ°μ μΊμ±νκΈ° λλ¬Έμ μ¬μμ μ λ³κ²½μ΄ μλ€λ©΄ μΊμ±λ μ 보λ₯Ό κ·Έλλ‘ μ¬μ©νμ¬ λΉ λ₯Έ μλΉμ€ μ€νμ 보μ₯ν μ μλ€.
- YAML μ½λμ ν¬ν¨λ μ ν리μΌμ΄μ λ€μ λμΌ λ€νΈμν¬μ ν¬ν¨λκΈ° λλ¬Έμ 볡μ‘ν μ°κ²° κ΅¬μ± μμ΄λ μ½κ² API ν΅μ μ΄ κ°λ₯νλ€.
μμ£Ό μ°λ Docker compose λ¬Έλ²
build
mydiary-front:
build:
context: ./my-diary-front #Dockerfile μμΉ (λμΌ κ²½λ‘λ©΄ μλ΅ κ°λ₯)
dockerfile: Dockerfile #μ 곡νλ Dockerfile μ΄λ¦
container_name
- μλ΅ μ μλμΌλ‘ λΆμ¬, "λλ ν 리λͺ _μλΉμ€λͺ _n"
- docker runμ --name μ΅μ κ³Ό λμΌ
ports
- μλΉμ€ λ΄λΆ ν¬νΈμ μΈλΆ νΈμ€νΈ ν¬νΈλ₯Ό μ§μ νμ¬ λ°μΈλ. μΈλΆ λ ΈμΆ ν¬νΈ μ§μ
- docker runμ -p μ΅μ κ³Ό λμΌ
networks
- μ΅μμ λ 벨μ networksμ μ μλ λ€νΈμν¬ μ΄λ¦μ μμ±
- docker runμ --net(--network) μ΅μ κ³Ό λμΌ
volumes
- μλΉμ€ λ΄λΆ λλ ν 리μ νΈμ€νΈ λλ ν 리λ₯Ό μ°κ²°νμ¬ λ°μ΄ν° μ§μμ± μ€μ .
- docker runμ -v
environment
- μλΉμ€ λ΄λΆ νκ²½ λ³μ μ€μ
- νκ²½ λ³μκ° λ§μ κ²½μ°, νμΌ (.env)λ‘ λ§λ€μ΄ μ§μ (env_file: ./envfile.env)
command
- μλΉμ€κ° ꡬλ μ΄ν μ€νν λͺ λ Ήμ΄ μμ±
- docker runμ λ§μ§λ§μ μμ±λλ λͺ λ Ήμ΄
restart
- μλΉμ€ μ¬μμ μ΅μ μ§μ
- docker runμ --restart μ΅μ κ³Ό λμΌ
depends_on
- μλΉμ€ κ°μ μ’ μμ±μ μλ―Έ.
- λ¨Όμ μ€νν΄μΌ νλ μλΉμ€λ₯Ό μ§μ νμ¬ μμ μ§μ
deploy.replicas
- ν΄λΉ μλΉμ€μ 볡μ 컨ν μ΄λ μ μ§μ
- replicaλ₯Ό μ¬μ©ν λ 컨ν μ΄λ μ΄λ¦μ μ§μ νλ©΄ μ λλ€. (μ μΌν΄μΌ νκΈ° λλ¬Έ)
μμ1 Docker compose file [Nodejs+ Spring + MySQL + Nginx]
docker-compose.yml
version: '3.8'
services:
mydiary-db:
image: mysql:5.7-debian
container_name: rolling-db
environment:
MYSQL_ROOT_PASSWORD: pass123
MYSQL_DATABASE: paperdb
MYSQL_ROOT_HOST: '%'
MYSQL_USER: user
MYSQL_PASSWORD: user
ports:
- '3306:3306'
networks:
- mydiary-net
restart: always
command:
- --character-set-server=utf8
- --collation-server=utf8_general_ci
mydiary-back:
build:
context: ./my-diary-back
dockerfile: Dockerfile
deploy:
replicas: 3
restart: always
depends_on:
- mydiary-db
ports:
- '8081-8083:8080'
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://rolling-db:3306/paperdb?serverTimezone=Asia/Seoul
SPRING_DATASOURCE_USERNAME: user
SPRING_DATASOURCE_PASSWORD: user
networks:
- mydiary-net
mydiary-front:
build:
context: ./my-diary-front
dockerfile: Dockerfile
deploy:
replicas: 3
restart: always
depends_on:
- mydiary-back
ports:
- '3000-3002:3000'
networks:
- mydiary-net
proxy-be:
image: nginx:1.21.5-alpine
container_name: rolling-server-lb
restart: always
depends_on:
- mydiary-back
ports:
- '8080:80'
volumes:
- ${PWD}/proxy/nginx-be.conf:/etc/nginx/nginx.conf
networks:
- mydiary-net
proxy-fe:
image: nginx:1.21.5-alpine
container_name: rolling-front-lb
restart: always
ports:
- '80:80'
volumes:
- ${PWD}/proxy/nginx-fe.conf:/etc/nginx/nginx.conf
networks:
- mydiary-net
networks:
mydiary-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.20.0.0/24
ip_range: 172.20.0.0/24
gateway: 172.20.0.1
Nginx backend conf
- μμμ μ€μ ν λ°±μλ 컨ν μ΄λ service nameμΈ "mydiray-back" μ μ¬μ©ν΄μΌ νλ€.
- νλ‘ νΈμμ νΈλν½μ μ λ¬λ°μ λ mydiary-backμλ€ νλμ© μ λ¬ν μ μκΈ° λ.
events { worker_connections 1024; }
http{
upstream mydiary-back { # upstreamμ μ΄λ¦μ rolling-serverλ‘ μ€μ
server mydiary-back:8081;
server mydiary-back:8082;
server mydiary-back:8083;
}
server {
listen *:8080 default_server; # ν΄λΌμ΄μΈνΈκ° μμ²νλ ν¬νΈ λ²νΈ
location / {
proxy_pass http://mydiary-back; # μ€μ ν upstreamμΌλ‘ μμ² λ³΄λ΄κΈ°
}
}
}
Nginx front conf
events { worker_connections 1024; }
http {
upstream front-servers {
server mydiary-front:3000;
server mydiary-front:3001;
server mydiary-front:3002;
}
server {
listen 80 default_server;
location / {
proxy_pass http://front-servers;
}
}
}
μμ2 Docker compose file [Spring+ MariaDB + Redis]
MariaDB
- /database/Dockerfile
FROM mariadb:10
ENV TZ=Asia/Seoul
- /database/config/mariadb.cnf
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
[mysqldump]
default-character-set=utf8mb4
Redis
- /redis/Dockerfile
FROM redis:6
ENV TZ=Asia/Seoul
Spring
- ./Dockerfile
FROM openjdk:11
ARG JAR_FILE=build/libs/app.jar
COPY ${JAR_FILE} ./app.jar
ENV TZ=Asia/Seoul
ENTRYPOINT ["java", "-jar", "./app.jar"]
docker-compose.yml
- ./docker-compose.yml
version: "3.8" # νμΌ κ·κ²© λ²μ
services: # μ΄ νλͺ© λ°μ μ€ννλ €λ 컨ν
μ΄λλ€μ μ μ
pharmacy-recommendation-redis: # μλΉμ€λͺ
container_name: pharmacy-recommendation-redis # 컨ν
μ΄λ λͺ
build:
dockerfile: Dockerfile
context: ./redis
image: dgjinsu/pharmacy-recommendation-redis
ports:
- "6380:6379"
pharmacy-recommendation-database:
container_name: pharmacy-recommendation-database
build:
dockerfile: Dockerfile
context: ./database
image: dgjinsu/pharmacy-recommendation-database
environment:
- MARIADB_DATABASE=pharmacy-recommendation
- MARIADB_ROOT_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
volumes:
- ./database/config:/etc/mysql/conf.d
- ./database/init:/docker-entrypoint-initdb.d
ports:
- "3307:3306" # μ κ·Ό ν¬νΈ μ€μ (컨ν
μ΄λ μΈλΆ:컨ν
μ΄λ λ΄λΆ)
pharmacy-recommendation-app:
container_name: pharmacy-recommendation-app
build: .
depends_on: # DB, REDIS 컨ν
μ΄λκ° μ€νλ λ€μ WEBμ μ€νμν¨λ€.
- pharmacy-recommendation-database
- pharmacy-recommendation-redis
image: dgjinsu/pharmacy-recommendation-app
environment:
- SPRING_DATASOURCE_USERNAME=${SPRING_DATASOURCE_USERNAME}
- SPRING_DATASOURCE_PASSWORD=${SPRING_DATASOURCE_PASSWORD}
- SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE}
- KAKAO_REST_API_KEY=${KAKAO_REST_API_KEY}
ports:
- "80:8080"
restart: always # depends onμ μ€ν μμλ§ μ»¨νΈλ‘€ ν λΏ,
# 컨ν
μ΄λ μμ μλΉμ€κ° μ€νκ°λ₯ν μνμΈμ§κΉμ§λ νμΈ νμ§ μκΈ° λλ¬Έμ
# DB λλ Redisκ° μμ§ μ€νκ°λ₯ν μνκ° μλμ¬μ μ€ν¨νλ κ²½μ° μ¬μμ νλλ‘ μ€μ