Skip to content

Commit

Permalink
Adding documentation, examples and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
muuki88 committed Feb 13, 2015
1 parent ac82b8d commit 816b770
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 16 deletions.
29 changes: 16 additions & 13 deletions src/main/scala/com/typesafe/sbt/packager/docker/DockerPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import sbt.Keys.{
import packager.Keys._
import linux.LinuxPlugin.autoImport.{ daemonUser, defaultLinuxInstallLocation }
import universal.UniversalPlugin.autoImport.stage
import SbtNativePackager.Universal
import SbtNativePackager.{ Universal, Linux }

/**
* == Docker Plugin ==
Expand Down Expand Up @@ -74,17 +74,18 @@ object DockerPlugin extends AutoPlugin {

val generalCommands = makeFrom(dockerBaseImage.value) +: makeMaintainer((maintainer in Docker).value).toSeq

generalCommands ++
generalCommands ++ Seq(
makeWorkdir(dockerBaseDirectory),
makeAdd(dockerBaseDirectory),
makeChown(user, group, "." :: Nil)
) ++
makeExposePorts(dockerExposedPorts.value) ++
makeVolumes(dockerExposedVolumes.value, user, group) ++
Seq(
makeAdd(dockerBaseDirectory),
makeWorkdir(dockerBaseDirectory),
makeChown(user, group, "."),
makeUser(user),
makeEntrypoint(dockerEntrypoint.value),
makeCmd(dockerCmd.value)
) ++
makeExposePorts(dockerExposedPorts.value) ++
makeVolumes(dockerExposedVolumes.value, user, group)
)

}

Expand Down Expand Up @@ -129,7 +130,7 @@ object DockerPlugin extends AutoPlugin {
* @return MAINTAINER if defined
*/
private final def makeMaintainer(maintainer: String): Option[CmdLike] =
if (maintainer == null || maintainer.isEmpty) None else Some(Cmd("MAINTAINER", maintainer))
if (maintainer.isEmpty) None else Some(Cmd("MAINTAINER", maintainer))

/**
* @param dockerBaseImage
Expand Down Expand Up @@ -158,8 +159,8 @@ object DockerPlugin extends AutoPlugin {
* @param directory to chown recursively
* @return chown command, owning the installation directory with the daemonuser
*/
private final def makeChown(daemonUser: String, daemonGroup: String, directory: String): CmdLike =
ExecCmd("RUN", "chown", "-R", s"$daemonUser:$daemonGroup", directory)
private final def makeChown(daemonUser: String, daemonGroup: String, directories: Seq[String]): CmdLike =
ExecCmd("RUN", Seq("chown", "-R", s"$daemonUser:$daemonGroup") ++ directories: _*)

/**
* @param daemonUser
Expand Down Expand Up @@ -199,12 +200,14 @@ object DockerPlugin extends AutoPlugin {
*
* @param exposedVolumes
* @return commands to create, chown and declare volumes
* @see http://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes
* @see https://docs.docker.com/userguide/dockervolumes/
*/
private final def makeVolumes(exposedVolumes: Seq[String], daemonUser: String, daemonGroup: String): Seq[CmdLike] = {
if (exposedVolumes.isEmpty) Seq()
if (exposedVolumes.isEmpty) Seq.empty
else Seq(
ExecCmd("RUN", Seq("mkdir", "-p") ++ exposedVolumes: _*),
makeChown(daemonUser, daemonGroup, exposedVolumes mkString " "),
makeChown(daemonUser, daemonGroup, exposedVolumes),
ExecCmd("VOLUME", exposedVolumes: _*)
)
}
Expand Down
15 changes: 15 additions & 0 deletions src/sbt-test/docker/override-commands/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import com.typesafe.sbt.packager.docker._

enablePlugins(JavaAppPackaging)

name := "docker-commands"

version := "0.1.0"

maintainer := "Gary Coady <gary@lyranthe.org>"

dockerCommands := Seq(
Cmd("FROM", "dockerfile/java:latest"),
Cmd("MAINTAINER", maintainer.value),
ExecCmd("CMD", "echo", "Hello, World from Docker")
)
1 change: 1 addition & 0 deletions src/sbt-test/docker/override-commands/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Main extends App {
println("Hello world")
}
3 changes: 3 additions & 0 deletions src/sbt-test/docker/override-commands/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generate the Docker image locally
> docker:publishLocal
$ exec bash -c 'docker run docker-test:latest | grep -q "Hello world"'
2 changes: 1 addition & 1 deletion src/sbt-test/docker/volumes/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ name := "simple-test"

version := "0.1.0"

dockerExposedVolumes in Docker := Seq("/opt/docker/logs", "/opt/docker/config")
dockerExposedVolumes := Seq("/opt/docker/logs", "/opt/docker/config")
1 change: 1 addition & 0 deletions src/sbt-test/docker/volumes/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Stage the distribution and ensure files show up.
> docker:stage
$ exec grep -q -F 'VOLUME ["/opt/docker/logs", "/opt/docker/config"]' target/docker/Dockerfile
$ exec grep -q -F 'RUN ["chown", "-R", "daemon:daemon", "/opt/docker/logs", "/opt/docker/config"]' target/docker/Dockerfile
$ exec grep -q -F 'RUN ["mkdir", "-p", "/opt/docker/logs", "/opt/docker/config"]' target/docker/Dockerfile
97 changes: 95 additions & 2 deletions src/sphinx/formats/docker.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ The Docker support provides the following commands:
Customize
---------

There are some predefined settings, which you can easily customize. These
settings are explained in some detail in the next sections. If you want to
describe your Dockerfile completely yourself, you can provide your own
`docker commands` as described in `Custom Dockerfile`_.

Docker Image Name
~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -149,9 +154,13 @@ Docker Image Customization

.. code-block:: scala
dockerExposedPorts in Docker := Seq(9000, 9443)
dockerExposedPorts := Seq(9000, 9443)
dockerExposedVolumes := Seq("/opt/docker/logs")
dockerExposedVolumes in Docker := Seq("/opt/docker/logs")
In order to work properly with `USER daemon` the exposed volumes first
created (if not existend) and chowned.

Install Location
~~~~~~~~~~~~~~~~
Expand All @@ -161,3 +170,87 @@ The files from ``mappings in Docker`` are extracted underneath this directory.
.. code-block:: scala
defaultLinuxInstallLocation in Docker := "/opt/docker"
Custom Dockerfile
~~~~~~~~~~~~~~~~~

All settings before are used to create a single sequence of docker commands.
You have the option to write all of them on your own, filter or change existing
commands or simply add some.

First of all you should take a look what you docker commands look like.
In your sbt console type

.. code-block:: bash
> show dockerCommands
[info] List(Cmd(FROM,dockerfile/java:latest), Cmd(MAINTAINER,Your Name <y.n@yourcompany.com>), ...)
Remove Commands
===============

SBT Native Packager added some commands you may not need. For example
the chowning of a exposed volume.

.. code-block:: scala
import com.typesafe.sbt.packager.docker._
// we want to filter the chown command for '/data'
dockerExposedVolumes += "/data"
dockerCommands := dockerCommands.value.filterNot {
// ExecCmd is a case class, and args is a varargs variable, so you need to bind it with @
case ExecCmd("RUN", args @ _*) => args.contains("chown") && args.contains("/data")
// dont filter the rest
case cmd => false
}
Add Commands
============

Adding commands is as straigtforward as adding anything in a list.

.. code-block:: scala
import com.typesafe.sbt.packager.docker._
dockerCommands += Cmd("USER", daemonUser.value)
dockerCommands ++= Seq(
// setting the run script executable
ExecCmd("RUN",
"chmod", "u+x",
s"${(defaultLinuxInstallLocation in Docker).value}/bin/${executableScriptName.value}"),
// setting a daemon user
Cmd("USER", "daemon")
)
Write from Scratch
==================

You can simply wipe out all docker commands with

.. code-block:: scala
dockerCommands := Seq()
Now let's start adding some Docker commands.

.. code-block:: scala
import com.typesafe.sbt.packager.docker._
dockerCommands := Seq(
Cmd("FROM", "dockerfile/java:latest"),
Cmd("MAINTAINER", maintainer.value),
ExecCmd("CMD", "echo", "Hello, World from Docker")
)
2 changes: 2 additions & 0 deletions test-project-docker/build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.typesafe.sbt.packager.docker._

enablePlugins(JavaAppPackaging)

name := "docker-test"
Expand Down
8 changes: 8 additions & 0 deletions test-project-docker/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import java.nio.file._
import scala.util._

object Main extends App {
println("Hello world")

val path = Paths get "/data/test01"
val result = Try(Files createFile path)

println(result)
}

0 comments on commit 816b770

Please sign in to comment.