๋งจ๋ค๋งจ๋ค ๊ท์ฝ๋คใ
๋จ์ผ ์๋ฒ์์ ์ฌ๋ฌ๊ฐ์ ์ปจํ ์ด๋๋ฅผ ํ๋์ ์๋น์ค๋ก ์ ์ํด ์ปจํ ์ด๋์ ๋ฌถ์์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ ์์ ํ๊ฒฝ์ ์ ๊ณตํ๋ ๋๊ตฌ
ex) ๋์์ ๊ณต์ ์ฌ์ดํธ โ ์ ํ๋ฆฌ์ผ์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ storingํ๋ ์๋น์ค, ์ฌ์ฉ์๊ฐ ๋ฌด์ธ๊ฐ๋ฅผ ์ ๋ก๋ ํ ํ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋น๋์ค trascoding์ ์ํ ์๋น์ค, front-end ์๋น์ค.. ๋ฑ๋ฑ.
version: "3"
services:
app:
build:
context: ./01_FLASK_DOCKER
dockerfile: Dockerfile
links:
- "db:mysqldb"
ports:
- "80:8080"
container_name: appcontainer
depends_on:
- db
db:
image: mysql:5.7
restart: always
volumes:
- ./mysqldata:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=dgkcoding
- MYSQL_DATABASE=dgkdb
ports:
- "3306:3306"
container_name: dbcontainer
service ๋ด๋ถ ์นดํ ๊ณ ๋ฆฌ
docker-compose [๋ช ๋ น์ด]
์๋ธ๋ช ๋ น | ์ค๋ช |
---|---|
up | ์ปจํ ์ด๋ ์์ฑ/์์ |
ps/ ls | ์ปจํ ์ด๋ ๋ชฉ๋ก ํ์ |
logs | ์ปจํ ์ด๋ ๋ก๊ทธ ์ถ๋ ฅ |
run | ์ปจํ ์ด๋ ์คํ |
start | ์ปจํ ์ด๋ ์์ |
stop | ์ปจํ ์ด๋ ์ ์ง |
restart | ์ปจํ ์ด๋ ์ฌ์์ |
pause | ์ปจํ ์ด๋ ์ผ์ ์ ์ง |
unpause | ์ปจํ ์ด๋ ์ฌ๊ฐ |
port | ๊ณต๊ฐ ํฌํธ ํ์ |
config | docker-compose.yml ํ์ผ ๊ตฌ์ฑ ํ์ธ |
kill | ์คํ ์ค์ธ ์ปจํ ์ด๋ ๊ฐ์ ์ ์ง |
rm | ์ปจํ ์ด๋ ์ญ์ |
down | ๋ฆฌ์์ค ์ญ์ |
ํฌ๊ฒ 3๋จ๊ณ
(1) ๊ฐ๊ฐ์ ์ปจํ ์ด๋์ Dockerfile๋ฅผ ์์ฑ
(2) docker-compose.yml๋ฅผ ์์ฑํ๊ณ , ๊ฐ๊ฐ ๋ ๋ฆฝ๋ ์ปจํ ์ด๋์ ์คํ ์ ์๋ฅผ ์ค์
(3) "docker-compose up" ์ปค๋งจ๋๋ฅผ ์คํํ์ฌ docker-compose.yml์ผ๋ก ์ ์ํ ์ปจํ ์ด๋๋ฅผ ๊ฐ์
๋ณธ์ธ์ docker desktop์ผ๋ก ๊น์๊ธฐ ๋๋ฌธ์ ์ด๋ฏธ docker compose ํฌํจ๋์ด ์ค์น๋์ด ์์.
(๋ฆฌ๋ ์ค๋ Docker compose๋ฅผ ๋ฐ๋ก ์ค์นํด์ผ)
$ curl -SL https://github.com/docker/compose/releases/download/v2.16.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
DokerCompose ๋๋ ํฐ๋ฆฌ์ client, server ํด๋์ ๊ฐ๊ฐ client.py, server.py์ Dockerfile์์ฑ
**client.py ์ฝ๋**
#!/usr/bin/env python3
# python ์์คํ
๋ผ์ด๋ธ๋ฌ๋ฆฌ import
# ์๋ฒ์์ 'index.html'์ ๋ค์ด๋ฐ๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
# ํ์ด์ฌ๊ณผ ํจ๊ป ์ค์น๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ถ๊ฐ์ ์ผ๋ก ์ค์นํ ๊ฒ์ x
import urllib.request
# 'http://localhost:1234/'์ ๋ํ request๋ฅผ ํฌํจํ๋ ๋ณ์ fp
# localhost: ์๋ฒ๊ฐ local ์๋ฒ
# 1234: ์๋ฒ์ port ์ฃผ์
fp = urllib.request.urlopen("http://localhost:1234/")
# 'encodedContent' ์ธ์ฝ๋ฉ๋ server response ('index.html').
# 'decodedContent' ๋์ฝ๋ฉ๋ server response (what we want to display).
encodedContent = fp.read()
decodedContent = encodedContent.decode("utf8")
# server file: 'index.html'์ถ๋ ฅ
print(decodedContent)
# server ์ฐ๊ฒฐ close
fp.close()
client image์ Dockerfile
FROM python:latest
LABEL mantainer="leeeunda"
#clent ํด๋์ ์คํํ ํ์ผ ์ค์
ADD client.py /client/
#์์
ํ ๋๋ ํฐ๋ฆฌ ์ค์ .
WORKDIR /client/
๋ฐ๋ก ๋ช ๋ น์ด๋ก ์ด๋ฏธ์ง ๋น๋ํ ํ์์์ด ์ดํ์ docker-compose build ๋ช ๋ น์ด๋ก ์ปจํ ์ด๋ ๋ฐ ์ด๋ฏธ์ง ํ ๋ฒ์ ์์ฑํ ๊ฒ
**server.py ์ฝ๋**
#!/usr/bin/env python3
# python ์์คํ
๋ผ์ด๋ธ๋ฌ๋ฆฌ import
# ์๋ฒ์์ 'index.html'์ ๋ค์ด๋ฐ๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
# ํ์ด์ฌ๊ณผ ํจ๊ป ์ค์น๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ถ๊ฐ์ ์ผ๋ก ์ค์นํ ๊ฒ์ x
import http.server
import socketserver
#์๋ฒ์์ client์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ณ์
handler = http.server.SimpleHTTPRequestHandler
# port 1234์์ server๋ฅผ ์์ํ๋๋ก ์ ์
with socketserver.TCPServer(("", 1234), handler) as httpd:
# client์ ์์ฒญ์ ๊ธฐ๋ค๋ฆฌ๋ฉด์ ์๋ฒ๋ฅผ ๊ณ์ ์คํด์๋๋ก ํ๋ ๋ช
๋ น
httpd.serve_foreve# FROM : base image ์ค์
# Dockerhub์์ python image import
# ์ด๋ฏธ์ง ์ด๋ฆ -> python, version-> latest
FROM python:latest
LABEL mantainer="daun0219@gmail.com"
# ์ฒซ๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ host ์ ํ์ผ์ ์ด๋ฆ
# ๋๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ ์ด๋ฏธ์ง์ ๋ฃ์ ํ์ผ์ ๊ฒฝ๋ก
# python code ์คํํ๊ธฐ ์ํด server.py ํ์ผ๊ณผ index.html ํ์ผ import
ADD server.py /server/
ADD index.html /server/
#์์
ํ ํด๋ ์ค์ ๋ช
๋ น์ด๊ฐ ์คํ๋๋ ์์น.
WORKDIR /server/r()
server image์ Dockerfile
#!/usr/bin/env python3
# python ์์คํ
๋ผ์ด๋ธ๋ฌ๋ฆฌ import
# ์๋ฒ์์ 'index.html'์ ๋ค์ด๋ฐ๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
# ํ์ด์ฌ๊ณผ ํจ๊ป ์ค์น๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ถ๊ฐ์ ์ผ๋ก ์ค์นํ ๊ฒ์ x
import http.server
import socketserver
#์๋ฒ์์ client์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๋ณ์
handler = http.server.SimpleHTTPRequestHandler
# port 1234์์ server๋ฅผ ์์ํ๋๋ก ์ ์
with socketserver.TCPServer(("", 1234), handler) as httpd:
# client์ ์์ฒญ์ ๊ธฐ๋ค๋ฆฌ๋ฉด์ ์๋ฒ๋ฅผ ๊ณ์ ์คํด์๋๋ก ํ๋ ๋ช
๋ น
httpd.serve_forever()
index.html ํ์ผ ์์๋ ๊ฐ๋จํ๊ฒ hello docker-compose.. ๋ญ ์ด๋ฐ๊ฑฐ ์ ์
๋ธ๋ผ์ฐ์ ๋ฅผ ํตํด local host์ 1234๋ฒ ํฌํธ์์ ์ ์ ๊ฐ๋ฅํ ๊ฒ ํ์ธ
# docker-compose๋ ํญ์ version tag๋ก ์์
# ์ต๊ทผ ๋ฒ์ ์ธ "3"์ ์ฌ์ฉ
version: "3"
# 1 service = 1 container
# 'services'๋ก ์๋น์ค ์์ฑ
services:
# server์ client ๋ ๊ฐ์ง service๋ฅผ ์์ฑ
# ์ฒซ๋ฒ์งธ service(container): server ์ ์
server:
# build : ์ด๋ฏธ์ง๋ฅผ ๋ง๋๋ ๋ฐ ์ฌ์ฉํ dockerfile์ ๊ฒฝ๋ก ์ง์ .
# service๋ฅผ ์คํํ๋ ์ญํ .
build: ./server/
#server์ image ์ด๋ฆ ์ ์
image: leeeunda/server:1.0
# ์ด๋ฏธ์ง๊ฐ ์์ฑ๋๋ฉด ํ ๋ฒ ์คํ๋๋ ๋ช
๋ น์ด
# python ./server.py๋ฅผ ์คํ
command: python ./server.py
# server.py์์ port๋ฅผ 1234๋ก ์ง์
# ์ปจํ
์ด๋ ์ธ๋ถ์์ ์๋ฒ์ ์ก์ธ์ค ํ๊ธฐ ์ํจ
#[์ปดํจํฐ์์ ์ํ๋ ํฌํธ]:[์ปจํ
์ด๋์์ ๊ฒ์ํ๋ ค๋ ํฌํธ]
ports:
- 1234:1234
# ๋๋ฒ์งธ service(container): client ์ ์
client:
build: ./client/
#client์ image ์ ์
image: leeeunda/client:1.0
# build : ์ด๋ฏธ์ง๋ฅผ ๋ง๋๋ ๋ฐ ์ฌ์ฉํ dockerfile์ ๊ฒฝ๋ก ์ง์ .
# service๋ฅผ ์คํํ๋ ์ญํ .
command: python ./client.py
#๋คํธ์ํฌ ์ ํ์ ์ ์
#์ปจํ
์ด๋๊ฐ ์ปดํจํฐ์ 'localhost'์ ์ก์ธ์คํ ์ ์์์ ์ ์
network_mode: host
# depends_on : ์๋น์ค ์ฌ๋ถ๋ฅผ ์ ์
# client service๊ฐ server service๊ฐ ready๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋๋ก ํจ.
depends_on:
- server
docker-compose.yml ํ์ผ์ ๋ฐํ์ผ๋ก ์ปจํ
์ด๋ ์์ฑ.
docker-compose up์ผ๋ก ๋ฐ๋ก ๋ช
๋ น์ด ์คํํด๋ ์ปจํ
์ด๋๋ฅผ ์๋์ผ๋ก ์์ฑํจ.
docker-compose.yml์ ๋ฐํ์ผ๋ก ์ปจํ
์ด๋๋ฅผ ์์ฑํ์ฌ ์์.
docker-compose up [์ต์ ][์๋น์ค๋ช ]
์ดํ docker-compose down์ผ๋ก ์คํ ์ค์ง์ํด.
docker-compose up ๋ช ๋ น์ด๋ก ์์ฑ๋ ๋์ปค ์ด๋ฏธ์ง๋ฅผ ๊ฐ๊ฐ push ๋ช ๋ น์ด๋ฅผ ํตํด ๋์ปค ํ๋ธ์ ์ ๋ก๋
services.client.networks must be a list
ymlํ์ผ์ networks ์์ ๋ฃ์ด๋๊ณ ๋ฐ์๋ค๊ฐ ๋ ๋คํธ์ํฌ ์ง์ ์ํด์ ์๊ธด ์ค๋ฅ
ํ์ ๋ฐ๋ผ์ yml ํ์ผ์ ๋คํธ์ํฌ ์ง์ ํด์ผํจ.
networks must be a mapping
์ด ์ฝ๋๋ ๊ธฐ์กด์ ๋คํธ์ํฌ๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋์์.
๋คํธ์ํฌ ์ง์ ํ์ง ์์ผ๋ฉด ๋ณ๋์ ๋คํธ์ํฌ๊ฐ ์์ฑ๋จ. ๊ทธ๋ฅ ๋คํธ์ํฌ ๋นผ๋ฒ๋ฆผ.
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to create LLB definition: the Dockerfile cannot be empty
โdockerfile ๊ฒฝ๋ก๋ฅผ ์ฐพ์ง ๋ชปํด์ ๋ฐ์
(yml ํ์ผ ๋ฌธ์ ์์. ์๋ ๊ฒฝ๋ก ์ง์ ํ ๋ ์ ์ ์ฐ์ด์ ๋ฌธ์ ์๊น)