You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[3 Running And Testing The Docker Container](#3-running-and-testing-the-docker-container)
15
+
-[4 Getting Inside The Docker Container](#4-getting-inside-the-docker-container)
16
+
-[5 Stopping The Docker Container](#5-stopping-the-docker-container)
17
+
-[6 Cheat Sheet](#6-cheat-sheet)
15
18
16
19
0 Spring Boot Web Service
17
20
-------------------------
@@ -22,7 +25,8 @@ straight-forward as it is obvious: [HelloController](https://github.com/bzdgn/do
22
25
The key thing using a standalone Spring Boot application is to create a **Fat Jar**, also known as
23
26
**Uber Jar**. In order to create the **Uber Jar**, there are two things to be done;
24
27
25
-
1. Add an **start-class** to point to the class acting as the Entry Point as below;
28
+
1. Add an **start-class** to point to the class acting as the Entry Point as the example below.
29
+
The reason we are doing so is, we need to reference the **.jar** file in the [Dockerfile](https://github.com/bzdgn/docker-spring-boot-java-web-service-example/blob/master/Dockerfile);
26
30
27
31
```
28
32
<properties>
@@ -41,55 +45,314 @@ In our example, you will see the properties as below in the [pom.xml](https://gi
41
45
</properties>
42
46
```
43
47
48
+
2. The second thing is, we need to know the name of our **Fat Jar** (a.k.a. **Uber Jar**). In order
49
+
to do that, we use define the **final name** under **build** section of our [pom.xml](https://github.com/bzdgn/docker-spring-boot-java-web-service-example/blob/master/pom.xml) file as below;
50
+
51
+
```
52
+
<build>
53
+
<finalName>docker-java-app-example</finalName>
54
+
...
55
+
...
56
+
</build>
57
+
```
58
+
59
+
In our example, the build section is as below;
60
+
61
+
```
62
+
<build>
63
+
<finalName>docker-java-app-example</finalName>
64
+
<plugins>
65
+
<plugin>
66
+
<groupId>org.springframework.boot</groupId>
67
+
<artifactId>spring-boot-maven-plugin</artifactId>
68
+
</plugin>
69
+
</plugins>
70
+
</build>
71
+
```
72
+
73
+
As we create our **Fat Jar** (a.k.a. **Uber Jar**) and we define the final name of our jar,
74
+
we are all set and we can write the [Dockerfile](https://github.com/bzdgn/docker-spring-boot-java-web-service-example/blob/master/Dockerfile).
75
+
44
76
[Go back to TOC](#toc)
45
77
46
78
47
79
1 Dockerfile
48
80
------------
81
+
With the **Dockerfile** we can define, configure and initialize our image and container. In **Dockerfile** we can define;
82
+
83
+
1. Which image we are going to use. We need a docker image that has the OpenJDK and thus we are going to use Alpine;
84
+
85
+
```
86
+
FROM openjdk:8-jre-alpine
87
+
```
88
+
89
+
2. Which shell we are going to have. I prefer **Bash** which I find easy to use;
90
+
91
+
```
92
+
RUN apk update && apk add bash
93
+
```
94
+
95
+
3. The working directory our app is going to run. The name of the working directory will be **app**;
96
+
97
+
```
98
+
WORKDIR /app
99
+
```
100
+
101
+
4. The files we need to copy into our image. We will need only the **Uber Jar** file so let's copy it into the working dir;
102
+
103
+
```
104
+
COPY /target/docker-java-app-example.jar /app
105
+
```
106
+
107
+
5. The port number(s) that we need to expose to reach out from the container. Spring Boot default port is 8080;
108
+
109
+
```
110
+
EXPOSE 8080
111
+
```
112
+
113
+
6. The commands that we need to run as the container goes live. And we need to add simply "java -jar <jar_file>.jar" format;
Notify that the CMD(command) parameters are separated with comma.
120
+
121
+
Now our [Dockerfile](https://github.com/bzdgn/docker-spring-boot-java-web-service-example/blob/master/Dockerfile) is all set, we can directly build the image.
49
122
50
123
[Go back to TOC](#toc)
51
124
52
125
53
126
2 Building The Image
54
127
--------------------
128
+
Before building the image, we need to create the **Uber Jar** (aka **Fat Jar**) file, to do so, just clean install with maven as below;
129
+
130
+
```
131
+
mvn clean install
132
+
```
133
+
134
+
Now our **Uber Jar** file is created under the target folder with the format: "target/<final_name>.jar" (or .war based on package in .pom file)
135
+
136
+
To build the image, we will use **docker build** command and tag it. The last parameter will be the directory, by using dot ("."),
137
+
we point to the current directory. So you must run this command on the top level directory of this repository.
138
+
139
+
```
140
+
docker build --tag=docker-java-hello-world-app .
141
+
```
142
+
143
+
As you run the command which is written above, a successful example output will be as below;
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
184
+
```
185
+
186
+
After the image is built, you can check it with the ```docker image ls``` command. An example is as below;
187
+
188
+
```
189
+
C:\00_ANA\JavaEE\WS\docker-java-app-example>docker image ls
190
+
REPOSITORY TAG IMAGE ID CREATED SIZE
191
+
docker-java-hello-world-app latest a0c355a25236 About a minute ago 105MB
192
+
openjdk 8-jre-alpine 7e72a7dcf7dc 5 days ago 83.1MB
193
+
```
55
194
56
195
57
196
[Go back to TOC](#toc)
58
197
59
198
60
199
3 Running And Testing The Docker Container
61
200
------------------------------------------
201
+
Now we have our docker image ready however, we need to run a container based on our image. We can simply run our docker container
202
+
as below;
203
+
204
+
```
205
+
docker run -p 80:8080 docker-java-hello-world-app
206
+
```
207
+
208
+
The format is simple;
209
+
210
+
```
211
+
docker run -p <external-port>:<internal-port> <image-name>
212
+
```
213
+
214
+
The parameter **p** stands for the **port**. **External port** is the port that will be available outside of the docker container.
215
+
The **internal port** is the port that will be available inside the docker container, which is **8080** because the default port of
216
+
Spring Boot is set to **8080**. So outside of the Docker Container, we will use the port number **80**, and it is mapped to **8080**
217
+
and will reach to the Spring Boot application.
218
+
219
+
As you run the command, you can open up your browser, you can go to this url [http://localhost:80/docker-java-app/test](http://localhost/docker-java-app/test)
220
+
221
+
Then you will see the container is returning the string message with date as below;
0 commit comments