Skip to content

Commit 4c2e114

Browse files
committed
readme file finished
1 parent 1279429 commit 4c2e114

File tree

10 files changed

+274
-11
lines changed

10 files changed

+274
-11
lines changed

210ed5d1dc3f

Whitespace-only changes.

5a58ac387fe7

Whitespace-only changes.

7e72a7dcf7dc

Whitespace-only changes.

README.md

Lines changed: 274 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ TOC
1212
- [1 Dockerfile](#1-dockerfile)
1313
- [2 Building The Image](#2-building-the-image)
1414
- [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)
1518

1619
0 Spring Boot Web Service
1720
-------------------------
@@ -22,7 +25,8 @@ straight-forward as it is obvious: [HelloController](https://github.com/bzdgn/do
2225
The key thing using a standalone Spring Boot application is to create a **Fat Jar**, also known as
2326
**Uber Jar**. In order to create the **Uber Jar**, there are two things to be done;
2427

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);
2630

2731
```
2832
<properties>
@@ -41,55 +45,314 @@ In our example, you will see the properties as below in the [pom.xml](https://gi
4145
</properties>
4246
```
4347

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+
4476
[Go back to TOC](#toc)
4577

4678

4779
1 Dockerfile
4880
------------
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;
114+
115+
```
116+
CMD ["java", "-jar", "docker-java-app-example.jar"]
117+
```
118+
119+
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.
49122

50123
[Go back to TOC](#toc)
51124

52125

53126
2 Building The Image
54127
--------------------
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;
144+
145+
```
146+
C:\00_ANA\JavaEE\WS\docker-java-app-example>docker build --tag=docker-java-hello-world-app .
147+
Sending build context to Docker daemon 16.85MB
148+
Step 1/6 : FROM openjdk:8-jre-alpine
149+
---> 7e72a7dcf7dc
150+
Step 2/6 : RUN apk update && apk add bash
151+
---> Running in 66ebd9812836
152+
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
153+
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
154+
v3.8.2-21-g54952ee375 [http://dl-cdn.alpinelinux.org/alpine/v3.8/main]
155+
v3.8.2-21-g54952ee375 [http://dl-cdn.alpinelinux.org/alpine/v3.8/community]
156+
OK: 9546 distinct packages available
157+
(1/5) Installing ncurses-terminfo-base (6.1_p20180818-r1)
158+
(2/5) Installing ncurses-terminfo (6.1_p20180818-r1)
159+
(3/5) Installing ncurses-libs (6.1_p20180818-r1)
160+
(4/5) Installing readline (7.0.003-r0)
161+
(5/5) Installing bash (4.4.19-r1)
162+
Executing bash-4.4.19-r1.post-install
163+
Executing busybox-1.28.4-r2.trigger
164+
OK: 91 MiB in 57 packages
165+
Removing intermediate container 66ebd9812836
166+
---> fa54653163af
167+
Step 3/6 : WORKDIR /app
168+
---> Running in c94cbad8627a
169+
Removing intermediate container c94cbad8627a
170+
---> 5a58ac387fe7
171+
Step 4/6 : COPY /target/docker-java-app-example.jar /app
172+
---> aa5930ee4a75
173+
Step 5/6 : EXPOSE 8080
174+
---> Running in 851b519d4514
175+
Removing intermediate container 851b519d4514
176+
---> 210ed5d1dc3f
177+
Step 6/6 : CMD ["java", "-jar", "docker-java-app-example.jar"]
178+
---> Running in 55b84275e095
179+
Removing intermediate container 55b84275e095
180+
---> a0c355a25236
181+
Successfully built a0c355a25236
182+
Successfully tagged docker-java-hello-world-app:latest
183+
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+
```
55194

56195

57196
[Go back to TOC](#toc)
58197

59198

60199
3 Running And Testing The Docker Container
61200
------------------------------------------
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;
222+
223+
<p align="center">
224+
<img src="https://github.com/bzdgn/docker-spring-boot-java-web-service-example/blob/master/screenshots/00_docker_test.PNG" alt="docker-web-test">
225+
</p>
226+
227+
[Go back to TOC](#toc)
228+
229+
230+
4 Getting Inside The Docker Container
231+
-------------------------------------
232+
While our container is running, we can get inside the running container for different purposes like checking out the logging, configuration
233+
and other stuff. Before getting inside the container, we need to know the **container id** of our container. To do so, let's first list
234+
the running containers with the following command;
235+
236+
```
237+
docker container ls
238+
```
239+
240+
A successful output example is as below;
241+
242+
```
243+
C:\00_ANA\JavaEE\WS\docker-java-app-example>docker container ls
244+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
245+
159c3ee8ef39 docker-java-hello-world-app "java -jar docker-ja…" 7 minutes ago Up 7 minutes 0.0.0.0:80->8080/tcp adoring_jackson
246+
```
247+
248+
You can see the container id on the left-most part of the output. For this example, the container id is "159c3ee8ef39". Each container will
249+
have a different hash, so when you run it, it will be different. We are going to use this container id to get into the docker container as below;
250+
251+
```
252+
docker exec -it <container_id> /bin/bash
253+
```
254+
255+
The "/bin/bash" at the end denotes that we are going to use **bash** shell, which our image has.
256+
257+
A successful output is as below;
258+
259+
```
260+
C:\00_ANA\JavaEE\WS\docker-java-app-example>docker exec -it 159c3ee8ef39 /bin/bash
261+
bash-4.4# pwd
262+
/app
263+
bash-4.4# ls
264+
docker-java-app-example.jar
265+
bash-4.4# ps -ef
266+
PID USER TIME COMMAND
267+
1 root 0:08 java -jar docker-java-app-example.jar
268+
45 root 0:00 /bin/bash
269+
53 root 0:00 ps -ef
270+
bash-4.4# exit
271+
exit
272+
273+
C:\00_ANA\JavaEE\WS\docker-java-app-example>
274+
```
275+
276+
As you can see, the commands "pwd", "ls" and "ps -ef" successfully ran. We can see that our jar file stand in the "app" folder
277+
as we have configured in our Dockerfile. We can easily exit with the "exit" command.
278+
279+
[Go back to TOC](#toc)
280+
281+
282+
5 Stopping The Docker Container
283+
-------------------------------
284+
To stop the docker container, we are going to use this command;
285+
286+
```
287+
docker stop <container_id>
288+
```
289+
290+
It is as simple as that. Remember that when we ran the docker container, we have the Spring Boot output. I'm using windows while
291+
writing this repository and if you hit ctrl+c, even if you are out of the scope of the logging output of Spring Boot, the container
292+
is still alive. To stop it, you have to use the command above. A successful example is as below;
62293

294+
```
295+
C:\00_ANA\JavaEE\WS\docker-java-app-example>docker container ls
296+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
297+
159c3ee8ef39 docker-java-hello-world-app "java -jar docker-ja…" 16 minutes ago Up 16 minutes 0.0.0.0:80->8080/tcp adoring_jackson
298+
299+
C:\00_ANA\JavaEE\WS\docker-java-app-example>docker container stop 159c3ee8ef39
300+
159c3ee8ef39
301+
302+
C:\00_ANA\JavaEE\WS\docker-java-app-example>docker container ls
303+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
304+
305+
C:\00_ANA\JavaEE\WS\docker-java-app-example>
306+
```
63307

64308
[Go back to TOC](#toc)
65309

66310

311+
6 Cheat Sheet
312+
-------------
67313

68314

69-
1- Maven install to create the fat jar
315+
1. Maven install to create the fat jar
70316

71-
mvn clean install
317+
```
318+
mvn clean install
319+
```
72320

73-
2- Docker build
321+
2. Docker build
74322

75-
docker build --tag=docker-java-hello-world-app .
323+
```
324+
docker build --tag=docker-java-hello-world-app .
325+
```
76326

77-
3- Docker run
327+
3. Docker run
78328

329+
```
79330
docker run -p 80:8080 docker-java-hello-world-app
331+
```
80332

81-
4- Test the app
333+
4. Test the app
82334

335+
```
83336
http://localhost/docker-java-app/test
337+
```
84338

85-
5- Get the container id
339+
5. Get the container id
86340

341+
```
87342
docker container ls
343+
```
88344

89-
6- Get into the app
345+
6. Get into the app
90346

347+
```
91348
docker exec -it <container_id> /bin/bash
349+
```
350+
351+
7. To stop the container
352+
353+
```
354+
docker container stop <container_id>
355+
```
92356

93-
7- To stop the container
94357

95-
docker container stop <container_id>
358+
[Go back to TOC](#toc)

Running

Whitespace-only changes.

a0c355a25236

Whitespace-only changes.

aa5930ee4a75

Whitespace-only changes.

docker

Whitespace-only changes.

fa54653163af

Whitespace-only changes.

screenshots/00_docker_test.PNG

13.6 KB
Loading

0 commit comments

Comments
 (0)