From ad5c01fd86e4e8888d03c194c2694ec7368e473e Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Thu, 5 Feb 2026 13:02:07 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20docker=20=EC=9D=B4=EC=9A=A9=ED=95=9C=20?= =?UTF-8?q?=EC=84=9C=EB=B2=84=20=EC=8B=A4=ED=96=89=20=EB=B0=8F=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=20=EC=84=A4=EB=AA=85=20=EC=B6=94=EA=B0=80,=20?= =?UTF-8?q?=ED=99=98=EA=B2=BD=EB=B3=80=EC=88=98=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=9C=20=EC=A3=BC=EC=86=8C=20=EC=B2=98=EB=A6=AC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 3 +++ Dockerfile | 17 +++++++++++++++++ README.md | 23 +++++++++++++++++------ client/main.cpp | 9 +++++++-- compose.yml | 28 +++++++++++++++++++++++++--- server/main.cpp | 19 ++++++++++++++++--- 6 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c1d315b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +compile_commands.json +.cache +build \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4a19a78 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM debian:trixie AS builder + +RUN apt-get update && apt-get install -y \ + cmake \ + make \ + g++ \ + libboost-all-dev \ + libssl-dev + +COPY . /app +WORKDIR /app + +RUN bash ./setup.sh + +RUN cmake --build build -j$(nproc) + +CMD ["build/Server"] \ No newline at end of file diff --git a/README.md b/README.md index a881618..e7619d9 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,23 @@ - 글로벌 세션 관리: 모든 접속자를 한곳에서 관리하고 브로드캐스팅하는 시스템. - 검 키우기 컨텐츠: MySQL 연동을 통한 유저 데이터 영속성 및 강화 로직 -## 사전 요구 사항 (테스트 환경, debian 13) -- g++ 14.2.0 이상 -- CMake 3.31.6 이상 -- Boost Libraries 1.83.0 이상 -- OpenSSL 3.5.4 이상 -- MariaDB Client Library 11.8.3 이상 +## 사전 요구 사항 (로컬 작동 확인한 환경, debian 13) +- g++ 14.2.0 +- CMake 3.31.6 +- Boost Libraries 1.83.0 +- OpenSSL 3.5.4 +- MariaDB Client Library 11.8.3 + +## Docker 실행 + +```bash +docker compose up -d + +# 서버에 연결 + docker run --rm -it --entrypoint /app/build/Client --network sword_game_network -e SERVER_HOST=game_server sword_game-server +``` + + ## 환경 설정 diff --git a/client/main.cpp b/client/main.cpp index e94f85b..170acb8 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -18,6 +18,11 @@ int main(int argc, char *argv[]) { try { boost::asio::io_context io_context; + const char *env_host = std::getenv("SERVER_HOST"); + const char *env_port = std::getenv("SERVER_PORT"); + std::string host = env_host ? env_host : "127.0.0.1"; + std::string port = env_port ? env_port : "30000"; + if (isScenario) { if (scenarioFile.empty()) { std::cerr @@ -25,10 +30,10 @@ int main(int argc, char *argv[]) { << std::endl; return 1; } - ScenarioClient client(io_context, "127.0.0.1", "30000", scenarioFile); + ScenarioClient client(io_context, host, port, scenarioFile); client.Run(); } else { - InteractiveClient client(io_context, "127.0.0.1", "30000"); + InteractiveClient client(io_context, host, port); client.Run(); } } catch (std::exception &e) { diff --git a/compose.yml b/compose.yml index ce6f2a9..dc930e0 100644 --- a/compose.yml +++ b/compose.yml @@ -1,12 +1,34 @@ +name: sword_game + services: db: image: mysql:8.0 - container_name: socket_server_db + container_name: game_server_db restart: always environment: MYSQL_ROOT_PASSWORD: root_password MYSQL_DATABASE: socket_server - ports: - - "33306:3306" volumes: - ./schema.sql:/docker-entrypoint-initdb.d/schema.sql + + server: + build: + context: . + dockerfile: Dockerfile + container_name: game_server + restart: always + depends_on: + - db + environment: + - DB_HOST=db + - DB_PORT=3306 + - DB_USER=root + - DB_PASSWORD=root_password + - DB_NAME=socket_server + ports: + - "30000:30000" + +networks: + default: + driver: bridge + name: sword_game_network \ No newline at end of file diff --git a/server/main.cpp b/server/main.cpp index d1f8e07..3270f08 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -12,9 +12,22 @@ int main() { boost::asio::io_context main_context; - if (!DatabaseManager::GetInstance().Init(main_context, "127.0.0.1", 33306, - "root", "root_password", - "socket_server")) { + // 환경 변수에서 DB 설정 읽기 + const char *env_db_host = std::getenv("DB_HOST"); + const char *env_db_port = std::getenv("DB_PORT"); + const char *env_db_user = std::getenv("DB_USER"); + const char *env_db_pass = std::getenv("DB_PASSWORD"); + const char *env_db_name = std::getenv("DB_NAME"); + + std::string db_host = env_db_host ? env_db_host : "127.0.0.1"; + uint16_t db_port = + env_db_port ? static_cast(std::stoi(env_db_port)) : 33306; + std::string db_user = env_db_user ? env_db_user : "root"; + std::string db_pass = env_db_pass ? env_db_pass : "root_password"; + std::string db_name = env_db_name ? env_db_name : "socket_server"; + + if (!DatabaseManager::GetInstance().Init(main_context, db_host, db_port, + db_user, db_pass, db_name)) { Logger::Log("DB 연결에 실패했습니다."); return 1; }