From d08422f92983c43aeba82fedd794fa059c1dba3d Mon Sep 17 00:00:00 2001 From: Guntis Smaukstelis Date: Tue, 19 Dec 2023 14:47:45 +0200 Subject: [PATCH] Docker compose for scala and postgres --- .dockerignore | 1 - Dockerfile.local | 29 ++++++++++++++++++++++++++++ docker-compose.yml | 23 ++++++++++++++++++++++ src/main/scala/db/DBConnection.scala | 2 +- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 Dockerfile.local create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore index c085b90..31b335e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,4 @@ # flyctl launch added from .gitignore -**/src/main/resources/application.conf **/project/target **/project/project/target diff --git a/Dockerfile.local b/Dockerfile.local new file mode 100644 index 0000000..cea7166 --- /dev/null +++ b/Dockerfile.local @@ -0,0 +1,29 @@ +# Use a base image with Java and Scala, including SBT +FROM hseeberger/scala-sbt:17.0.2_1.6.2_2.13.8 as build + +# Set the working directory +WORKDIR /app + +# Copy project files to the Docker image +COPY . /app + +# Compile the application +RUN sbt clean assembly + +# Use a smaller base image for the final image +FROM amazoncorretto:17-alpine + +# Copy the compiled JAR from the build stage +COPY --from=build /app/target/scala-2.13/WeatherTool-assembly-0.1.1-SNAPSHOT.jar /app.jar + +# Define the volume to persist data +VOLUME /data + +# Command to run the application +CMD ["java", "-jar", "/app.jar"] + + + + +# docker build -t scala-app . +# docker run -v /local/data/path:/data scala-app \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ed3078a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,23 @@ +version: '3' +services: + postgres: + image: postgres:latest + ports: + - "5432:5432" + environment: + POSTGRES_DB: weather-tool + POSTGRES_USER: postgres + POSTGRES_PASSWORD: mysecretpassword + volumes: + - /var/lib/postgresql/data:/var/lib/postgresql/data + + scala-app: + build: + context: . + dockerfile: Dockerfile.local + volumes: + - $PWD:/app + ports: + - "8080:8080" + depends_on: + - postgres \ No newline at end of file diff --git a/src/main/scala/db/DBConnection.scala b/src/main/scala/db/DBConnection.scala index 3c0a2f9..e24ee06 100644 --- a/src/main/scala/db/DBConnection.scala +++ b/src/main/scala/db/DBConnection.scala @@ -13,7 +13,7 @@ case class PostgresConfig(url: String, username: String, password: String) object DBConnection { private def getDatabaseUrl: String = - sys.env.getOrElse("DATABASE_URL", "postgres://postgres:mysecretpassword@localhost:5432/weather-tool") + sys.env.getOrElse("DATABASE_URL", "postgres://postgres:mysecretpassword@postgres:5432/weather-tool") private def parseUrl(url: String): Either[String, PostgresConfig] = { Try {