Last updated on 16th July 2024
Before you begin make sure your model is added inside org.models package
, and registered inside the main class. You should also confirm being able to run locally before deploying.
The first step is to add the docker & assembly plugin to your environment. You can do this by going to your .sbt directory. Choosing the correct version you will see a 'plugins.sbt' file (this step should be familiar if using SBT and Eclipse; however, if the file/folder is missing you will need to add them) and add the following.
plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.5.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.3.3")
To build in SBT make sure you have properly configured your build.sbt
file. You'll need to add the below section to both enable the docker plugin, and configure how it will build your docker image.
build.sbt
//*** SBT ASSEMBLY ***
assemblyMergeStrategy in assembly := {
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
//akka configuration files
case PathList("reference.conf") => MergeStrategy.concat
case _ => MergeStrategy.first
}
assemblyJarName in assembly := "simudyne-sbt-docker.jar"
//*** SBT DOCKER ***
enablePlugins(sbtdocker.DockerPlugin)
// Generating docker file
dockerfile in docker := {
val artifact: File = assembly.value
val artifactTargetPath = s"/${artifact.name}"
new Dockerfile {
from("simudyne/scala-sbt:2.11.12.1.0.4")
copy(baseDirectory(_ / "simudyneSDK.properties" ).value, s"/simudyneSDK.properties")
copy(artifact, artifactTargetPath)
entryPoint("java", "-jar", artifactTargetPath)
}
}
imageNames in docker := Seq(
ImageName(s"${name.value}-$simudyneVersion:latest")
)
Build the image by running 'sbt docker' inside the project's folder. This will create a docker image using the dockerfile generated by sbt in a way defined inside 'build.sbt'.
You can now proceed to 'Pushing with Docker Hub'
The first step is to make a few changes to your pom.xml (if not pulling from the quickstart repo). Make sure you add the jodatime and junit versions to your properties section.
pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jodatime.version>2.5</jodatime.version>
<junit.version>4.11</junit.version>
</properties>
You will then want to add the jodatime and junit dependencies to the section starting with '
pom.xml
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${jodatime.version}</version>
</dependency>
Finally you will add the plugins for assembling and dockerize-ing the project.
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>allinone</shadedClassifierName>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<main-Class>Main</main-Class>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>simudyne-maven-docker</imageName>
<baseImage>simudyne/scala-sbt:2.11.12.1.0.4</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}-allinone.jar"]</entryPoint>
<!-- copy the service's jar file from target into the root directory of the image -->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}-allinone.jar</include>
</resource>
<resource>
<targetPath>/</targetPath>
<directory>.</directory>
<include>simudyneSDK.properties</include>
</resource>
</resources>
</configuration>
</plugin>
You will now push the docker image you have created to the Docker hub. You will want to replace the following things.
An example of this would be simudyne/simu-repo:demos
sudo docker tag [build] [account]/[repo-name]:[tag]
sudo docker push [account]/[repo-name]
You should be able to navigate to the Docker hub and confirm your image has been updated.
You must first log into the VM or server which you are deploying on. Please make sure the appropriate firewall and ports are made available based on either your public or intranet settings.
Pull the image using the details you specified above.
sudo docker pull [account]/[repo-name]:[tag]
You will likely already be running a container if not please skip to the next step. However, if you are you need to stop the existing container.
sudo docker ps #to identify the container ID
sudo docker stop <containerID>
You will then look for the exact image name, and then run it with the following configuration options.
An example of this would be 'docker run -p 80:80 f3c0fa608b22 0.0.0.0 80'
sudo docker images # to have a look at the available images and get ImageID
sudo docker run -p <port>:<port> <ImageID> <ip> <port>
Once completed you should be able to go to the IP or DNS address of the server you've deployed on (mind the port) and see the console as you would locally.