Prepopulating dependencies in docker build step fails if kscript runs from root directory.
I built the kscript 3.1.0 Docker image (didn't find it the main docker repo) and based a Dockerfile on that, to create a script-image with prepopulated dependencies:
FROM .../kscript:3.1.0
COPY *.kts *.kt /root/
RUN /bin/bash -c "source /root/.sdkman/bin/sdkman-init.sh && /usr/bin/env kscript /root/adb-monitor.kts --early-exit"
ENTRYPOINT ["/bin/bash", "-c", "source /root/.sdkman/bin/sdkman-init.sh && cd && /usr/bin/env kscript /root/adb-monitor.kts \"$@\""]
The script starts by checking for the option '--early-exit' and quits immediately, so the requirements are cached ready for the entrypoint:
@file:DependsOn("com.fasterxml.jackson.module:jackson-module-kotlin:2.8.8")
import com.fasterxml.jackson.module.kotlin.*
import com.fasterxml.jackson.databind.ObjectMapper
if (args.contains("--early-exit")) System.exit(0)
but if the RUN command doesn't change directory away from the root directory, pulling the dependencies fails:
Step 3/4 : RUN /bin/bash -c "source /root/.sdkman/bin/sdkman-init.sh && /usr/bin/env kscript /root/adb-monitor.kts --early-exit"
---> Running in 8c813ea13a87
[kscript] Resolving dependencies...
[kscript] Resolving com.fasterxml.jackson.module:jackson-module-kotlin:2.8.8...
[kscript] [ERROR] Failed while connecting to the server. Check the connection (http/https, port, proxy, credentials, etc.) of your maven dependency locators. If you suspect this is a bug, you can create an issue on https://github.com/holgerbrandl/kscript
[kscript] [ERROR] Exception: java.lang.NullPointerException
If I add ' && cd ' just before the kscript invocation (as shown in the ENTRYPOINT) then it works:
Step 3/4 : RUN /bin/bash -c "source /root/.sdkman/bin/sdkman-init.sh && cd && /usr/bin/env kscript /root/adb-monitor.kts --early-exit"
---> Running in 9cebdf068251
[kscript] Resolving dependencies...
[kscript] Resolving com.fasterxml.jackson.module:jackson-module-kotlin:2.8.8...
[kscript] Resolving com.github.holgerbrandl:kscript-annotations:1.4...
[kscript] Dependencies resolved
Arg --early-exit requested exit
Removing intermediate container 9cebdf068251
- I don't know why running it at / fails the dependency handling, but it would be good if it worked - or at least printed a warning about it.
- It would be nice if there were an option to load the dependencies but not run the script. :)
Please check, the docker-file has been reworked entirely in https://github.com/holgerbrandl/kscript/pull/325
In my understanding, kscript should not mind the working directory at all. So it's indeed a surprising finding that it does. Can you reproduce this problem outside of docker? Also, the error seems to point more towards issues with networking (e.g. name resolution when trying to read from maven central).
I am not sure if this problem really is about kscript. It seems more about docker to me. But it's certainly a valid use-case.
I am closing this issue as there are no updates on this ticket. However, if the problem is still on the latest kscript version (4.2.0), please create a new ticket with a test case.