Computer Science/DevOps

what is Docker Compose? | ๐Ÿณ ๋„์ปค ์ปดํฌ์ฆˆ๋ฅผ ์•Œ์•„๋ณด์ž | ์‹ค์ŠตํŽธ

ํ† ๋งˆํ† . 2021. 8. 29. 22:37

๐Ÿณ ๐Ÿณ ๐Ÿณ ๐Ÿณ ๐Ÿณ

 

์ฐธ๊ณ  - ์–„ํŒํ•œ ์ฝ”๋”ฉ์‚ฌ์ „ : ๊ฐ€์žฅ ์‰ฝ๊ฒŒ ๋ฐฐ์šฐ๋Š” ๋„์ปค - YouTube

 

 

๐Ÿณ ๋ชฉํ‘œ ๐Ÿณ

๋„์ปค, ๋„์ปค ์ปดํฌ์ฆˆ๋ฅผ ์•ˆ๋‹ค.

๊ฐœ๋… + ์‹ค์Šต์„ ๋‚จ์—๊ฒŒ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ•™์Šต ๋ชฉํ‘œ ๋Š๋‚Œ~

๋‚ด์ผ ๋™์•„๋ฆฌ์—์„œ ๋„์ปค ์ปดํฌ์ฆˆ๋ฅผ ๋ฐœํ‘œํ•ด์•ผ ํ•œ๋‹ค.

ใ…‹ใ…‹ใ…‹ใ…‹. ^^.

 

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

๋‚˜์˜ ์ดํ•ด๋„๋ฅผ ์ ๊ฒ€ํ•ด๋ณด์ž!

 

๊ทธ๋™์•ˆ ๋™์•„๋ฆฌ์—์„œ ๋ญ˜ ํ•ด๋ดค๋‚˜..

๋ญ”๊ฐ€ .. ์ด๊ฒƒ์ €๊ฒƒ ๊น”์•˜๋‹ค!

 

๋„์ปค ๊น”๊ธฐ

๋„์ปค ์ด๋ฏธ์ง€๋กœ mysql ๋‹ค์šด

+ ๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ•

ํ•˜๋‘ก + ์ŠคํŒŒํฌ

spark ML

์นดํ”„์นด

 

-> ๊ทธ๋ฆฌ๊ณ  ์ปดํฌ์ฆˆ..

 

๊ทธ๋ฆฌ๊ณ  ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ•œ ๊ฒƒ ๊ฐ™์•„์„œ

๋ฐ์ดํ„ฐ ์—”์ง€๋‹ˆ์–ด๋ง ์ธํ”„๋ผ๋„ ๊ณต๋ถ€๋ฅผ ํ•ด ๋ณด์•˜๋‹ค. 

 


๐Ÿณ ๊ทธ๋ฆฌ๊ณ  ์–„์ฝ”! ๐Ÿณ

์–„์ฝ”์˜ ๋„์ปค ์‹ค์Šต ๊ตฌ์กฐ๋„

 

๋กœ์ปฌ ํ™˜๊ฒฝ์˜ ์›น์„ docker๋กœ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ํ•œ๋‹ค!

๐Ÿณ docker image - node.js

ใ„ด๐Ÿณ๐Ÿณ frontend - dockerfile1

ใ„ด๐Ÿณ๐Ÿณ backend - dockerfile2

ใ„ด๐Ÿณ๐Ÿณ database - dockerfile3

๐Ÿณ๐Ÿณ๐Ÿณ docker-compose - .yml

 

๐Ÿณ ๋ชฉ์ฐจ ๐Ÿณ

๐Ÿณ step 1 ๐Ÿณ

docker image

๐Ÿณ step 2 ๐Ÿณ

dockerfile

- i) frontend

- ii) backend

- iii) database

๐Ÿณ step 3 ๐Ÿณ

docker compose

 

 

๐Ÿณ step 1. docker image ๐Ÿณ

 

(1)

์‹ค์Šต์„ ์œ„ํ•œ ํด๋” ์ƒ์„ฑ

 

์‹ค์Šต ํ™˜๊ฒฝ : IDE์—์„œ terminal ์‹คํ–‰ํ•œ ํ›„ + WSL์— ๋“ค์–ด๊ฐ,,

 

* ์ฃผ์˜ *

์œˆ๋„์šฐ๋Š” Users > [์‚ฌ์šฉ์ž ์ด๋ฆ„] ๋‚ด๋ถ€์— ํด๋” ์ƒ์„ฑํ•˜์—ฌ์•ผ ํ•จ

 

(2)

์‹ค์Šต์„ ์œ„ํ•œ git ๋‹ค์šด

 

git clone https://github.com/tomatozoo/docker-compose-without-dockerfile.git

 

* ์–„์ฝ”์˜ ์‹ค์Šต ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—์„œ dockerfile๊ณผ docker-compose.yml ํŒŒ์ผ์„ ์‚ญ์ œํ•œ ๊ฒƒ์ž„ *

 

(์›๋ž˜์˜) ์‹ค์Šต ํด๋”๋Š” ๋ฐฉ๋ช…๋ก์„ ๋“ฑ๋กํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์›นํŽ˜์ด์ง€์ธ๋ฐ 

 

๐Ÿณ docker-practice -> application
ใ„ด๐Ÿณfrontend -> service 1
   ใ„ด .css
   ใ„ด .js
   ใ„ด .html
   ใ„ด dockerfile
ใ„ด๐Ÿณbackend -> service 2
   ใ„ด .py
   ใ„ด dockerfile
ใ„ด๐Ÿณdatabase -> service 3
   ใ„ด .sql
   ใ„ด dockerfile
๐Ÿณ docker-compose.yml

 

๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

(์—ฌ๊ธฐ ์˜ฌ๋ฆฐ git ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—๋Š” ์—†์Œ :))

 

๋„์ปค์—์„œ ์ „์ฒด ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋Š” 'application'์ด๋ผ๊ณ  ํ•˜๊ณ 

๋‚ด๋ถ€๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” frontend, backend, database, search engine ๋“ฑ์„ 'service'๋ผ๊ณ  ํ•œ๋‹ค.

 

 

(3)

์ด ์‹ค์Šต์—์„œ๋Š” ๋กœ์ปฌ ํ™˜๊ฒฝ์— node.js๊ฐ€ ์—†๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ 

๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด ๋ฐ›๋Š”๋‹ค.

 

$ docker run -it node

...

node image๊ฐ€ ์ด๋ฏธ ์žˆ์œผ๋ฉด, ๋กœ์ปฌ ํ™˜๊ฒฝ์—์„œ ์„ค์น˜ํ•˜๊ณ 

์—†์œผ๋ฉด docker hub์—์„œ ์ฐพ์•„์„œ ๋‹ค์šด ๋ฐ›๋Š” ๋ช…๋ น์–ด๋ผ๊ณ  ํ•œ๋‹ค.

 

Welcome to Node.js v16.8.0.
Type ".help" for more information.
>

์ง ~

์„ค์น˜ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ, ์ด๋ ‡๊ฒŒ node.js์ด ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค.

 

 

Docker Hub

 

์œ„ ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€๋ฉด,

๊ฐœ๋ฐœ์— ํ•„์š”ํ•œ ํ™˜๊ฒฝ์„ ์ด๋ฏธ์ง€ ํ˜•ํƒœ๋กœ ๋‹ค์šด ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋•Œ

๋„์ปค์˜ ์ด๋ฏธ์ง€๋ž€, ๋ฆฌ๋ˆ…์Šค์˜ ์ปดํ“จํ„ฐ์˜ ํŠน์ • ์ƒํƒœ๋ฅผ ๋ฐ•์ œํ•ด๋†“์€ ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. 

 

node ์ด๋ฏธ์ง€๋Š” ๋ฆฌ๋ˆ…์Šค์— node.js๊ฐ€ ์„ค์น˜๋œ ๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ๊ณ ์ •ํ•œ ๊ฒƒ์ด๊ณ 

์ผ๋‹จ ๋กœ์ปฌ์—์„œ ์ฐพ์•„๋ณด๊ณ ,

์—†์œผ๋ฉด docker hub์—์„œ ์ฐพ์•„์„œ ๋‹ค์šด์„ ๋ฐ›๋Š”๋‹ค. 

 

๋„์ปค ์ด๋ฏธ์ง€๊ฐ€ ์„ค๊ณ„๋„๋ผ๋ฉด

์ปจํ…Œ์ด๋„ˆ๋Š” ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“  ๊ฑด๋ฌผ์ด๋‹ค. 

 

 

$ docker images
REPOSITORY                 TAG       IMAGE ID       CREATED        SIZE
node                       latest    1016313cda78   2 days ago     907MB

docker images๋ฅผ ํ†ตํ•ด node image๊ฐ€ ์ƒ๊ฒผ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

tomato@DESKTOP-IP2MBOJ:/mnt/c/Users/82109/Source/Repos/practice-docker$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
e1b735810b4c   node      "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes             relaxed_yonath

docker ps

๋กœ ์ƒํƒœ ํ™•์ธ

์ปจํ…Œ์ด๋Ÿฌ ์ด๋ฆ„์ด relaxed_yonath๋ผ์„œ

docker exec -it [์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„] bash๋กœ

๋ฆฌ๋ˆ…์Šค CLI์— ์ ‘์†ํ•ด์ค€๋‹ค.

$ docker exec -it relaxed_yonath bash
root@e1b735810b4c:/#

์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ๊ฐ€์ƒ์˜ ํ™˜๊ฒฝ์— ์ ‘์†ํ•œ ๊ฒƒ์ด๋‹ค. 

 

์•„๊นŒ ๋งํ–ˆ๋“ฏ์ด,

๋„์ปค๋Š” ๋ฆฌ๋ˆ…์Šค ๊ฐ€์ƒํ™˜๊ฒฝ์˜ ํ˜•ํƒœ๋กœ ๋Œ์•„๊ฐ„๋‹ค. 

ls๋ฅผ ํ†ตํ•ด ๊ธฐ๋ณธ ๋””๋ ‰ํ† ๋ฆฌ ํ™•์ธ ๊ฐ€๋Šฅํ•จ

 

 

๐Ÿณ step2. docker file ๐Ÿณ

์ •ํ•ด์ง„ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ hub์—์„œ ๋‹ค์šด ๋ฐ›๊ณ ,

์ปจํ…Œ์ด๋„ˆ๋กœ ๋งŒ๋“ค์–ด ํ„ฐ๋ฏธ๋„๋กœ ๋ช…๋ นํ•˜๋Š” ๊ฒƒ๋งŒ์œผ๋กœ๋Š” ์ถฉ๋ถ„ํ•˜์ง€ ์•Š๋‹ค.

 

-> ๊ทธ๋ž˜์„œ Dockerfile์„ ์ด์šฉํ•œ๋‹ค.

 

ํ•„์š”์— ๋งž๊ฒŒ 

๊ณต์‹ ์ด๋ฏธ์ง€๋ฅผ ํŠœ๋‹ํ•ด์„œ

๋”ฑ ๋งž๋Š” ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค์–ด๋‚ด๊ฒŒ ๋œ๋‹ค. 

 

์˜ˆ๋ฅผ ๋“ค๋ฉด, node.js์™€ http-server๋ฅผ ํ•ฉ์น˜๋Š” ์‹(?)

 

์ด ์‹ค์Šต ํด๋”์—๋Š” frontend, backend, database ํ•˜์œ„ ํด๋”์— ๋ชจ๋‘

dockerfile์ด ์žˆ์—ˆ๋Š”๋ฐ.. ์‚ญ์ œํ–ˆ๋‹ค ^^

 

dockerfile์˜ ๊ธฐ๋ณธ์ ์ธ ๋ช…๋ น์–ด๋ฅผ ๊ณต๋ถ€ํ•˜๊ณ 

์ง์ ‘ frontend, backend, database์˜ dockerfile์„ ์ƒ์„ฑํ•ด๋ณด์ž!

 

 

(0)

๐Ÿณ dockerfile ๋ช…๋ น์–ด ์•Œ์•„๋ณด๊ธฐ ๐Ÿณ

FROM - ๋ฒ ์ด์Šค ์ด๋ฏธ์ง€ ์ €์žฅ

ex) FROM node:12.18.4

FROM ์ด๋ฏธ์ง€๋ช…:๋ฒ„์ „์ •๋ณด

RUN - ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๊ณผ์ •์—์„œ ๋ช…๋ น์–ด ์‹คํ–‰

ex) RUN npm install -g http-server
* npm : python์˜ pip๊ฒฉ. Node Package Manager

WORKDIR - ๊ธฐ๋ณธ ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ • (๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ๊ณ„์† ์ž‘์—…ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ)

ex) WORKDIR /home/node/app

๋ช…๋ น์–ด ์‹คํ–‰ํ•  ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ • (ํ˜„์œ„์น˜)

CMD - ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ๋ช…๋ น์–ด ์ •์˜

ex) CMD ["http-server", "-p", "8080", "./public"]

$ docker run --name frontend_container -v $(pwd):/home/node/app -p 8080:8080 frontend_image

??

ENV - ์ด๋ฏธ์ง€ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ง€์ •. -e ์˜ต์…˜์„ ํ†ตํ•ด ๊ธฐ์กด ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ. 

ex)

ENV MYSQL_USER mysql_user
ENV MYSQL_PASSWORD mysql_password

Environment variables in Compose | Docker Documentation ์ฐธ๊ณ 

COPY - ํŒŒ์ผ, ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ด๋ฏธ์ง€๋กœ ๋ณต์‚ฌ


๋„์ปคํ™˜๊ฒฝ์—์„œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์‹œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ํด๋”๋กœ
๋ฏธ๋ฆฌ ์ž‘์„ฑ๋œ ์Šคํฌ๋ฆฝํŠธ๋“ค์„ ๋ณต์‚ฌ์‹œํ‚จ๋‹ค.
ex) COPY ./scripts/ /docker-entrypoint-initdb.d/

MAINTAINER - ๊ด€๋ฆฌ์ž ์ •๋ณด(์ด๋ฆ„, ์ด๋ฉ”์ผ ๋“ฑ)

ADD - ... COPY์™€ ์œ ์‚ฌํ•จ

EXPOSE - ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์‹คํ–‰๋˜์—ˆ์„ ๋•Œ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํฌํŠธ(Listen) ์ง€์ •

 

์ฐธ๊ณ  - [Docker] ๊ฐœ๋… ์ •๋ฆฌ ๋ฐ ์‚ฌ์šฉ๋ฐฉ๋ฒ•๊นŒ์ง€. (cultivo-hy.github.io)

: dockerfile reference

์ฐธ๊ณ  - Get started with Docker Compose | Docker Documentation

 

# syntax=docker/dockerfile:1
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"]
  • Build an image starting with the Python 3.7 image.
  • Set the working directory to /code.
  • Set environment variables used by the flask command.
  • Install gcc and other dependencies
  • Copy requirements.txt and install the Python dependencies.
  • Add metadata to the image to describe that the container is listening on port 5000
  • Copy the current directory . in the project to the workdir . in the image.
  • Set the default command for the container to flask run.

 

 

(1)

๐Ÿณ frontend ๐Ÿณ

 

dockerfile ์‚ดํŽด๋ณด๊ธฐ

FROM node:12.18.4

# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๊ณผ์ •์—์„œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด
RUN npm install -g http-server 

# ์ด๋ฏธ์ง€ ๋‚ด์—์„œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ• (ํ˜„ ์œ„์น˜๋กœ ์žก์„) ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •
WORKDIR /home/node/app

# ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰์‹œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด
CMD ["http-server", "-p", "8080", "./public"]

# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ช…๋ น์–ด (ํ˜„ ํŒŒ์ผ๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ)
# docker build -t {์ด๋ฏธ์ง€๋ช…} .

# ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ & ์‹คํ–‰ ๋ช…๋ น์–ด
# docker run --name {์ปจํ…Œ์ด๋„ˆ๋ช…} -v $(pwd):/home/node/app -p 8080:8080 {์ด๋ฏธ์ง€๋ช…}

 

์ด ๋„์ปคํŒŒ์ผ์„ ๋„์ปค ์ด๋ฏธ์ง€๋กœ ์ƒ์„ฑํ•œ๋‹ค.

 

$ docker build -t frontend_image .

..

tomato@DESKTOP-IP2MBOJ:/mnt/c/Users/82109/Source/Repos/practice-docker/frontend$ docker images
REPOSITORY       TAG       IMAGE ID       CREATED          SIZE
frontend_image   latest    91c8ec3097f3   19 seconds ago   922MB

 

docker images

๋ช…๋ น์–ด๋กœ frontend_image ์ด๋ฏธ์ง€๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

$ docker run --name frontend_container -v $(pwd):/home/node/app -p 8080:8080 frontend_image

๊ทธ ๋‹ค์Œ, ์ด๋ฏธ์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹คํ–‰์‹œํ‚จ๋‹ค. 

 

 

8080๋ฒˆ์œผ๋กœ ์†ก์ถœํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์—

localhost:8080์— ์ ‘์†ํ•˜๋ฉด, ์›นํŽ˜์ด์ง€๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๊ฒฐ๊ณผ

 

(2)

๐Ÿณ database ๐Ÿณ

 

๋„์ปค ํŒŒ์ผ๋กœ (1) ๊ณผ์ • ๋ฐ˜๋ณต

 

database์˜ ๋„์ปค ํŒŒ์ผ์„ ์‚ดํŽด๋ณด์ž. 

 

FROM mysql:5.7

# ์ด๋ฏธ์ง€ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋“ค ์„ธํŒ…
# ์‹ค์ „์—์„œ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ์„ ์ด๊ณณ์— ์ž…๋ ฅํ•˜์ง€ ๋ง ๊ฒƒ!
# ์„œ๋ฒ„์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋“ฑ์„ ํ™œ์šฉํ•˜์„ธ์š”.
ENV MYSQL_USER mysql_user
ENV MYSQL_PASSWORD mysql_password
ENV MYSQL_ROOT_PASSWORD mysql_root_password
ENV MYSQL_DATABASE visitlog

# ๋„์ปคํ™˜๊ฒฝ์—์„œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์‹œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ํด๋”๋กœ
# ๋ฏธ๋ฆฌ ์ž‘์„ฑ๋œ ์Šคํฌ๋ฆฝํŠธ๋“ค์„ ์ด๋™
COPY ./scripts/ /docker-entrypoint-initdb.d/

# ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ช…๋ น์–ด (ํ˜„ ํŒŒ์ผ๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ)
# docker build -t {์ด๋ฏธ์ง€๋ช…} .

# ์‹คํ–‰ ๋ช…๋ น์–ด (ํ„ฐ๋ฏธ๋„์— ๋กœ๊ทธ ์ฐํžˆ๋Š” ๊ฒƒ ๋ณด๊ธฐ)
# docker run --name {์ปจํ…Œ์ด๋„ˆ๋ช…} -it -p 3306:3306 {์ด๋ฏธ์ง€๋ช…}

# ์‹คํ–‰ ๋ช…๋ น์–ด (๋ฐ๋ชฌ์œผ๋กœ ์‹คํ–‰)
# docker run --name {์ปจํ…Œ์ด๋„ˆ๋ช…} -p 3306:3306 -d {์ด๋ฏธ์ง€๋ช…}

 

์šฐ์„ , database ํด๋”๋กœ ์ด๋™ํ•œ๋‹ค. 

(cd ..)

(cd database)

$ docker build -t database_image .

 

๋˜‘๊ฐ™์ด ๋„์ปคํŒŒ์ผ์„ ์ด๋ฏธ์ง€๋กœ ์ƒ์„ฑํ•ด์ฃผ๊ณ ,

 

 

tomato@DESKTOP-IP2MBOJ:/mnt/c/Users/82109/Source/Repos/practice-docker/database$ docker run --nam
e database_container -it -p 3306:3306 database_image

 

์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ์‹œํ‚จ๋‹ค!

 

mysql์€ 3306 ํฌํŠธ๊ฐ€ ๊ธฐ๋ณธ์ด๋ผ๊ณ  ํ•œ๋‹ค..

 

์œ„ ๋ช…๋ น์–ด ๋Œ€์‹ , - d : daemon ์„ ์ถ”๊ฐ€ํ•˜๋ฉด,

(๋’ค์—์„œ ์—ฐ๊ฒฐํ•˜๋ผ๋Š” ๋œป)

ID ๋งŒ ํ‘œ์‹œ๋˜๊ณ ,

terminal์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

tomato@DESKTOP-IP2MBOJ:/mnt/c/Users/82109/Source/Repos/practice-docker$ docker run --name databas
e_container -p 3306:3306 -d database_image
6e89286cdd9307656b725e16ff8990ac6281b54771bf4caaf51720d03d4f6cc6

...

 

(3)

๐Ÿณ backend ๐Ÿณ

 

๋ฐฑ์—”๋“œ๋„ ๋˜‘~๊ฐ™์ด ํ•ด์ค€๋‹ค.

 

FROM python:3.8.5

# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๊ณผ์ •์—์„œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด
RUN pip3 install flask flask-cors flask-mysql

# ์ด๋ฏธ์ง€ ๋‚ด์—์„œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ• (ํ˜„ ์œ„์น˜๋กœ ์žก์„) ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •
WORKDIR /usr/src/app

# ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰์‹œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด
CMD ["python3", "backend.py"]

# ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ช…๋ น์–ด (ํ˜„ ํŒŒ์ผ๊ณผ ๊ฐ™์€ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ)
# docker build -t {์ด๋ฏธ์ง€๋ช…} .

# ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ & ์‹คํ–‰ ๋ช…๋ น์–ด
# docker run --name {์ปจํ…Œ์ด๋„ˆ๋ช…} -v $(pwd):/usr/src/app -p 5000:5000 {์ด๋ฏธ์ง€๋ช…}

๋„์ปค ํŒŒ์ผ

tomato@DESKTOP-IP2MBOJ:/mnt/c/Users/82109/Source/Repos/practice-docker$ docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED              STATUS
 PORTS                                                  NAMES
6e89286cdd93   database_image   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   database_container

 

๋ฐฑ์—”๋“œ๋Š” ํŒŒ์ด์ฌ์˜ flask๋กœ api ์„œ๋ฒ„๋ฅผ ๋Œ๋ฆฐ๋‹ค. 

์„œ๋น„์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์„ธ ์š”์†Œ๊ฐ€ dockerfile์ด๋ผ๋Š” ๋ฏธ์‹œ์  ์„ค๊ณ„๋„๋กœ ์„ค์ •๋œ๋‹ค. 

 


 

๐Ÿณ step3. docker-compose ๐Ÿณ

ํ•˜๋‚˜ํ•˜๋‚˜ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค๊ณ 

์‹คํ–‰์‹œํ‚ค๊ธฐ ๋ฒˆ๊ฑฐ๋กญ๊ธฐ ๋•Œ๋ฌธ์—

 

๊ฑฐ์‹œ์  ์„ค๊ณ„๋„์ธ docker-compose๋ฅผ ์ด์šฉํ•œ๋‹ค. 

 

docker-compose.yml ์˜ˆ์‹œ ํŒŒ์ผ์„ ์‚ดํŽด๋ณด์ž.

 

์ด๋ฏธ์ง€์˜ version์„ ์ •ํ•˜๊ณ ,

๊ฐ service(database, backend, frontend)์˜ ์˜ต์…˜๋“ค์„ ์ •ํ•ด์ค€๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. 

version: '3'
services:
  database:
    # Dockerfile์ด ์žˆ๋Š” ์œ„์น˜
    build: ./database
    # ๋‚ด๋ถ€์—์„œ ๊ฐœ๋ฐฉํ•  ํฌํŠธ : ์™ธ๋ถ€์—์„œ ์ ‘๊ทผํ•  ํฌํŠธ
    ports:
      - "3306:3306"
  backend:
    build: ./backend
    # ์—ฐ๊ฒฐํ•  ์™ธ๋ถ€ ๋””๋ ‰ํ† ๋ฆฌ : ์ปจํ…Œ์ด๋„ˆ ๋‚ด ๋””๋ ‰ํ† ๋ฆฌ
    volumes:
      - ./backend:/usr/src/app
    ports:
      - "5000:5000"
    # ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •
    environment: 
      - DBHOST=database
  frontend:
    build: ./frontend
    # ์—ฐ๊ฒฐํ•  ์™ธ๋ถ€ ๋””๋ ‰ํ† ๋ฆฌ : ์ปจํ…Œ์ด๋„ˆ ๋‚ด ๋””๋ ‰ํ† ๋ฆฌ
    volumes:
      - ./frontend:/home/node/app
    ports:
      - "8080:8080"

 

๊ฒฐ๊ตญ, ์•ž์„œ ์ผ์ผ์ด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰์‹œ์ผœ์ค€ ๊ฒƒ์„

๋Œ€์‹ ํ•˜๊ธฐ ์œ„ํ•ด ๋„์ปค ์ปดํฌ์ฆˆ๊ฐ€ ์žˆ์œผ๋‹ˆ๊นŒ ๋ง์ด๋‹ค.

 

ํ”„๋ก ํŠธ์—”๋“œ์˜ ๊ฒฝ์šฐ

$ docker build -t frontend_image .

$ docker run --name frontend_container -v $(pwd):/home/node/app -p 8080:8080 frontend_image

ํ”„๋ก ํŠธ์—”๋“œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ช…๋ น์–ด ์กฐ๊ฑด์ด

(ํฌํŠธ๋„˜๋ฒ„ 8080:8080 , ๊ฒฝ๋กœ .. /app ๋“ฑ)

services:
  frontend:
    build: ./frontend
    # ์—ฐ๊ฒฐํ•  ์™ธ๋ถ€ ๋””๋ ‰ํ† ๋ฆฌ : ์ปจํ…Œ์ด๋„ˆ ๋‚ด ๋””๋ ‰ํ† ๋ฆฌ
    volumes:
      - ./frontend:/home/node/app
    ports:
      - "8080:8080"

compose ํŒŒ์ผ๋กœ ์˜ฎ๊ฒจ์™”๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

๋ฒก์—”๋“œ๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋‹ค. 

 


dockerfile, docker-compose.yml

ํŒŒ์ผ์„ ๋‹ค ๊ตฌ์„ฑํ–ˆ๋‹ค๋ฉด,

 

์ด์ œ ํ„ฐ๋ฏธ๋„์—์„œ ์‹คํ–‰์‹œ์ผœ๋ณด์ž!

 

tomato@DESKTOP-IP2MBOJ:/mnt/c/Users/82109/Source/Repos/practice-docker$ docker-compose up

์‹ค์Šต ํด๋”์—์„œ docker-compose up ๋ช…๋ น์„ ํ•ด์ฃผ๋ฉด..

 

๋กœ์ปฌ ํ˜ธ์ŠคํŠธ์—์„œ ์›น์‚ฌ์ดํŠธ๊ฐ€ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ถ”๊ฐ€์ ์ธ ๊ณผ์ • :)

๋„์ปค ์ปดํฌ์ฆˆ ๊ธฐ๋ณธ ๋ช…๋ น์–ด

[Docker CE] docker-compose ๊ธฐ๋ณธ ๋ช…๋ น์–ด ์ •๋ฆฌ :: Nirsa (tistory.com)

docker-compose --version

๋ฒ„์ „ ํ™•์ธ

 

docker-compose up [์˜ต์…˜] [์„œ๋น„์Šค๋ช…]

์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ + ์‹คํ–‰

์ด๋•Œ ์˜ต์…˜์˜ ์ข…๋ฅ˜์—๋Š”

1) -d : ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์‹คํ–‰ daemon

2) --no-deps : ๋งํฌ ์„œ๋น„์Šค ์‹คํ–‰ํ•˜์ง€ ์•Š์Œ

3) --build : ์ด๋ฏธ์ง€ ๋นŒ๋“œ

4) -t : ํƒ€์ž„์•„์›ƒ ์ง€์ •

 

docker-compose ps

์ปจํ…Œ์ด๋„ˆ ์ƒํƒœ ํ™•์ธ

 

docker-compose logs

์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ ์ถœ๋ ฅ

 

docker-compose run

์ปจํ…Œ์ด๋„ˆ ๋กœ๊ทธ ์ถœ๋ ฅ

docker-compose up ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑ ๋ฐ ์‹คํ–‰๋œ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ž„์˜์˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ ํ•ฉ๋‹ˆ๋‹ค.

์ปจํ…Œ์ด๋„ˆ๋“ค์„ ๋ชจ๋‘ ์‚ญ์ œํ•  ๊ฒฝ์šฐ docker-compose start๊ฐ€ ์•„๋‹Œ, docker-compose up์œผ๋กœ ๋‹ค์‹œ ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ์ƒ์„ฑ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํŠน์ • ์„œ๋น„์Šค์—์„œ /bin/bash๋ฅผ ์‹คํ–‰์‹œ์ผœ ์‰˜ ํ™˜๊ฒฝ์œผ๋กœ ์ง„์ž…ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ์„œ๋น„์Šค๋ช…๊ณผ ์ปจํ…Œ์ด๋„ˆ๋ช…์€ ๋‹ค๋ฅธ๊ฒ๋‹ˆ๋‹ค.

์„œ๋น„์Šค๋ช…์€ docker-compose.yml์˜ services: ๋ฐ‘์— ์ž‘์„ฑํ•œ ์„œ๋น„์Šค ์ด๋ฆ„ ์ž…๋‹ˆ๋‹ค.

 

# ์„œ๋น„์Šค ์‹œ์ž‘
docker-compose start

# ์„œ๋น„์Šค ์ •์ง€
docker-compose stop

# ์„œ๋น„์Šค ์ผ์‹œ ์ •์ง€
docker-compose pause

# ์„œ๋น„์Šค ์ผ์‹œ ์ •์ง€ ํ•ด์ œ
docker-compose unpause

# ์„œ๋น„์Šค ์žฌ์‹œ์ž‘
docker-compose restart

์„œ๋น„์Šค ์‹œ์ž‘ / ์ •์ง€ / ์ผ์‹œ์ •์ง€ / ์žฌ์‹œ์ž‘

 

docker-compose rm

์ปจํ…Œ์ด๋„ˆ ์ผ๊ด„ ์‚ญ์ œ

 

 

docker-compose kill [์˜ต์…˜]

docker-compose kill -s SIGINT

docker-compose kill

์ปจํ…Œ์ด๋„ˆ ๊ฐ•์ œ ์ •์ง€

-s SIGINT ์‚ฌ์šฉ์‹œ SIGINT ์ „๋‹ฌ

์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด SIGKILL ์ „์†ก

์„œ๋น„์Šค ์ง€์ •ํ•˜๋ฉด, ํŠน์ • ์„œ๋น„์Šค๋งŒ killํ•  ์ˆ˜ ์žˆ์Œ

 

 

 

docker-compose down

์ปจํ…Œ์ด๋„ˆ ์ผ๊ด„ ์ •์ง€ ๋ฐ ์‚ญ์ œ

 

 

 

docker-compose down --rmi all

์ปจํ…Œ์ด๋„ˆ ์ผ๊ด„ ์‚ญ์ œ (์ด๋ฏธ์ง€๊นŒ์ง€ ๋ชจ๋‘ ์‚ญ์ œ)

 

 

 

docker-compose port [์„œ๋น„์Šค๋ช…] [ํฌํŠธ๋ฒˆํ˜ธ]

ํ”„๋ผ์ด๋น— ํฌํŠธ๋ฒˆํ˜ธ ์„ค์ •

 

 

docker-compose config

docker-compose ํŒŒ์ผ ๋‚ด์šฉ ํ™•์ธ

 

 

์ถ”์ฒœ - Quickstart: Compose and Django | Docker Documentation

 

Quickstart: Compose and Django

 

docs.docker.com