1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Spring BootのjarをDocker上で動かすまで

Posted at

はじめに

Docker上でSpring Bootのjarを動かすための備忘録。公式にも書いてあるけど自分用に念のため検証した。

環境

  • Docker 20.10.8, build 3967b7d
  • Spring Boot 2.5.3
  • Openjdk 11

内容

アプリケーション側

検証用なので文字列返すだけ。 コマンドでもIDEでもいいからビルドしてjarを作っておく。

@Controller
@RequestMapping("/demo")
public class ContainerdemoController {

    @GetMapping
    @ResponseBody
    public String demo() {
        return "sample";
    }
}

Dockerfile

Openjdk11指定で JAR_FILE 相対パスのjarを app.jar としてコピー。
最後にENTRYPOINTで java -jar /app.jar を指定して実行。

FROM openjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

コンテナビルド&立ち上げ

下記コマンドで containerdemo のタグをつけてイメージビルド完了。

docker build -t containerdemo .
docker build -t containerdemo .
[+] Building 2.0s (7/7) FINISHED
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 144B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/openjdk:11                                                      1.9s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 89B                                                                                   0.0s
 => [1/2] FROM docker.io/library/openjdk:11@sha256:369854afc795005451787f24e455daa8f409ecf3b1f67c3c1395359adf1234  0.0s
 => CACHED [2/2] COPY target/*.jar app.jar                                                                         0.0s
 => exporting to image                                                                                             0.0s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:97ebdd9191f5a9aa77b8f6396986e4023b6119d59692f613691314d2aa0a1234                       0.0s
 => => naming to docker.io/library/containerdemo                                                                   0.0s

下記コマンドでホスト側のポート9999とコンテナのポート8080をポートフォワーディングして実行することで立ち上げまで完了。

docker run -p 9999:8080 containerdemo

コンテナ側で8080ポートで立ち上がるアプリケーションに localhost:9999 でアクセスできていることを確認できる。
無題.png

おわり。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?