@Component
public class MyBean {
@Value("${foo.bar}")
private String name;
// ...
}
$ export FOO_BAR=mobydock
@ConfigurationProperties("foo")
@Component
public class MyBeanProperties {
private String name;
public String getName() { ... }
public void setName(String name) { ... }
}
Use Type-safe Configuration Properties
Create a GreeterConfiguration component using the @ConfigurationProperties annotation
$ GREETER_NAME=gradle-runner ./gradlew bootrun
$ git clone https://github.com/devops-gathering/bootiful-hello-world.git
buildscript {
ext {
springBootVersion = '1.5.1.RELEASE'
}
repositories {
jcenter()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath 'com.bmuschko:gradle-docker-plugin:3.0.5'
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
repositories {
jcenter()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
apply plugin: 'com.bmuschko.docker-remote-api'
import com.bmuschko.gradle.docker.tasks.image.Dockerfile
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
def dockerBuildDir = 'build/docker/'
def applicationJar = "${archivesBaseName}.jar"
task copyJar(type: Copy) {
dependsOn bootRepackage
file(dockerBuildDir).mkdirs()
from "$libsDir/$applicationJar"
into dockerBuildDir
}
task createDockerfile(type: Dockerfile) {
dependsOn copyJar
destFile = project.file(dockerBuildDir + "Dockerfile")
from 'openjdk:8-jre'
copyFile(applicationJar, "/")
entryPoint "sh", "-c",
"java \$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /$applicationJar"
}
task buildImage(type: DockerBuildImage) {
dependsOn createDockerfile
inputDir = createDockerfile.destFile.parentFile
tag = 'devops/hello-world'
}
$ docker run \
--rm \
--name "hello-world-test" \
-e "GREETER_NAME=docker-command-line" \
devops/hello-world
$ ./gradlew buildImage
(or via IDE...)
version: "3"
services:
greeter:
image: dog.bootcamp/greeter
environment:
GREETER_NAME: docker-compose
Like before:
image is the tag from the build.gradle
Configure application properties via environment variable
$ docker-compose up
class HelloSpockSpec extends spock.lang.Specification {
def "length of Spock's and his friends' names"() {
expect:
name.size() == length
where:
name | length
"Spock" | 5
"Kirk" | 4
"Scotty" | 6
}
}
@ContextConfiguration
@SpringBootTest
class ApplicationTests extends Specification {
def "should boot up"() {
expect:
true
}
}
$ git clone https://github.com/devops-gathering/bootiful-book-store.git
$ gradlew check
Run the test suite with:
For better visualized results, run the gradle task from your IDE:
@Testcontainers
class JpaTests extends Specification {
@Shared
PostgreSQLContainer postgresContainer = new PostgreSQLContainer()
.withDatabaseName("bootiful")
.withUsername("bootiful")
.withPassword("secret")
...
}
Your client tells you they're - by some obscure policy - not allowed to run a postgres database. They are instead confined to using MySQL 5.5. No way you're going to install that on your local machine for testing.
Our bookstore application depends on creating the schema at startup...
In our integration test, using the PostgreSQLContainer ensures the database is ready.
But how do we ensure the database is ready in our deployment scenario?
version: "2"
services:
db:
image: postgres
myservice:
image: devops/myservice
depends_on:
- "db"
entrypoint:
- dockerize
- -wait
- tcp://db:5432
- -timeout
- 120s
- java
- -jar
- myservice.jar
In your createDockerfile-task:
task createDockerfile(type: Dockerfile) {
[...]
// Install dockerize
environmentVariable('DOCKERIZE_VERSION', 'v0.3.0')
runCommand 'apt-get update && apt-get install -y wget'
runCommand 'wget \
https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz'
[...]
}
https://github.com/jwilder/dockerize#installation