├── README.md
├── docker-multi-stage-build-demo
├── .gitignore
├── Dockerfile
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ └── HelloController.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── demo
│ └── DemoApplicationTests.java
├── docker-normal-build-demo
├── .gitignore
├── Dockerfile
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── demo
│ │ │ ├── DemoApplication.java
│ │ │ └── HelloController.java
│ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── demo
│ └── DemoApplicationTests.java
└── docker-package-only-build-demo
├── .gitignore
├── Dockerfile
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── example
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ └── HelloController.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # docker-with-java-demos
2 |
3 | These are example projects that correlate with my blog post [here](https://adotpalindrome.wordpress.com/2020/02/25/three-ways-to-create-docker-images-for-java/).
4 |
5 | ## docker-multi-stage-build-demo
6 |
7 | To build the image:
8 |
9 | ```shell
10 | $ cd docker-multi-stage-build-demo
11 | $ docker build -t anna/docker-multi-stage-build-demo:1.0-SNAPSHOT .
12 | ```
13 |
14 | ## docker-normal-build-demo
15 |
16 | To build the image:
17 |
18 | ```shell
19 | $ cd docker-normal-build-demo
20 | $ docker build -t anna/docker-normal-build-demo:1.0-SNAPSHOT .
21 | ```
22 |
23 | ## docker-package-only-build-demo
24 |
25 | First, package the source code:
26 |
27 | ```shell
28 | $ cd docker-package-only-build-demo
29 | $ mvn clean package
30 | ```
31 |
32 | Then, build the image:
33 | ```shell
34 | $ docker build -t anna/docker-normal-build-demo:1.0-SNAPSHOT .
35 | ```
36 |
--------------------------------------------------------------------------------
/docker-multi-stage-build-demo/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/docker-multi-stage-build-demo/Dockerfile:
--------------------------------------------------------------------------------
1 | # the first stage of our build will use a maven 3.6.1 parent image
2 | FROM maven:3.6.1-jdk-8-alpine AS MAVEN_BUILD
3 |
4 | # copy the pom and src code to the container
5 | COPY ./ ./
6 |
7 | # package our application code
8 | RUN mvn clean package
9 |
10 | # the second stage of our build will use open jdk 8 on alpine 3.9
11 | FROM openjdk:8-jre-alpine3.9
12 |
13 | # copy only the artifacts we need from the first stage and discard the rest
14 | # COPY --from=MAVEN_BUILD /docker-multi-stage-build-demo/target/demo-0.0.1-SNAPSHOT.jar /demo.jar
15 | COPY --from=MAVEN_BUILD /target/demo-0.0.1-SNAPSHOT.jar /demo.jar
16 |
17 | # set the startup command to execute the jar
18 | CMD ["java", "-jar", "/demo.jar"]
19 |
--------------------------------------------------------------------------------
/docker-multi-stage-build-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.2.4.RELEASE
9 |
10 |
11 | com.example
12 | demo
13 | 0.0.1-SNAPSHOT
14 | demo
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 | org.junit.vintage
34 | junit-vintage-engine
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-maven-plugin
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/docker-multi-stage-build-demo/src/main/java/com/example/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class DemoApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(DemoApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/docker-multi-stage-build-demo/src/main/java/com/example/demo/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.web.bind.annotation.RestController;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 |
6 | @RestController
7 | public class HelloController {
8 |
9 | @RequestMapping("/")
10 | public String index() {
11 | return "Greetings from Spring Boot!";
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/docker-multi-stage-build-demo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/docker-multi-stage-build-demo/src/test/java/com/example/demo/DemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class DemoApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/docker-normal-build-demo/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/docker-normal-build-demo/Dockerfile:
--------------------------------------------------------------------------------
1 | # select parent image
2 | FROM maven:3.6.3-jdk-8
3 |
4 | # copy the source tree and the pom.xml to our new container
5 | COPY ./ ./
6 |
7 | # package our application code
8 | RUN mvn clean package
9 |
10 | # set the startup command to execute the jar
11 | CMD ["java", "-jar", "target/demo-0.0.1-SNAPSHOT.jar"]
12 |
--------------------------------------------------------------------------------
/docker-normal-build-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.2.4.RELEASE
9 |
10 |
11 | com.example
12 | demo
13 | 0.0.1-SNAPSHOT
14 | demo
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 | org.junit.vintage
34 | junit-vintage-engine
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-maven-plugin
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/docker-normal-build-demo/src/main/java/com/example/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class DemoApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(DemoApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/docker-normal-build-demo/src/main/java/com/example/demo/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.web.bind.annotation.RestController;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 |
6 | @RestController
7 | public class HelloController {
8 |
9 | @RequestMapping("/")
10 | public String index() {
11 | return "Greetings from Spring Boot!";
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/docker-normal-build-demo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/docker-normal-build-demo/src/test/java/com/example/demo/DemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class DemoApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/docker-package-only-build-demo/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**
5 | !**/src/test/**
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 |
30 | ### VS Code ###
31 | .vscode/
32 |
--------------------------------------------------------------------------------
/docker-package-only-build-demo/Dockerfile:
--------------------------------------------------------------------------------
1 | # we will use openjdk 8 with alpine as it is a very small linux distro
2 | FROM openjdk:8-jre-alpine3.9
3 |
4 | # copy the packaged jar file into our docker image
5 | COPY target/demo-0.0.1-SNAPSHOT.jar /demo.jar
6 |
7 | # execute the jar
8 | CMD ["java", "-jar", "/demo.jar"]
--------------------------------------------------------------------------------
/docker-package-only-build-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | org.springframework.boot
7 | spring-boot-starter-parent
8 | 2.2.4.RELEASE
9 |
10 |
11 | com.example
12 | demo
13 | 0.0.1-SNAPSHOT
14 | demo
15 | Demo project for Spring Boot
16 |
17 |
18 | 1.8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 |
28 | org.springframework.boot
29 | spring-boot-starter-test
30 | test
31 |
32 |
33 | org.junit.vintage
34 | junit-vintage-engine
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 | org.springframework.boot
44 | spring-boot-maven-plugin
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/docker-package-only-build-demo/src/main/java/com/example/demo/DemoApplication.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class DemoApplication {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(DemoApplication.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/docker-package-only-build-demo/src/main/java/com/example/demo/HelloController.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.springframework.web.bind.annotation.RestController;
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 |
6 | @RestController
7 | public class HelloController {
8 |
9 | @RequestMapping("/")
10 | public String index() {
11 | return "Greetings from Spring Boot!";
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/docker-package-only-build-demo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/docker-package-only-build-demo/src/test/java/com/example/demo/DemoApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.example.demo;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class DemoApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------