I have created a Kotlin application in IntelliJ with the following settings:
When I try to containerize this appliaction to Docker I get the following error:
=> ERROR [builder 9/11] RUN ./gradlew dependencies --no-daemon
ERROR: failed to solve: process "/bin/sh -c ./gradlew dependencies --no-daemon" did not complete successfully: exit code: 1
I build it with this command:
docker build --tag ktdocker .
I use Windows 11 and my goal it to create a Dockerfile so that I can run the application in Google Cloud Run.
Dockerfile
# Use a lightweight Java runtime image
FROM eclipse-temurin:17-jdk-alpine as builder
# Set the working directory
WORKDIR /app
# Install necessary tools
RUN apk add --no-cache bash curl git dos2unix
# Copy Gradle wrapper and project files
COPY gradlew ./
COPY gradle ./gradle
COPY build.gradle.kts ./
COPY settings.gradle.kts ./
# Fix line endings and permissions for Gradle wrapper
RUN dos2unix gradlew && chmod +x gradlew
# Download Gradle dependencies
RUN ./gradlew dependencies --no-daemon
# Copy the application source code
COPY src ./src
# Build the application
RUN ./gradlew build --no-daemon
# Use a lightweight Java runtime image for the final build
FROM eclipse-temurin:17-jre-alpine
# Set the working directory
WORKDIR /app
# Copy the jar file from the build stage
COPY --from=builder /app/build/libs/*.jar app.jar
# Expose the application port (Cloud Run uses PORT)
EXPOSE 8080
# Command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
Main.kt
package com.demo
fun main() {
val name = "Kotlin"
println("Hello, " + name + "!")
for (i in 1..5) {
println("i = $i")
}
}
build.gradle.kts
plugins {
kotlin("jvm") version "2.0.20"
}
group = "com.demo"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(20)
}
./gradlew -v
------------------------------------------------------------
Gradle 8.8
------------------------------------------------------------
Build time: 2024-05-31 21:46:56 UTC
Revision: 4bd1b3d3fc3f31db5a26eecb416a165b8cc36082
Kotlin: 1.9.22
Groovy: 3.0.21
Ant: Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM: 23.0.1 (Oracle Corporation 23.0.1+11-39)
OS: Windows 11 10.0 amd64
What can I change in my Dockerfile so I can build it without errors?
The error is related to the fact that the gradlew command cannot be executed inside the Docker container.
The reason is that there are no permissions.
You need to use the chmod +x command.
I assume that the following lines should help in solving the problem:
//... without changes
# Copy Gradle wrapper and project files
COPY gradlew .
COPY gradle gradle
COPY build.gradle settings.gradle ./
# permissions for Gradle wrapper
RUN chmod +x gradlew
# permissions for dos2unix
RUN chmod +x dos2unix
# Download Gradle dependencies (without build project)
RUN ./gradlew dependencies --no-daemon
// ... without changes
If I'm wrong, please correct me.
Since the question was asked a long time ago, I assume that you have already solved this problem.
It would be great if you could write an answer to this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With