From 4bf94aaad4a243f91ad7aba0b441e255c20fe3af Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Tue, 4 Nov 2025 21:18:28 +0700 Subject: [PATCH] Add Docker support and environment configuration Introduced Dockerfile, .dockerignore, and example environment file for containerization. Added compose.yml for Docker Compose setup and renamed docker-compose.dev.yml to compose.dev.yml. Updated build.gradle.kts for Java 21 and shadow plugin, and improved .gitignore and .gitattributes. Refactored Config.java to reorder ENV initialization. --- .dockerignore | 47 +++++++++++++++++++ .env.example | 22 +++++++++ .gitattributes | 6 +++ .gitignore | 1 + Dockerfile | 19 ++++++++ build.gradle.kts | 17 +++++-- docker-compose.dev.yml => compose.dev.yml | 2 + compose.yml | 26 ++++++++++ .../miti99/storescraperbot/config/Config.java | 4 +- 9 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 .dockerignore create mode 100644 .env.example create mode 100644 .gitattributes create mode 100644 Dockerfile rename docker-compose.dev.yml => compose.dev.yml (92%) create mode 100644 compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4ba6ec6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,47 @@ +**/.DS_Store +**/.classpath +**/.dockerignore +**/.env +**/.factorypath +**/.git +**/.gitignore +**/.idea +**/.project +**/.sts4-cache +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose.y*ml +**/Dockerfile* +**/secrets.dev.yaml +**/values.dev.yaml +**/vendor +LICENSE +README.md +**/*.class +**/*.iml +**/*.ipr +**/*.iws +**/*.log +**/.apt_generated +**/.gradle +**/.gradletasknamecache +**/.nb-gradle +**/.springBeans +**/build +**/dist +**/gradle-app.setting +**/nbbuild +**/nbdist +**/nbproject/private +*.ctxt +.mtj.tmp +hs_err_pid* +replay_pid* \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f687753 --- /dev/null +++ b/.env.example @@ -0,0 +1,22 @@ +# Copy this file to .env and customize the values for your environment +# cp .env.example .env + +# Couchbase configuration +COUCHBASE_CONNECTION_STRING=localhost:8091 +COUCHBASE_USERNAME=admin +COUCHBASE_PASSWORD=your_password_here +COUCHBASE_BUCKET_NAME=store_scraper + +# Telegram Bot configuration +TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here +TELEGRAM_BOT_USERNAME=your_bot_username + +# Java configuration +JAVA_OPTS=-Xmx512m + +# Logging configuration +LOG_LEVEL=INFO + +# App configuration +ENV=DEVELOPMENT +ADMIN_IDS=1000001999,1000002000,1000002001 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..417cfbe --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +* text=auto + +*.java text eol=crlf + +*.sh text eol=lf +gradlew text eol=lf diff --git a/.gitignore b/.gitignore index e58b3f6..12ad9c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6360a3a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM amazoncorretto:21.0.5 AS deps +WORKDIR /build +COPY --chmod=0755 gradlew gradlew +COPY gradle/ gradle/ +RUN --mount=type=bind,source=build.gradle.kts,target=build.gradle.kts \ + --mount=type=cache,target=/root/.gradle \ + ./gradlew dependencies --no-daemon + +FROM deps AS package +WORKDIR /build +COPY ./src src/ +RUN --mount=type=bind,source=build.gradle.kts,target=build.gradle.kts \ + --mount=type=cache,target=/root/.gradle \ + ./gradlew shadowJar -x test --no-daemon +RUN cp build/libs/*-all.jar app.jar + +FROM amazoncorretto:21.0.5 AS final +COPY --from=package build/app.jar app.jar +ENTRYPOINT [ "sh", "-c", "java ${JAVA_OPTS} -jar app.jar" ] diff --git a/build.gradle.kts b/build.gradle.kts index 55da747..c57edc6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,14 +1,11 @@ plugins { - id("java") + java + id("com.gradleup.shadow") version "8.3.5" } group = "com.miti99" version = "1.0-SNAPSHOT" -repositories { - mavenCentral() -} - configurations { compileOnly { extendsFrom(configurations.annotationProcessor.get()) @@ -35,6 +32,16 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") } +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(21)) + } +} + +repositories { + mavenCentral() +} + tasks.test { useJUnitPlatform() } diff --git a/docker-compose.dev.yml b/compose.dev.yml similarity index 92% rename from docker-compose.dev.yml rename to compose.dev.yml index 534ffa6..4a2e505 100644 --- a/docker-compose.dev.yml +++ b/compose.dev.yml @@ -1,6 +1,8 @@ services: couchbase: image: couchbase:community-7.6.2 + env_file: + - .env ports: - "8091-8097:8091-8097" - "9123:9123" diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..119e412 --- /dev/null +++ b/compose.yml @@ -0,0 +1,26 @@ +services: + server: + build: + context: . + env_file: + - .env + depends_on: + - couchbase + + couchbase: + image: couchbase:community-7.6.2 + # env_file: + # - .env + # ports: # Enable these ports if you need + # - "8091-8097:8091-8097" + # - "9123:9123" + # - "11207:11207" + # - "11210:11210" + # - "11280:11280" + # - "18091-18097:18091-18097" + volumes: + - couchbase_data:/opt/couchbase/var + restart: unless-stopped + +volumes: + couchbase_data: diff --git a/src/main/java/com/miti99/storescraperbot/config/Config.java b/src/main/java/com/miti99/storescraperbot/config/Config.java index 3fe44aa..2ecc0d6 100644 --- a/src/main/java/com/miti99/storescraperbot/config/Config.java +++ b/src/main/java/com/miti99/storescraperbot/config/Config.java @@ -8,8 +8,6 @@ import java.util.Set; import java.util.stream.Collectors; public class Config { - public static final Env ENV = Env.valueOf(System.getenv("ENV")); - public static final String COUCHBASE_CONNECTION_STRING = System.getenv("COUCHBASE_CONNECTION_STRING"); public static final String COUCHBASE_USERNAME = System.getenv("COUCHBASE_USERNAME"); @@ -19,7 +17,7 @@ public class Config { public static final String TELEGRAM_BOT_TOKEN = System.getenv("TELEGRAM_BOT_TOKEN"); public static final String TELEGRAM_BOT_USERNAME = System.getenv("TELEGRAM_BOT_USERNAME"); - public static final Long CREATOR_ID = Long.parseLong(System.getenv("CREATOR_ID")); + public static final Env ENV = Env.valueOf(System.getenv("ENV")); public static final Set ADMIN_IDS = Optional.ofNullable(System.getenv("ADMIN_IDS")) .map(