2
2

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 1 year has passed since last update.

Docker+SpringBootの仮想環境作ってみた。

Last updated at Posted at 2023-04-08

Docker+SpringBootの仮想環境作ってみた。

Spring Bootで作成した、webアプリケーションを仮想環境で実装するサンプルプロジェクトを作成しました。

使用技術

Java : 17
Spring Boot : 3.0.5
thymeleaf
spring-boot-starter-web
lombok
spring-boot-devtools
Docker

ファイル構成

.
├── README.md
├── app
│   ├── build.gradle
│   ├── gradle
│   │   └── wrapper
│   │       ├── gradle-wrapper.jar
│   │       └── gradle-wrapper.properties
│   ├── gradlew
│   ├── gradlew.bat
│   ├── settings.gradle
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── com
│       │   │       └── example
│       │   │           └── demo
│       │   │               ├── AppApplication.java
│       │   │               └── controller
│       │   │                   └── helloController.java
│       │   └── resources
│       │       ├── application.properties
│       │       └── templates
│       │           └── index.html
│       └── test
│           └── java
│               └── com
│                   └── example
│                       └── demo
│                           └── AppApplicationTests.java
├── docker-compose.yml
└── dockerfile
  • dockerfile
FROM openjdk:18
RUN microdnf install findutils
  • dockercompose.yml
docker-compose.yml
version: '3'
services:
  app:
    build: .
    container_name: java
    tty: true
    ports:
      - 8080:8080
    working_dir: /doc
    volumes:
      - ./app:/doc

その他、追加したファウル

  • helloController.java
helloController.java
package com.example.demo.controller;

import org.springframework.stereotype.Controller;

@Controller
public class helloController {
	public String getHello() {
		return "/index";
	}
}
  • index.html
index.html
<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>sample</title>
</head>
<body>
	<h1>HelloWorld</h1>
</body>
</html>

起動方法

  • クローン
git@github.com:yukishi24/SpringBootTestContainer.git
  • docker-composeを起動
cd SpringBootTestContainer
docker-compose up -d
  • 起動確認
docker ps -a

以下が表示されていれば起動してる

CONTAINER ID   IMAGE                         COMMAND    CREATED          STATUS          PORTS                    NAMES
42f2e3e25b00   springboottestcontainer-app   "jshell"   15 seconds ago   Up 14 seconds   0.0.0.0:8080->8080/tcp   java
  • 起動したコンテナに入る
docker exec -it java /bin/sh
  • Springの起動
sh-4.4# sh gradlew bootRun

localhostに接続

ブラウザに「Hello World」が表示されていれば成功

詳しくは、GitHubを参照ください。
次回は、webアプリ開発に必要なDBを仮想環境に構築していきます。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?