What you need ?
- Docker is installed
- A simple Spring Boot application
To start, we will go to the root folder of the project and create a new text file called Dockerfile. It’ll contain all steps necessary to create an image. For this tutorial, it looks like this:
FROM java:8-jdk-alpine COPY ./target/Projet_formation-0.0.1-SNAPSHOT.jar /usr/app/ WORKDIR /usr/app EXPOSE 8080 ENTRYPOINT ["java", "-jar", "Projet_formation-0.0.1-SNAPSHOT.jar"]
let’s explain every step :
- FROM java:8-jdk-alpine : with this line we tell Docker that our image will be based on another image that is available on Docker Hub. This image was contains all necessary dependencies that we would need to run any Java application (example JDK, etc…)
- COPY ./target/Projet_formation-0.0.1-SNAPSHOT.jar /usr/app/ : in this step we copy-paste something from one directory to another. First argument after COPY is a path of an application that we want to put into container. The second parameter, /usr/app/ , is a directory in a container where we put the app.
- WORKDIR /usr/app : here we instruct Docker to use /usr/app folder as a root, otherwise in each following command we would need to provide the full path to the resource.
- EXPOSE 8080 : with it we inform Docker that a container will listen to specific port, the same as our app is using.
- ENTRYPOINT [“java”, “-jar”, “Projet_formation-0.0.1-SNAPSHOT.jar“] : in the last command we tell Docker to run the application, where first value is a command and the last two are parameters.
Now we will go to the terminal, inside the folder where Dockerfile is located and when you’re in there build an image with following command (dont forget the dot . at the end of the command):
$ docker build -t javatuto-img . #1 [internal] load build definition from Dockerfile #1 sha256:da97f01452306b4900b72411888e17c3f03b3f2d9b449a132c99f35a46d2da7b #1 transferring dockerfile: 221B 0.0s done #1 DONE 0.1s #2 [internal] load .dockerignore #2 sha256:ae7fef85474565f2be17d66f8fa5d8f1aeedac9bcab36f07656494385500a6e5 #2 transferring context: 2B 0.0s done #2 DONE 0.1s #3 [internal] load metadata for docker.io/library/java:8-jdk-alpine #3 sha256:3601af91695cb4b6101b77a3460eaef7481c0448b32b05bf4220e07364391a1e #3 DONE 3.3s
Now, Docker is building the image by executing command step-by-step . to check if the image was built with the following command :
$ docker images
Or if you installed the Docker desktop on windows you can see your image :
The image is available, so let’s run it with the following command :
$ docker run -p 3333:8088 javatuto-img
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.1) 2021-02-01 11:22:12.344 INFO 1 --- [ main] com.marwen.ProjetFormationApplication : Starting ProjetFormationApplication v0.0.1-SNAPSHOT using Java 1.8.0_111-internal on c328af712706 with PID 1 (/usr/app/Projet_formation-0.0.1-SNAPSHOT.jar started by root in /usr/app) 2021-02-01 11:22:12.348 INFO 1 --- [ main] com.marwen.ProjetFormationApplication : No active profile set, falling back to default profiles: default 2021-02-01 11:22:13.958 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2021-02-01 11:22:13.981 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-02-01 11:22:13.982 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41] 2021-02-01 11:22:14.132 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-02-01 11:22:14.132 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1687 ms 2021-02-01 11:22:14.652 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-02-01 11:22:15.084 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2021-02-01 11:22:15.107 INFO 1 --- [ main] com.marwen.ProjetFormationApplication : Started ProjetFormationApplication in 3.347 seconds (JVM running for 3.977)
To test your container, navigate to http://localhost:8080