{"id":526,"date":"2019-02-22T00:00:00","date_gmt":"2019-02-21T15:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/02\/22\/java11_springboot_docker"},"modified":"2022-10-26T10:13:56","modified_gmt":"2022-10-26T01:13:56","slug":"java11_springboot_docker-2","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/02\/22\/java11_springboot_docker-2\/","title":{"rendered":"Java11+Spring Boot+Docker\u3067\u4f5c\u308bWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u305d\u306e\uff12"},"content":{"rendered":"
\u672c\u8a18\u4e8b\u306f\u524d\u56de\u79c1\u304c\u63b2\u8f09\u3057\u305f\u8a18\u4e8bJava11+Spring Boot+Docker\u3067\u4f5c\u308bWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3<\/a>\u306e\u7d9a\u304d\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n \u524d\u56de\u306fWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u96db\u5f62\u3092\u4f5c\u6210\u3057\u3066Docker\u3067MySQL\u30b3\u30f3\u30c6\u30ca\u3092\u7acb\u3061\u4e0a\u3052\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u69cb\u7bc9\u3057\u307e\u3057\u305f\u306e\u3067\u3001\u4eca\u56de\u306fWeb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304b\u3089MySQL\u306b\u63a5\u7d9a\u3057\u3066\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u753b\u9762\u306b\u8868\u793a\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u69cb\u7bc9\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u5074\u3068\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u5074\u306e\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u69cb\u9020\u304a\u3088\u3073\u30d5\u30a1\u30a4\u30eb\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n CustomerController.java<\/p>\n Customer.java<\/p>\n CustomerService.java<\/p>\n CustomerRepository.java<\/p>\n list.html<\/p>\n application.yml<\/p>\n \u524d\u56de\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30eb\u30fc\u30c8\u914d\u4e0b\u306b\u4f5c\u6210\u3057\u305fdocker-compose.yml\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306bapp\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n docker-compose.yml<\/p>\n \u6700\u5f8c\u306b\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30eb\u30fc\u30c8\u914d\u4e0b\u306bDockerfile\u3092\u4f5c\u6210\u3057\u3066\u5b8c\u6210\u3067\u3059\u3002<\/p>\n Dockerfile<\/p>\n \u3053\u3053\u307e\u3067\u51fa\u6765\u305f\u3089\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30d3\u30eb\u30c9\u3057\u3066\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n \u6b63\u5e38\u306bMySQL\u3068\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u8d77\u52d5\u3057\u305f\u3053\u3068\u3092\u78ba\u8a8d\u5f8c\u3001\u4ee5\u4e0b\u306eURL\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068DB\u306b\u767b\u9332\u3055\u308c\u305fcustomer\u30c7\u30fc\u30bf\u304c\u753b\u9762\u306b\u8868\u793a\u3055\u308c\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002 \u4ee5\u4e0a\u3001\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u306a\u69cb\u6210\u3067\u3059\u304c\u3001Java11+Spring Boot+Docker\u3067\u69cb\u6210\u3055\u308c\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u69cb\u7bc9\u306b\u3064\u3044\u3066\u306e\u307e\u3068\u3081\u3067\u3057\u305f\u3002<\/p>\n \u306a\u304a\u3001\u4ee5\u4e0b\u306e\u307a\u30fc\u30b8\u3067MMM\u306eDocker\u30b3\u30f3\u30c6\u30ca\u3078\u306e\u53d6\u308a\u7d44\u307f\u3092\u3054\u7d39\u4ecb\u3057\u3066\u3044\u307e\u3059\u3002\u305c\u3072\u5408\u308f\u305b\u3066\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n\u2514\u2500\u2500 src\n \u2514\u2500\u2500main\n \u00a0 \u251c\u2500\u2500 java\n \u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 com\n \u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 example\n \u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 demo\n \u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 DemoApplication.java\n \u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 domain\n \u00a0 \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 Customer.java\n \u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 repository\n \u00a0 \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 CustomerRepository.java\n \u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 service\n \u00a0 \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 CustomerService.java\n \u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 web\n \u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 CustomerController.java\n \u00a0 \u2514\u2500\u2500 resources\n \u00a0 \u251c\u2500\u2500 application.yml\n \u00a0 \u251c\u2500\u2500 static\n \u00a0 \u2514\u2500\u2500 templates\n \u00a0 \u2514\u2500\u2500 customer\n \u00a0 \u2514\u2500\u2500 list.html<\/code><\/pre>\n
\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u90e8\u5206\u3092\u4f5c\u6210<\/h2>\n
@Controller\n@RequestMapping("customer")\npublic class CustomerController {\n @Autowired\n CustomerService customerService;\n\n @GetMapping(path = "list")\n String list(Model model) {\n List<Customer> customers = customerService.findAll();\n model.addAttribute("customers", customers);\n return "customer\/list";\n }\n}<\/code><\/pre>\n
@Data\n@Entity\n@Table(name = "customer")\npublic class Customer {\n @Id\n @GeneratedValue\n private Integer id;\n private String firstName;\n private String lastName;\n}<\/code><\/pre>\n
@Service\n@Transactional\npublic class CustomerService {\n @Autowired\n CustomerRepository customerRepository;\n\n public List<Customer> findAll() {\n return customerRepository.findAll();\n }\n}<\/code><\/pre>\n
public interface CustomerRepository extends JpaRepository<Customer, Integer> {\n}<\/code><\/pre>\n
<!doctype html>\n<html xmlns:th="http:\/\/www.thymeleaf.org">\n<head>\n <meta charset="UTF-8">\n <title>Customers<\/title>\n<\/head>\n<body>\n<table class="table">\n <thead class="thead-dark">\n <tr>\n <th scope="col">ID<\/th>\n <th scope="col">FirstName<\/th>\n <th scope="col">LastName<\/th>\n <\/tr>\n <\/thead>\n <tbody>\n <tr th:each="customer : ${customers}">\n <td th:text="${customer.id}">id<\/td>\n <td th:text="${customer.firstName}">firstName<\/td>\n <td th:text="${customer.lastName}">lastName<\/td>\n <\/tr>\n <\/tbody>\n<\/table>\n<\/body>\n<\/html><\/code><\/pre>\n
spring:\n profiles:\n active: localhost\n---\nspring:\n profiles: localhost\n datasource:\n driverClassName: com.mysql.cj.jdbc.Driver\n url: jdbc:mysql:\/\/localhost:3306\/demo_db\n username: user\n password: password\n jpa:\n database-platform: org.hibernate.dialect.MySQL5InnoDBDialect\n show-sql: true\n hibernate:\n ddl-auto: update\n data:\n web:\n base-path: \/customer\n---\nspring:\n profiles: docker\n datasource:\n driverClassName: com.mysql.cj.jdbc.Driver\n url: jdbc:mysql:\/\/dbserver\/demo_db\n username: user\n password: password\n jpa:\n database-platform: org.hibernate.dialect.MySQL5InnoDBDialect\n show-sql: true\n hibernate:\n ddl-auto: update\n data:\n web:\n base-path: \/customer<\/code><\/pre>\n
version: '2'\nservices:\n dbserver:\n build: .\/docker\/mysql\n image: demo-db:0.0.1\n restart: always\n environment:\n MYSQL_DATABASE: demo_db\n MYSQL_USER: user\n MYSQL_PASSWORD: password\n MYSQL_ROOT_PASSWORD: rootpassword\n ports:\n - "3314:3306"\n volumes:\n - .\/docker\/mysql\/initdb.d:\/docker-entrypoint-initdb.d\n - .\/docker\/mysql\/conf.d:\/etc\/mysql\/conf.d\n - .\/log\/mysql:\/var\/log\/mysql\n\n app:\n build: .\n image: demo-app\/boot:0.0.1\n depends_on:\n - dbserver\n ports:\n - "8080:8080"\n volumes:\n - .:\/app\n environment:\n JAR_TARGET: demo-0.0.1-SNAPSHOT.jar\n\nvolumes:\n mysql-db:\n driver: local<\/code><\/pre>\n
FROM ubuntu:18.10\nRUN apt-get update\nRUN apt-get -y install openjdk-11-jdk\nENV JAVA_HOME \/usr\/lib\/jvm\/java-11-openjdk-amd64\nVOLUME \/tmp\nRUN mkdir \/app\nWORKDIR \/app\n# jar target\nENV JAR_TARGET "demo-0.0.1-SNAPSHOT.jar"\n# set entrypoint to execute spring boot application\nENTRYPOINT ["sh","-c","java -jar -Dspring.profiles.active=docker build\/libs\/${JAR_TARGET}"]<\/code><\/pre>\n
docker-compose build<\/code>
docker-compose up<\/code><\/p>\n
\nhttp:\/\/localhost:8080\/customer\/list<\/a><\/p>\n