@@ -13,6 +13,9 @@ import com.typesafe.sbt.SbtNativePackager.Universal
13
13
import com .typesafe .sbt .packager .Compat ._
14
14
import com .typesafe .sbt .packager .{MappingsHelper , Stager }
15
15
16
+ import scala .sys .process .Process
17
+ import scala .util .Try
18
+
16
19
/**
17
20
* == Docker Plugin ==
18
21
*
@@ -80,6 +83,9 @@ object DockerPlugin extends AutoPlugin {
80
83
dockerEntrypoint := Seq (" bin/%s" format executableScriptName.value),
81
84
dockerCmd := Seq (),
82
85
dockerExecCommand := Seq (" docker" ),
86
+ dockerVersion := Try (Process (dockerExecCommand.value ++ Seq (" version --format '{{.Server.Version}}'" )).!! )
87
+ .toOption.map(_.trim)
88
+ .flatMap(DockerVersion .parse),
83
89
dockerBuildOptions := Seq (" --force-rm" ) ++ Seq (" -t" , dockerAlias.value.versioned) ++ (
84
90
if (dockerUpdateLatest.value)
85
91
Seq (" -t" , dockerAlias.value.latest)
@@ -96,7 +102,7 @@ object DockerPlugin extends AutoPlugin {
96
102
val generalCommands = makeFrom(dockerBaseImage.value) +: makeMaintainer((maintainer in Docker ).value).toSeq
97
103
98
104
generalCommands ++
99
- Seq (makeWorkdir(dockerBaseDirectory), makeAdd(dockerBaseDirectory), makeChown( user, group, " . " :: Nil ) ) ++
105
+ Seq (makeWorkdir(dockerBaseDirectory)) ++ makeAdd(dockerVersion.value, dockerBaseDirectory, user, group) ++
100
106
dockerLabels.value.map(makeLabel) ++
101
107
makeExposePorts(dockerExposedPorts.value, dockerExposedUdpPorts.value) ++
102
108
makeVolumes(dockerExposedVolumes.value, user, group) ++
@@ -179,18 +185,34 @@ object DockerPlugin extends AutoPlugin {
179
185
Cmd (" WORKDIR" , dockerBaseDirectory)
180
186
181
187
/**
182
- * @param dockerBaseDirectory, the installation directory
188
+ * @param dockerVersion
189
+ * @param dockerBaseDirectory the installation directory
190
+ * @param daemonUser
191
+ * @param daemonGroup
183
192
* @return ADD command adding all files inside the installation directory
184
193
*/
185
- private final def makeAdd (dockerBaseDirectory : String ): CmdLike = {
194
+ private final def makeAdd (dockerVersion : Option [DockerVersion ], dockerBaseDirectory : String ,
195
+ daemonUser : String , daemonGroup : String ): Seq [CmdLike ] = {
186
196
187
197
/**
188
198
* This is the file path of the file in the Docker image, and does not depend on the OS where the image
189
199
* is being built. This means that it needs to be the Unix file separator even when the image is built
190
200
* on e.g. Windows systems.
191
201
*/
192
202
val files = dockerBaseDirectory.split(UnixSeparatorChar )(1 )
193
- Cmd (" ADD" , s " $files / $files" )
203
+
204
+ dockerVersion match {
205
+ case Some (DockerVersion (major, minor, _, _)) if major >= 17 && minor >= 9 =>
206
+ Seq (
207
+ Cmd (" ADD" , s " --chown= $daemonUser: $daemonGroup $files / $files" )
208
+ )
209
+
210
+ case _ =>
211
+ Seq (
212
+ Cmd (" ADD" , s " $files / $files" ),
213
+ makeChown(daemonUser, daemonGroup, " ." :: Nil )
214
+ )
215
+ }
194
216
}
195
217
196
218
/**
@@ -278,7 +300,7 @@ object DockerPlugin extends AutoPlugin {
278
300
}
279
301
280
302
/**
281
- * uses the `mappings in Unversial ` to generate the
303
+ * uses the `mappings in Universal ` to generate the
282
304
* `mappings in Docker`.
283
305
*/
284
306
def mapGenericFilesToDocker : Seq [Setting [_]] = {
0 commit comments