Search code examples
dockerscalasbtsbt-native-packager

How to install apt-get dependencies in Docker image using sbt-native-packager plugin


I am using the sbt-native-packager plugin to generate a Docker image for a Scala project. The project requires a specific package libnetcdf.so to be installed on the image.

I am using the below configuration on my build.sbt

dockerCommands ++= Seq(
  ExecCmd("RUN","sudo","apt-get","update"),
  ExecCmd("RUN","sudo","apt-get","install","-y","libnetcdf-dev")
)

However when generating/building the image using sbt docker:publishLocal I get the below errors.

[error] #18 [mainstage 6/7] RUN ["sudo", "apt-get", "update"]
[error] #18 0.358 runc run failed: unable to start container process: exec: "sudo": executable file not found in $PATH
[error] #18 ERROR: process "sudo apt-get update" did not complete successfully: exit code: 1
[error] ------
[error]  > [mainstage 6/7] RUN ["sudo", "apt-get", "update"]:
[error] 0.358 runc run failed: unable to start container process: exec: "sudo": executable file not found in $PATH
[error] ------
[error] Dockerfile:24
[error] --------------------
[error]   22 |     ENTRYPOINT ["/opt/docker/bin/root"]
[error]   23 |     CMD []
[error]   24 | >>> RUN ["sudo", "apt-get", "update"]
[error]   25 |     RUN ["sudo", "apt-get", "install", "-y", "libnetcdf-dev"]
[error]   26 |
[error] --------------------
[error] ERROR: failed to solve: process "sudo apt-get update" did not complete successfully: exit code: 1

Cannot find the right way to install a dependency using the dockerCommands

Any help/pointers will be greatly appreciated.

EDIT:

Silly enough, it turns the default base image was not a Debian-based distro. Replaced with an Ubuntu docker image and it all worked as intended. As per @Gaël J response it doesn't need "sudo".

Was able to figure the distribution using the below.

ExecCmd("RUN","cat","/etc/os-release")

Solution

  • Remove sudo.

    ExecCmd("RUN","apt-get","update")
    

    It doesn't exist in the image as the error says.

    And moreover you don't need it because the build is likely done as root user, and only switched to a non-root user at the end. (Worth checking that though, I don't know what the SBT plugin generates by default).