8.4.1. Initial Setup of Web Project for Container¶
The following is procedures of initial setup of the web project for container:
- Generating web project for container
- Confirm communications of web project
- Create a container image
- Run Container Image
8.4.1.1. Overview of the generated project¶
The overview of the project generated by this procedure is as follows.
Item | Description |
---|---|
Project Type | Maven project |
Project composition | Single project composition |
DB used | H2 Database Engine (embedded in the application) |
Built-in adapter | Routing adapter (for details, see Routing Adapter) |
What is included in the generated project? | The following is included in the generated project:
|
For relationship with other projects and directories, see Maven Archetype Configuration.
8.4.1.2. Create blank project¶
Generate a blank project using the archetypes provided by Nablarch.
8.4.1.2.1. Execute the mvn command¶
Use Maven Archetype Plugin(external site) to generate a blank project.
Change the current directory to the directory where the blank project (can be any directory) is to be created, and place the following file.
After placing the file, specify the necessary parameters in the arguments and execute the bat file.
generateContainerWebProject.bat 5u24 <<groupId>> <<artifactId>> <<version>> <<package(optional)>>
The parameters to be set in the above command are as follows. If you want to change the version of nablarch, change 5u24 .
Input item | Description | Configuration example |
---|---|---|
groupId | Group ID (normally, enter the package name) | com.example |
artifactId | Artifact ID | myapp-container-web |
version | Version number | 0.1.0 |
package | Package (normally the same as group ID) | com.example |
Important
Item groupId and package are mapped to the Java package name. Use lowercase letters, numbers, and dots for these input values, and do not use hyphens.
If the command ends normally, a blank project is created under the current directory.
8.4.1.3. Communication confirmation¶
The communication confirmation mechanism and procedures are the same as for a normal web project. Thus, see Initial Setup of Web Project.
Note
The artifact ID should be replaced with myapp-container-web
to specify the directory and command.
8.4.1.4. Create a container image¶
The blank project has a plugin named Jib (External sites) built in to create an image of a Docker container.
The jib: dockerBuild
goal of this plugin can be executed to create a container image.
cd myapp-container-web
mvn package jib:dockerBuild
If the execution is successful, the log given below will be output to the console.
(omission)
[INFO] Built image to Docker daemon as myapp-container-web, myapp-container-web, myapp-container-web:0.1.0
(omission)
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
(Omitted)
Built Docker images are stored in a local repository. Can see the images stored in the local repository with the following command.
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp-container-web 0.1.0 dd60cbdc7722 50 years ago 449MB
myapp-container-web latest dd60cbdc7722 50 years ago 449MB
Can see that there are 2 images registered: myapp-container-web:0.1.0
and myapp-container-web:latest
.
As you can see, the blank project is configured to create the following two images by executing jib:dockerBuild
.
${project.artifactId}:latest
${project.artifactId}:${project.version}
Tomcat image (External sites) is used as base image by default.
The base image can be changed with the jib.from.image
property.
For example, if you want to use tomcat:9.0.31-jdk11-adoptopenjdk-hotspot
for your base image, you would write it in pom.xml
.
<project>
<! -- Omitted ...-->
<properties>
<! -- Omitted ...-->
<jib.from.image>tomcat:9.0.36-jdk11-adoptopenjdk-hotspot</jib.from.image>
<! -- Omitted ...-->
</properties>
<! -- Omitted ...-->
</project>
Tip
In the blank project, the base image is specified with a Docker image tag. In this case, the latest version of the specified image will be selected. If a different version is selected than at the time of verification, it may affect the operation of the application. Therefore, it is recommended to specify the base image as a digest in order to specify exactly which version, after the test is completed.
An example of setting by digest is shown below.
<jib.from.image>tomcat@sha256:7d59567f61e79f5dc1226a3ee26b4a4c2befc5cae182f7e0823199cf5885409b</jib.from.image>
8.4.1.5. Run a container image¶
Once you have created a container image, you can run it with the following command.
cd myapp-container-web
docker run -d -p 8080:8080 -v %CD%\h2:/usr/local/tomcat/h2 --name myapp-container-web myapp-container-web
Once the container is up and running, you can check the application in your web browser by accessing http://localhost:8080/
.
Tip
The above command is an example of the case where SAMPLE.h2.db, which is included in the blank project beforehand, is used as the database.
If you do not use SAMPLE.h2.db, you do not need to specify a volume (-v
).
Tip
Running Docker assumes that you are using Docker Desktop (see Prerequisite). If you are using the Docker Toolbox, the volume specification in the above example will fail.
If you are using the Docker Toolbox, Docker is running in a VM on VirtualBox. Therefore, the path that can be specified on the host side of the volume is the path on the VM.
On Windows, by default C:\Users
is mounted in /c/users
on the VM.
Thus, if you are using the Docker Toolbox, you must specify the volume as -v/c/users/path/to/project/h2:/usr/local/tomcat/h2
.
To exit the container, execute the following command.
docker stop myapp-container-web
To delete a container, execute the following command.
docker rm myapp-container-web
8.4.1.6. Supplementary notes¶
For information on the method of confirming the data of H2 and tools included in the blank project, see Initial Setup Procedure Supplementary Information.