Akiko: Handsome people are concerned
Making: making
Hobby: Americano More Ice!
Project source: project Github source code! Star to walk again
directory
- directory
- preface
- What is a Docker image
- Dockerfile Quick lookup table of common commands
- Maven builds Docker images – three steps
- 1. Preparation
- 2. Perform the build
- 3. Check the results
- extension
preface
Most Docker applications provide some infrastructure, such as Redis/Mongo/Mysql, etc. This paper mainly describes how to package the Springboot application written by myself into Docker images and distribute them
What is a Docker image
Grasp the core four points
- It is a static read-only template
- It contains the build
Docker
Container instructions - It’s layered
- It does this by
Dockerfile
To create the image
Dockerfile Quick lookup table of common commands
The command | role | example |
---|---|---|
FROM | Based on what mirror | FROM [:] [AS ] |
LABEL | Set up the label | LABEL maintainer=”Geektime” |
RUN | Run the setup command | RUN [“executable”, “param1”, “param2”] |
CMD | Container startup command (only one) | CMD [“executable”, “param1”, “param2”] |
ENTRYPOINT | Container start command (only one) | ENTRYPOINT [“executable”, “param1”, “param2”] |
VOLUME | The mount command | VOLUME [“/data”] |
EXPOSE | Container listening port | EXPOSE [/] |
ENV | Setting environment Variables | ENV |
ADD | Add files | ADD [–chown=:] … |
WORKDIR | Set the working directory to run | WORKDIR /path/to/workdir |
USER | Setting a Running User | USER [:] |
Maven builds Docker images – three steps
1. Preparation
- Look at
Dockerfile
file
FROM java:8
EXPOSE 8080
ARG JAR_FILE
ADD target/${JAR_FILE} /waiter-service.jar
ENTRYPOINT ["java"."-jar"."/waiter-service.jar"]
Copy the code
- Look at the
pox.xml
The core part, configurationdockerfile-maven-plugin
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
Copy the code
2. Perform the build
- through
maven
Command to package the build image
$ mvn clean package -Dmaven.test.skip=true
[INFO] Scanning forprojects... [INFO] [INFO] -------------< geektime.spring.springbucks:waiter-service >------------- [INFO] Building waiter-service 0.0.1 - the SNAPSHOT [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (jar) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --... . . [INFO] Step 2/5 : EXPOSE 8080 [INFO] [INFO] ---> Runningin 6ae617f0acdf
[INFO] Removing intermediate container 6ae617f0acdf
[INFO] ---> 6b182f57247c
[INFO] Step 3/5 : ARG JAR_FILE
[INFO]
[INFO] ---> Running in 9a25b1d1b38c
[INFO] Removing intermediate container 9a25b1d1b38c
[INFO] ---> 863117cd06f4
[INFO] Step 4/5 : ADD target/${JAR_FILE} /waiter-service.jar
[INFO]
[INFO] ---> 064b021fac28
[INFO] Step 5/5 : ENTRYPOINT ["java"."-jar"."/waiter-service.jar"]
[INFO]
[INFO] ---> Running inc8b3c173ebad [INFO] Removing intermediate container c8b3c173ebad [INFO] ---> 85fd7a4f7f3d [INFO] Successfully built 85fd7a4f7f3D [INFO] Successfully tagged Springbucks /waiter-service:0.0.1-SNAPSHOT [INFO] [INFO] Detected build of image with id 85fd7a4f7f3d [INFO] Building jar: / Users/lidean/gitee/geektime - spring - family/Chapter 10 / docker - demo/target/waiter - service - 0.0.1 - the SNAPSHOT - docker - info. The jar [INFO] Successfully built Springbucks /waiter-service:0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 07:18 min [INFO] Finished at: 2021-06-07T10:35:33+08:00 [INFO] ------------------------------------------------------------------------Copy the code
You can see that the build is complete
3. Check the results
Let’s see if the packaged image is successfully deployed
$Docker Images REPOSITORY TAG IMAGE ID CREATED SIZE Springbucks /waiter- Service 0.0.1-SNAPSHOT 85fd7a4f7f3D About a minute ago 683MBCopy the code
Use Docker run to execute the image, and then use Docker logs to see that it is still started
$docker run --name wax-service -d -p 8080:8080 Springbucks/wax-service :0.0.1-SNAPSHOT 28eba6685144634016d7e73c22000265aa14713276242505684e7eb62241954e $ docker logs waiter-service . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.3. RELEASE) the 2021-06-07 06:36:36. 084 INFO 1 - [the main] G.S.S.W aiter. WaiterServiceApplication: Starting WaiterServiceApplication v0.0.1-SNAPSHOT on 28eba6685144 with PID 1 (/waiter-service.jar started by root in /) The 2021-06-07 06:36:36. 101 INFO 1 - [the main] G.S.S.W aiter. WaiterServiceApplication: No active profile set, falling back to default profiles: Default 06:36:38 2021-06-07. 937 INFO 1 - [the main] S.D.R.C.R epositoryConfigurationDelegate: Bootstrapping Spring Data in DEFAULT Mode. 2021-06-07 06:36:39.323 INFO 1 -- [main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data Repository scanning in 344ms. Found 2 Repository interfaces. 2021-06-07 06:36:41.811 INFO 1 -- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$f2e71ce] is not eligible for getting processed by all BeanPostProcessors (for example: Not eligible for auto - proxying) 2021-06-07 06:36:43. 553 INFO 1 - [the main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: Tomcat initialized with port(s): 8080 (HTTP) 2021-06-07 06:36:43. 683 INFO 1 - [the main] o.a pache, catalina. Core. StandardService: Starting the service [Tomcat] 2021-06-07 06:36:43. 684 INFO 1 - [the main] org. Apache. Catalina. Core. StandardEngine: Starting Servlet engine: Apache Tomcat / 9.0.16 06:36:43 2021-06-07. 727 1 - [the main] INFO O.A.C atalina. Core. AprLifecycleListener: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni : / lib/usr/lib] 2021-06-07 06:36:44. 205 INFO 1 - [the main] O.A.C.C.C. [Tomcat] [localhost]. / / : Initializing Spring Embedded WebApplicationContext 2021-06-07 06:36:44.206 INFO 1 -- [main] o.s.web.context.ContextLoader : Root WebApplicationContext: Initialization completed in 7871, the 2021-06-07 ms 06:36:44. 727 INFO 1 - [the main] com. Zaxxer. Hikari. HikariDataSource: HikariPool-1 - Starting... The 2021-06-07 06:36:45. 430 INFO 1 - [the main] com. Zaxxer. Hikari. HikariDataSource: HikariPool - 1 - Start completed. The 2021-06-07 06:36:46. 171 INFO 1 - [the main] o.h ibernate. Jpa. Internal. Util. LogHelper: HHH000204: Processing PersistenceUnitInfo [ name: default ...] 2021-06-07 06:36:46.770 INFO 1 -- [main] org.hibernate.Version: HHH000412: Hibernate Core {5.3.7. Final} 2021-06-07 06:36:46. 775 INFO 1 - [the main] org. Hibernate. CFG. Environment: HHH000206: Hibernate. The properties not found the 2021-06-07 06:36:47. 578 INFO 1 - [the main] o.hibernate.annotations.com mon. Version: HCANN000001: Hibernate Commons Annotations {5.0.4. Final} 2021-06-07 06:36:49. 212 INFO 1 - [the main] org. Hibernate. The dialect, the dialect: HHH000400: Using dialect: Org. Hibernate. The dialect. H2Dialect 06:36:52 2021-06-07. 227 INFO 1 - [the main] j.L ocalContainerEntityManagerFactoryBean: Initialized JPA EntityManagerFactory for persistence unit 'default'the 2021-06-07 06:36:55. 609 INFO 1 - [the main] O.S.S.C oncurrent. ThreadPoolTaskExecutor: Initializing the ExecutorService'applicationTaskExecutor2021-06-07 06:36:55.815 WARN 1 -- [main] aWebConfiguration$JpaWebMvcConfiguration: spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this Warning the 2021-06-07 06:36:56. 808 INFO 1 - [the main] O.S.B.A.E.W eb. EndpointLinksResolver: Exposing 15 endpoint(s) beneath base path '/actuator'the 2021-06-07 06:36:57. 082 INFO 1 - [the main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat started on the port (s) : 8080 (http) with context path ''the 2021-06-07 06:36:57. 089 INFO 1 - [the main] G.S.S.W aiter. WaiterServiceApplication: Started WaiterServiceApplication in 22.508 seconds (JVM running for 24.292)Copy the code
Check whether the command is successfully executed. You can see that it is successfully executed and mapped to the corresponding port
$docker ps | grep springbucks/waiter - service: 28 eba6685144 0.0.1 - the SNAPSHOT springbucks/waiter - service: 0.0.1 - the SNAPSHOT"Java - jar/waiter - s..."3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/ TCP waiter-serviceCopy the code
Finally, we visit the actual port to see if the corresponding data is returned
${curl http://127.0.0.1:8080/coffee/1"id" : 1,
"createTime" : "The 2021-06-07 T14:36:45. 551 + 0800"."updateTime" : "The 2021-06-07 T14:36:45. 551 + 0800"."name" : "espresso"."price" : 20.00
}%
Copy the code
extension
The Dockerfile-maven-plugin also provides some helper methods that can be deployed to the repository I specify.
Try locally building your own Docker image repository push