feat: docker 이용한 서버 실행 및 사용 설명 추가, 환경변수 이용한 주소

처리 추가
This commit is contained in:
bumpsoo 2026-02-05 13:02:07 +00:00
parent f108967cc7
commit ad5c01fd86
6 changed files with 85 additions and 14 deletions

3
.dockerignore Normal file
View file

@ -0,0 +1,3 @@
compile_commands.json
.cache
build

17
Dockerfile Normal file
View file

@ -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"]

View file

@ -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
```
## 환경 설정

View file

@ -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) {

View file

@ -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

View file

@ -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<uint16_t>(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;
}