These days I'm touching Java or Spring. By the way, I decided to run it with Docker, so I will set it.
You can use Docker. The setting method is omitted.
Create a crisp template with Spring Initializr. I specified the following. I think anything is fine if it is not written.
item | Contents |
---|---|
Project | Gradle project |
Language | Java |
Spring boot | 2.2.0(SNAPSHOT) |
packaging | jar |
Java version | 11 |
Dependencies | Lombok,Web |
It works as it is, but it only works, so I'll make some minor modifications. The content simply allows you to return a response. First, create ʻIndexController.java` in the same row as the automatically generated class. The contents of the created file are as follows.
$ touch src/main/java/...(package)/IndexContoroller.java
package hoge;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@GetMapping("index")
public String getIndexPage() {
return "Hello World!";
}
}
I will write Dockerfile in various ways. It feels like I looked it up. Everyone built it locally and then ran it with Docker. I want to do everything with Docker, so copy the necessary files and build & execute. I think it's better not to hit COPY repeatedly like this, but I don't know how to deal with it, so I'll do it once.
Dockerfile
FROM openjdk:latest
RUN mkdir /app
WORKDIR /app
COPY ./gradlew /app
COPY ./build.gradle /app
COPY ./settings.gradle /app
COPY ./src /app/src
COPY ./gradle /app/gradle
ENTRYPOINT ["sh", "./gradlew", "bootRun"]
I'm ready, so I'll try it. It's OK if you hit from the local and get a proper response.
$ docker build -t spring-docker ./
#Spring will start on port8080, so set it to 3000 locally
$ docker run -p 3000:8080 spring-docker
If it starts without any problem, check it with curl. It is OK if it becomes as follows.
$ curl localhost:3000/index
Hello World!
This time I set Docker + Spring boot. It was easier than I expected, and I want to touch Java for the first time in a while, so I want to make something a little more. As I wrote above, I wonder if it will be built on Docker unexpectedly. .. .. That's all for today.
Recommended Posts