Show List
Sample Docker File
Docker file is used to create a docker image and then to deploy the application to the container. Docker image is a collection of files libraries and configuration files that build up the environment.
- Docker file is created with set of instructions to build image
- "docker build" command creates the image
- "docker run" command is used to start the container using the docker image.
Sample Docker File
Here is a sample docker file to create docker image for a Java Spring Boot application that we created in the Earlier Chapter .
Dockerfile :
FROM adoptopenjdk/openjdk15:ubi
COPY target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
The docker file is created in the root of the folder. Jar file for the project is present in the target folder.
Here are the commands provided in the docker file:
- FROM : This provides the base image for the application. In this case as it is a Java application, JDK will provide the environment to run the application.
- COPY : This is to copy the file from local copy to the image. Here we are copying the Jar file that will be run.
- EXPOSE : Provides the information about the port used by the service. This is not required but is helpful and tools and users can use this to understand the service.
- CMD : Sets the command to run when user starts a container based on this image.
Creating Docker Image
1) Start the Docker desktop application.
2) Navigate to the root folder from command prompt and run command "docker build -t docker-rest-image ." This would create the image and tag with name "docker-rest-image"
C:\Users\mail2\Downloads\Spring-Boot-Rest-Service>docker build -t docker-rest-image . [+] Building 2.2s (7/7) FINISHED => [internal] load build definition from Dockerfile 0.1s => => transferring dockerfile: 32B 0.0s => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/adoptopenjdk/openjdk15:ubi 0.7s => [internal] load build context 0.5s => => transferring context: 38.97MB 0.5s => CACHED [1/2] FROM docker.io/adoptopenjdk/openjdk15:ubi@sha256:c860508ce2f180bb58dcb225b0ec967b3730e2f21fbc935 0.0s => [2/2] COPY target/*.jar app.jar 0.6s => exporting to image 0.3s => => exporting layers 0.2s => => writing image sha256:c5dbff4c3cc11af1400aafbfde86b6048b4dcce9017c1a1ed67cf57a5e143177 0.0s => => naming to docker.io/library/docker-rest-image 0.0s Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Running Docker Image
To run the image created above, run command "docker run -p 8080:8080 docker-rest-image". This would start the container and and forward the port 8080 from container onto the host OS so you can hit the services using http://localhost:8080.
C:\Users\mail2\Downloads\Spring-Boot-Rest-Service>docker run -p 8080:8080 docker-rest-image . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.3) 2022-10-30 15:03:04.427 INFO 1 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 15.0.2 on 708b9a311bd4 with PID 1 (/app.jar started by root in /) 2022-10-30 15:03:04.432 INFO 1 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default" 2022-10-30 15:03:05.517 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2022-10-30 15:03:05.603 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 70 ms. Found 1 JPA repository interfaces. 2022-10-30 15:03:06.702 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-10-30 15:03:06.725 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-10-30 15:03:06.726 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65] 2022-10-30 15:03:06.928 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-10-30 15:03:06.928 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2378 ms 2022-10-30 15:03:07.281 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2022-10-30 15:03:07.622 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. 2022-10-30 15:03:07.701 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default] 2022-10-30 15:03:07.781 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.10.Final 2022-10-30 15:03:08.074 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final} 2022-10-30 15:03:08.303 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect Hibernate: drop table if exists student CASCADE Hibernate: create table student (student_id integer generated by default as identity, grade varchar(255), name varchar(255), primary key (student_id)) 2022-10-30 15:03:09.119 INFO 1 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2022-10-30 15:03:09.133 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2022-10-30 15:03:09.613 WARN 1 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : 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 2022-10-30 15:03:10.210 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-10-30 15:03:10.228 INFO 1 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 6.544 seconds (JVM running for 7.28) 2022-10-30 15:03:38.645 INFO 1 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-10-30 15:03:38.645 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-10-30 15:03:38.648 INFO 1 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms Hibernate: select student0_.student_id as student_1_0_, student0_.grade as grade2_0_, student0_.name as name3_0_ from student student0_ Hibernate: insert into student (student_id, grade, name) values (default, ?, ?) Hibernate: select student0_.student_id as student_1_0_, student0_.grade as grade2_0_, student0_.name as name3_0_ from student student0_ 2022-10-30 15:46:03.257 INFO 1 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2022-10-30 15:46:03.283 INFO 1 --- [ionShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down' Hibernate: drop table if exists student CASCADE 2022-10-30 15:46:03.300 INFO 1 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2022-10-30 15:46:03.308 INFO 1 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
The information can also be viewed from the Docker Desktop
Testing the Application
The application can be tested from Postman
Leave a Comment