1. 도커 컴포즈(Doker Compose) 란?
Docker Compose는 다중 컨테이너 Docker 애플리케이션을 정의하고 실행하기 위한 도구이다. 이를 통해 개발자들은 하나의 명령어로 여러 컨테이너를 손쉽게 시작, 종료 및 관리할 수 있다.
2. DB 설정

이렇게 패키지를 설정한다.
ex08/db/Dockerfile
FROM mysql COPY init.sql /docker-entrypoint-initdb.d/init.sql ENV MYSQL_USER=ssar ENV MYSQL_PASSWORD=ssar1234 ENV MYSQL_ROOT_PASSWORD=root1234 ENV MYSQL_DATABASE=blogdb CMD ["--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"] EXPOSE 3306
ex08/db/init.sql
use blogdb; CREATE TABLE IF NOT EXISTS user_tb ( id integer auto_increment, created_at timestamp, email varchar(20) not null, password varchar(60) not null, username varchar(20) not null unique, profile varchar(100), primary key (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS board_tb ( id integer auto_increment, content varchar(10000), created_at timestamp, title varchar(100) not null, user_id integer, primary key (id), constraint fk_board_user_id foreign key (user_id) references user_tb (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS reply_tb ( id integer auto_increment, comment varchar(100) not null, created_at timestamp, board_id integer, user_id integer, primary key (id), constraint fk_reply_board_id foreign key (board_id) references board_tb (id), constraint fk_reply_user_id foreign key (user_id) references user_tb (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; FLUSH PRIVILEGES;
3. 서버 설정
ex08/server/Dockerfile
FROM openjdk:11-jdk-slim RUN apt-get update RUN apt-get install -y git WORKDIR /app COPY ./entrypoint.sh ./entrypoint.sh RUN ["chmod", "+x", "entrypoint.sh"] ENTRYPOINT ["/bin/bash", "./entrypoint.sh"]
ex08/server/entrypoint.sh
git clone https://github.com/busanuv/blog-last.git cd blog-last chmod +x gradlew ./gradlew build chmod +x build/libs/*.jar java -jar -Dspring.profiles.active=docker build/libs/*.jar
4. DB 연결

이미지를 생성한다.

컨테이너를 생성한다. 3308 포트로 요청이 들어오면 3306 으로 연결된다.

터미널 모드로 진입한다.

mysql 버전을 확인한다.

mysql -u root -p // mysql에 진입한다.
mysql -u root -p 를 통해 mysql 에 연결한다.

show databases; // 현재 존재하는 데이터베이스의 목록을 표시한다.
database 를 확인한다.

use blogdb; // blogdb 를 사용한다.

user테이블의 스키마를 확인할 수 있다.

빠져나온다.
5. 도커 컴포즈 실행
docker-compose.yml
파일은 Docker Compose를 사용하여 멀티 컨테이너 애플리케이션을 정의하는 구성 파일입니다. 이 파일을 통해 여러 Docker 컨테이너를 정의하고, 이들 간의 의존성과 설정을 지정할 수 있습니다.
docker-compose.yml
version: '3' services: db: build: context: ./db ports: - 3306:3306 restart: always server: build: context: ./server ports: - 8080:8080 restart: always depends_on: - db environment: RDS_HOSTNAME: db RDS_PORT: 3306 RDS_USERNAME: ssar RDS_PASSWORD: ssar1234 RDS_DB_NAME: blogdb
도커 컴포즈 명령어
docker-compose up :
docker-compose.yml 파일에 정의된 모든 서비스를 시작한다.
docker-compose down :
실행 중인 모든 서비스를 중지하고 컨테이너, 네트워크, 볼륨을 제거한다.
docker-compose build :
docker-compose.yml 파일에 정의된 이미지를 빌드한다.
docker-compose logs :
실행 중인 모든 서비스의 로그를 출력한다.
docker-compose exec 서미스명 커맨드(bash 같은) :
실행 중인 컨테이너에서 명령어를 실행한다.

docker-compose up
명령어를 통해 docker-compose.yml 을 실행한다.

DB 도 정상적으로 작동된다.

git clone 된 스프링 서버가 정상적으로 실행된다.
6. MySQL 워크벤치 확인

워크벤치에서 새로운 커넥트를 생성한다. 포트 번호는 컨테이너를 실행 시 적용한 3308 포트를 넣는다. 비밀번호는 ROOT 비밀번호를 넣었다.


mysql 에서도 정상적으로 테이블을 확인할 수 있다.
Share article