The guy who could only run the Spring app on the IDE tried running it on Docker

Background

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.

Premise

Execution environment

What to use (roughly)

App directory structure

.
├── 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. ..

procedure

Overview

  1. Generate a jar file from the created app.
  2. Prepare a Dockerfile.
  3. Start Docker.

fin. (Runs on Docker container !!)

Details

1. Generate a jar file from the created app.

Added settings to generate jar in build.gradle


//↑ 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)
}

Go to the app directory on the terminal and hit the following command

$ gradle build

Confirm that the jar file is generated under ./build/libs

$ tree ./build/libs
./build/libs
└── test-build.jar

2. Prepare a Dockerfile.

Create a file named 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

Describe the following in the Dockerfile

#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"]

3. Start Docker.

Install Docker if not installed

Go to the app directory on the terminal and hit the following command. (Generation of Docker image)

docker 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

After generating the image, hit the following command. (Launch Docker image)

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

I think that the Spring startup log that you are familiar with in the IDE will flow, so after confirming that it has started successfully, access localhost: 8080

→ You can connect to the app on the container! !!

Digression: How to connect to DB on localhost

Postscript

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.

reference

Recommended Posts

The guy who could only run the Spring app on the IDE tried running it on Docker
Run the AWS CLI on Docker
I tried running Docker on Windows Server 2019
I tried running Ansible on a Docker container
left4dead2 I made a Docker image for the server and tried running it on GCE # 3 (I had a hard time building the server)
Run the Android emulator on Docker using Android Emulator Container Scripts