๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Server/Docker

[Docker] SpringBoot/MyBatis ํ”„๋กœ์ ํŠธ Docker์—์„œ ์‹คํ–‰ํ•˜๊ธฐ

by soy๋ฏธ๋‹ˆ 2021. 10. 6.

 

 

 

 

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” MyBatis ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” SpringBoot ํ”„๋กœ์ ํŠธ๋ฅผ Docker ์—์„œ ์‹คํ–‰ํ•ด๋ณด๋Š” ๊ณผ์ •์„ ์ ์–ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

 

โœ… ์šฐ์„  Eclipse ์— ์ ‘์†ํ•˜์—ฌ SpringBoot ํ”„๋กœ์ ํŠธ์˜ ์„ค์ •๋ถ€ํ„ฐ ํ™•์ธํ•œ๋‹ค. MyBatisProject/pom.xml ์— dependency ๋กœ mysql ๊ณผ mybatis ๋ฅผ ์ฃผ์ž…ํ•ด์ค€๋‹ค.

 

 

 

 

โœ… ๊ทธ๋ฆฌ๊ณ  war ํŒŒ์ผ๋กœ export ํ•˜๊ธฐ ์œ„ํ•ด์„œ <packaging>jar</packaging> ๋ฅผ war ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

 

 

 

 

โœ… mybatis-config.xml ์— DB ์—ฐ๋™ํ•˜๋Š” ์ •๋ณด๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

 

 

 

 

โœ… mysql ๊ณผ tomcat ์ด๋ฏธ์ง€๋ฅผ ์„ค์ •ํ•œ ํ›„ docker hub ์— push ํ•˜๊ณ  pull ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•œ๋‹ค.

 

# docker ์„ค์น˜
sudo apt-get install docker
sudo apt-get install docker.io

# tomcat ์ด๋ฏธ์ง€ container ์‹คํ–‰
sudo docker pull tomcat:9
# volume
sudo docker run -d --name="tomcat-test" -p 8383:8080 \
-v /home/tiger/webapps:/usr/local/tomcat/webapps/ tomcat:9

# tomcat container ๋ฅผ docker hub ์— push
sudo docker commit tomcat-test(์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„) tomcat_test:latest(์ด๋ฏธ์ง€ ์ด๋ฆ„)
sudo docker tag tomcat_test:latest wjdalsdkcla/tomcat-test
sudo docker login
sudo docker push wjdalsdkcla/tomcat-test (docker hub ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ)

# docker hub ์— ์˜ฌ๋ฆฐ tomcat ์ด๋ฏธ์ง€ pull
sudo docker pull wjdalsdkcla/tomcat-test:latest
# volume
sudo docker run -d --name="tomcat-test" -p 8383:8080 \
-v /home/tiger/webapps:/usr/local/tomcat/webapps/ \
wjdalsdkcla/tomcat-test:latest

# mysql ์ด๋ฏธ์ง€ container ์‹คํ–‰
sudo docker pull mysql:latest
# volume
sudo docker run -it --name mysql-container -p 3380:3306 \
-v /home/tiger/db_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=mysql๋น„๋ฒˆ \
-d mysql:latest

# mysql container ๋ฅผ docker hub ์— push
sudo docker commit mysql-container(์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„) mysql-test:latest(์ด๋ฏธ์ง€ ์ด๋ฆ„)
sudo docker tag mysql-test:latest wjdalsdkcla/mysql-test
sudo docker login
sudo docker push wjdalsdkcla/mysql-test (docker hub ๋ ˆํŒŒ์ง€ํ† ๋ฆฌ)

# docker hub ์— ์˜ฌ๋ฆฐ mysql ์ด๋ฏธ์ง€ pull
sudo docker pull wjdalsdkcla/mysql-test:latest

# mysql ์ด๋ฏธ์ง€ container ์‹คํ–‰
# volume
sudo docker run -it --name mysql-container -p 3380:3306 \
-v /home/tiger/db_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=mysql๋น„๋ฒˆ \
-d wjdalsdkcla/mysql-test:latest

# mysql ์ ‘์† ํ›„ ๊ณ„์ • ์„ค์ •
sudo docker exec -it mysql-container /bin/bash
> mysql -u root -p
> use mysql;
> CREATE USER 'user1'@'%'IDENTIFIED BY '1234'; 
> GRANT ALL PRIVILEGES ON *.* TO 'user1'@'%';
> FLUSH PRIVILEGES;
> exit
> mysql -u user1 -p

 

 

tomcat ์ด๋ฏธ์ง€๋ฅผ ์‹คํ–‰ํ•  ๋•Œ volume (-v) ์„ ์‚ฌ์šฉํ•œ๋‹ค.

volume ์€ docker ์˜ ๊ฒฝ๋กœ์—์„œ ๋กœ์ปฌ์˜ ๊ฒฝ๋กœ์— ์žˆ๋Š” ํŒŒ์ผ์„ ์ฐธ์กฐํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์œผ๋กœ, container run ํ•  ๋•Œ๋งˆ๋‹ค -v ๋ช…๋ น์œผ๋กœ ๊ทธ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

sudo docker run -d --name="tomcat-test" -p 8383:8080 \
-v /home/tiger/webapps:/usr/local/tomcat/webapps/ tomcat:9

 

 

 

๐Ÿ’ก ์œ„์˜ command ์—์„œ -v ๋ช…๋ น์ด ์ˆ˜ํ–‰๋˜๋Š” ๊ณผ์ •์€ ๋Œ€๋žต ์ด๋Ÿฌํ•˜๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋ฆฌ๋ˆ…์Šค์˜ ๋กœ์ปฌ์ธ home/tiger ์•ˆ์— webapps ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์— ROOT.war ํŒŒ์ผ์„ ๋„ฃ๋Š”๋‹ค. ๊ทธ๋Ÿฌ๋ฉด command ๋กœ copy๋ฅผ ๋”ฐ๋กœ ํ•ด์ฃผ์ง€ ์•Š๋”๋ผ๋„ docker ์˜ usr/local/tomcat/webapps/ ๊ฒฝ๋กœ์— ROOT.war ํŒŒ์ผ์ด ์ฐธ์กฐ๋œ๋‹ค. ๋‹ค๋งŒ, -v ๋ช…๋ น์œผ๋กœ docker ์—์„œ ROOT.war ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค๊ณ  ํ•ด์„œ ๊ทธ war ํŒŒ์ผ์ด ์ด๋ฏธ์ง€์— ๊ณ„์† ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ์ฆ‰, dokcer ์—์„œ local ์˜ ROOT.war ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜์—ฌ ์ •์ƒ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์ด๋ฏธ์ง€๋ฅผ docker hub ์— push ํ•˜๊ณ  ๋‹ค์‹œ pull ๋ฐ›์•„์„œ -v ๋ช…๋ น ์—†์ด ์‹คํ–‰ํ•ด๋ณด๋ฉด ๊ทธ war ํŒŒ์ผ์€ docker ์— ๋‚จ์•„์žˆ์ง€ ์•Š๋‹ค. -v ๋ช…๋ น์„ ์‹คํ–‰ํ•ด์„œ local ํด๋”์— ์žˆ๋Š” ROOT.war ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด docker ์˜ tomcat/webapps/ ๊ฒฝ๋กœ๋กœ war ํŒŒ์ผ์ด ์ฐธ์กฐ๋˜์–ด ๋“ค์–ด๊ฐ€๊ณ  ์‹คํ–‰์ด ์ •์ƒ์ ์œผ๋กœ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

 

mysql ์ด๋ฏธ์ง€๋ฅผ ์‹คํ–‰ํ•  ๋•Œ์—๋„ volume (-v) ์„ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.

 

sudo docker run -it --name mysql-container -p 3380:3306 \
-v /home/tiger/db_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=mysql๋น„๋ฒˆ \
-d wjdalsdkcla/mysql-test:latest

 

 

๐Ÿ’ก ์œ„์˜ command ์—์„œ -v ๋ช…๋ น์ด ์ˆ˜ํ–‰๋˜๋Š” ๊ณผ์ •์€ ๋Œ€๋žต ์ด๋Ÿฌํ•˜๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋ฆฌ๋ˆ…์Šค์˜ ๋กœ์ปฌ์ธ home/tiger ์•ˆ์— db_data ํด๋”๋ฅผ ๋งŒ๋“ค๊ณ  command ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด docker ์˜ var/lib/mysql ๊ฒฝ๋กœ์— ์ €์žฅ๋˜๋Š” db data ์ •๋ณด๋“ค์ด ์ฐธ์กฐ๋˜์–ด ๋กœ์ปฌ์˜ db_data ํด๋”์— ๋ฐฑ์—…์ด ๋œ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ docker์˜ mysql ์— ์ ‘์†ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค db01 ๊ณผ db02 ๋ฅผ ๋งŒ๋“ค๊ณ  db01 ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— president ์™€ citizen ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค๋ฉด ๊ทธ ์ •๋ณด๊ฐ€ ๋กœ์ปฌ์˜ db_data/db01/ ๊ฒฝ๋กœ์— ์ €์žฅ์ด ๋œ๋‹ค. ์ด ํด๋”๋Š” ๋กœ์ปฌ์—์„œ ๊ณ„์† ์œ ์ง€๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ์—๋„ db_data ๋‚ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ, mysql ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธ(insert, update, delete) ํ•˜๊ณ  ๋‚˜์„œ ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ docker hub ์— push ํ•˜๊ณ ๋‚˜์„œ ๋‹ค์‹œ pull ๋ฐ›์•„๋ณด๋ฉด ์ด๋ฏธ์ง€์— ๊ทธ ๋ฐ์ดํ„ฐ๋Š” ๋”์ด์ƒ ๋‚จ์•„์žˆ์ง€ ์•Š๋‹ค. ๋‹ค๋งŒ ๋กœ์ปฌ์— db_data ์ •๋ณด๊ฐ€ ๋‚จ์•„์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ…Œ์ด๋ธ”์€ ์œ ์ง€๋œ๋‹ค.

 

 

 


๐Ÿ’ก volume ๊ณผ mount ์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ?

-v (volume) ์€ ๋ชจ๋“  ์˜ต์…˜์„ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฐ์— ๋น„ํ•ด --mount ๋Š” ์˜ต์…˜์„ ๋ถ„๋ฆฌํ•จ์œผ๋กœ์จ ๋” ๋ช…ํ™•ํ•˜๋‹ค.

docker volume ์— ๋Œ€ํ•œ ๊ณต์‹ ๋ฌธ์„œ : https://docs.docker.com/storage/volumes/

 

--mount ๋ช…๋ น์–ด  /  --v ๋ช…๋ น์–ด

 

 

 

โœ… docker hub ์— ์ด๋ฏธ์ง€๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์—…๋กœ๋“œ ๋œ ๊ฒƒ์„ ํ™•์ธํ•œ๋‹ค.

 

 

 

 

โœ… docker-compose ์—์„œ docker hub์— ์—…๋กœ๋“œ ๋œ ์ด๋ฏธ์ง€๋“ค์„ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

 

 

โœ… docker-compose.yml ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค.

 

 

 

 

โœ… docker-compose ํŒŒ์ผ ํ˜น์€ ํ”„๋กœ์ ํŠธ๋ฅผ github ์— ์—…๋กœ๋“œ ํ•˜๊ณ  clone ์„ ๋ฐ›๋Š”๋‹ค.

 

# ๊นƒ ์„ค์น˜
sudo apt-get install git -y

# ๊นƒ ํด๋ก 
git clone https://github.com/qwa310/compose-test.git

cd ๊นƒํด๋ก  ๋ฐ›์€ ํด๋” # docker-compose.yml ์ด ์žˆ๋Š” ํด๋”๊นŒ์ง€ ๋“ค์–ด๊ฐ„๋‹ค.

# docker compose ์‹คํ–‰
docker-compose up -d

 

 

 

โœ… docker-compose.yml ์ด ์žˆ๋Š” ๊ฒฝ๋กœ๊นŒ์ง€ ๋“ค์–ด๊ฐ€์„œ sudo docker-compose up -d ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€ํ•˜๊ณ  ์‚ญ์ œํ•  ๋•Œ๋Š” sudo docker-compose down ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

 

 

 

โœ… web ์‹คํ–‰ ๋ฐ DB ์—ฐ๋™ ํ™•์ธ

 

 

์›น ์‹คํ–‰

DB ์—ฐ๋™

 

 

๋Œ“๊ธ€