I wrote it for people who can make Spring apps using a techie IDE, but don't know Docker or jar.
For the time being, I wanted to get used to the Docker + Spring environment locally, so a memorandum of learning.
.
├── HELP.md
├── build
├── build.gradle //Build settings
├── gradle
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src //Source written with great care
I generated it from Spring initializer, but it should be like the above. ..
fin. (Runs on Docker container !!)
//↑ Omitted, the dependencies are smooth ...//
//Additional ↓//
def springBootApplicationName = 'test-build' //Variable that defines the name of the jar file to be generated (it does not have to be variable separately)
def applicationVersion = project.properties['release.version'] //Variable that defines the version (it does not have to be variable separately)
bootJar {
baseName = springBootApplicationName //Specify the name of the jar file to generate
version = applicationVersion //Specify version (I haven't checked the details, but it doesn't have to be)
}
$ gradle build
./build/libs
$ tree ./build/libs
./build/libs
└── test-build.jar
Dockerfile
in the top directory of your app. (No extension required).
├── Dockerfile // New!!
├── HELP.md
├── build
├── build.gradle
├── docker-compose.yaml
├── gradle
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
#Specify the image used by Docker Hub. Specify the JDK to run Java 11
#(Although there is an official openJDK, I created it on the assumption that it will be deployed on AWS, so I somehow specify the one provided by AWS here)
FROM amazoncorretto:11
#Variable definition that stores the directory path where the previously generated jar is located (here, specify the value from the command)
ARG JAR_FILE
#Copy the jar file into the container.(app.jar specifies the location in the container (any location))
COPY ${JAR_FILE} app.jar
#Define the command to be executed when this container is executed (here, the copied jar is executed)
ENTRYPOINT ["java","-jar","/app.jar","--spring.profiles.active=docker"]
Docker
if not installeddocker build --build-arg JAR_FILE=build/libs/exercise-manage-backend.jar -t springio/gs-spring-boot-docker .
Specify the value of the variable defined on the Dockerfile.
--build-arg JAR_FILE=build/libs/exercise-manage-backend.jar
The name given to the generated image (optional, I will quote the one of the article referred to here as it is)
-t springio/gs-spring-boot-docker
Note: Options such as -p must be defined just to the right of run
to be set. (I was crazy here because there was no reference article ... Is it common sense in the command area ...?)
docker run -p 8080:8080 --name spring-test --rm springio/gs-spring-boot-docker
Map the port on the container (left) and the port on the execution host (right). If it is not specified in application.properties, it should be started on 8080, so associate it with the local 8080 port.
-p 8080:8080
→ You can connect to the app on the container! !!
host.docker.internal: (port number)
(it will not connect if localhost: (port number)
remains)jdbc: mysql: //host.docker.internal: 3306 / xxx
If you look at this, you can run Spring with Docker for the time being ...! I wrote it aiming at an article that can be in such a state. Detailed explanations such as the concept of Docker and the details of commands have been written by various experts, so I will omit them here.
Docker is still fresh to the touch, so I would appreciate it if you could point out any misunderstandings.
Recommended Posts