Skip to content

Commit

Permalink
MATE-142 : [FEAT] 도커 파일 내 크롬 및 크롬 드라이버 설정 (#125)
Browse files Browse the repository at this point in the history
MATE-142 : [FEAT] 도커 파일 내 크롬 드라이버 설치

- 1. 환경 별 드라이버 실행 구현
  • Loading branch information
juchan204 authored Jan 3, 2025
1 parent 9b41169 commit 68e43a6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ RUN apk add --no-cache \
freetype \
harfbuzz

# Chrome 환경변수 설정
ENV CHROME_BIN=/usr/bin/chromium-browser \

COPY ./build/libs/*SNAPSHOT.jar project.jar
ENTRYPOINT redis-server & java -jar project.jar
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,49 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.io.BufferedReader;
import java.io.InputStreamReader;

@Configuration
@Slf4j
public class WebDriverConfig {

private static final boolean IS_DOCKER = System.getenv().containsKey("CHROME_BIN");

@Bean
@Scope("prototype")
public WebDriver chromeDriver() {
log.info("Creating new Chrome WebDriver instance...");
log.info("Creating new Chrome WebDriver instance... Docker environment: {}", IS_DOCKER);
try {
WebDriverManager.chromedriver().setup();
log.info("WebDriverManager setup completed");

ChromeOptions options = new ChromeOptions();

if (IS_DOCKER) {
// Docker 환경 설정
log.info("Configuring for Docker environment");
System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");
System.setProperty("chrome.binary", "/usr/bin/chromium-browser");
options.setBinary("/usr/bin/chromium-browser");
} else {
// 로컬 환경 설정
log.info("Configuring for local environment");
WebDriverManager.chromedriver().setup();
}

// 공통 옵션 설정
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--remote-allow-origins=*");

return new ChromeDriver(options);
// Docker 환경에서 하드웨어 가속화 비활성화 (필수)
if (IS_DOCKER) {
options.addArguments("--disable-gpu");
}

WebDriver driver = new ChromeDriver(options);
log.info("ChromeDriver successfully initialized");
return driver;

} catch (Exception e) {
log.error("Failed to initialize Chrome WebDriver: ", e);
throw new CustomException(ErrorCode.WEBDRIVER_ERROR);
Expand Down

0 comments on commit 68e43a6

Please sign in to comment.