用sbt-native-packager的docker支持添加/复制文件
我正在使用sbt-native-packager 1.0.0-M5创build我的泊坞窗图像。 我需要添加一个不是源文件或资源文件夹的文件。 我的docker命令如下:
dockerCommands := Seq( Cmd("FROM", "myrepo/myImage:1.0.0"), Cmd("COPY", "test.txt keys/"), // <-- The failing part Cmd("WORKDIR", "/opt/docker"), Cmd("RUN", "[\"chown\", \"-R\", \"daemon\", \".\"]"), Cmd("USER", "daemon"), ExecCmd("CMD", "echo", "Hello, World from Docker") )
它失败: msg="test.txt: no such file or directory"
所以挖了一下后,似乎我需要在target/docker/stage
test.txt
。 然后它工作。 但是,我怎样才能自动得到它呢? 该文件实际上在项目的根文件夹中。
我设法通过将文件添加到mappings in Universal
。 所以对于你,你需要这样的东西:
mappings in Universal += file("test.txt") -> "keys/test.txt"
顺便说一下,如果你这样做,你将不需要COPY
命令。
现在,我不确定是否要将此映射添加到其他sbt-native-packager插件。 我希望评论者能告诉我这是否是真实的,但是我的直觉是它会这样做,这对你来说可能是一个破坏者。 但是,任何解决方法总比没有好,对吧? 如果你使用Build.scala
你可以使用VM参数来告诉是否添加这个映射。
您可以将所有其他文件(必须包含在容器映像中)放到文件夹src/universal
。 该文件夹的内容将自动复制到您的容器图像中的/opt/app
文件夹中。 你不需要任何额外的configuration。 有关更多信息,请参阅“Universal Packaging入门” 。
我能够得到这个使用dockerPackageMappings
工作:
dockerPackageMappings in Docker += (baseDirectory.value / "docker" / "ssh_config") -> "ssh_config" dockerCommands := (dockerCommands.value match { case Seq(from@Cmd("FROM", _), rest@_*) => Seq( from, Cmd("Add", "ssh_config", "/sbin/.ssh/config") ) ++ rest })
我能够以这种方式添加文件:
例如,要添加位于src/main/resources/docker/some-file.ext
dockerfile in docker := { val targetPath = "/usr/app" // map of (relativeName -> File) of all files in resources/docker dir, for convenience val dockerFiles = { val resources = (unmanagedResources in Runtime).value val dockerFilesDir = resources.find(_.getPath.endsWith("/docker")).get resources.filter(_.getPath.contains("/docker/")).map(r => dockerFilesDir.toURI.relativize(r.toURI).getPath -> r).toMap } new Dockerfile { from(s"$namespace/$baseImageName:$baseImageTag") ... add(dockerFiles("some-file.ext"), s"$targetPath/some-file.ext") ... } }