Write a Dockerfile and docker-compose.yml
At first , we prepare a Dockerfile and a docker-compose.yml as follows.docker-compose_sample_git.yml
Dockerfile
version: "3" services: db: image: mysql:8 container_name: "spring_db_git" ports: - "3306:3306" volumes: - ./mysql/sql/:/docker-entrypoint-initdb.d - ./mysql/settings/:/var/lib/mysql - ./mysql/sql/mysql.cnf:/etc/mysql/conf.d/my.cnf environment: MYSQL_DATABASE: "sample" # database named "sample" is created MYSQL_ROOT_USER: "root" MYSQL_ROOT_PASSWORD: "root" TZ: "Asia/Tokyo" # SpringBootApplication spring: container_name: "spring_compose_git" build: context: . dockerfile: Dockerfile restart: always ports: - "8080:8080" tty: true depends_on: - db volumes: - ./spring_git:/srv:cached working_dir: /srv
FROM openjdk:11 RUN apt-get update && apt-get -y upgrade \ && apt-get install -y vim less wget git unzip \ && wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.zip \ && unzip apache-maven-3.8.6-bin.zipThen we execute next command in the directory where docker-compose_sample_git.yml and Dockerfile exits.
$docker-compose -f docker-compose_sample_git.yml up --build -d
Setting environment more
And then , we login spring_compose_git container and set environmetnt using some more command.$ docker exec -it spring_compose_git /bin/bash # get in container root@01720fff8d82:/srv# #internal container
Cloning the repository of SpringBoot
# git clone https://github.com/spring-guides/gs-spring-boot-docker.git
# cd /
Installing maven refering this page
# wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.zip
# unzip apache-maven-3.8.6-bin.zip
Editing PATH Variable
# export PATH=$PATH:/apache-maven-3.8.6/bin
# cd /srv/gs-spring-boot-docker/complete
Maven build , skipping test Directory
# ./mvnw package -Dmaven.test.skipIf you can confirm the message "build success" , you can go next step.
Editting application.yml
Next , we edit application.yml as follows.# pwd /srv/gs-spring-boot-docker/complete/src/main/resources # ls application.ymlapplication.yml
server: port: 8080 #TODO: figure out why I need this here and in bootstrap.yml spring: application: name: testLatticeApp datasource: url: jdbc:mysql://db:3306/sample?useLegacyDatetimeCode=false&serverTimezone=Asia/Tokyo username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver ribbon: ServerListRefreshInterval: 1000 endpoints: health: sensitive: false restart: enabled: true shutdown: enabled: true
Edittting pom.xml
# pwd /srv/gs-spring-boot-docker/complete # ls Dockerfile build.gradle gradle gradlew gradlew.bat mvnw mvnw.cmd pom.xml src targetpom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.7.1</version> <relativepath> <!--lookup parent from repository--> </relativepath></parent> <groupid>com.example</groupid> <artifactid>spring-boot-docker-complete</artifactid> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-docker-complete</name> <description>Demo project for Spring Boot</description> <properties> <java .version="">1.8</java> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jdbc</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
Create a Controller
Furthermore we create new file , TestDB.java , as follows.# pwd /srv/gs-spring-boot-docker/complete/src/main/java/hello # ls Application.java TestDB.javaTestDB.java
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class TestDB { @Autowired JdbcTemplate jdbcTemplate; @RequestMapping("/db") public String testDB() { return jdbcTemplate.queryForList("SELECT * FROM sample").toString(); } }
Build again
Finally , let's build project again# pwd /srv/gs-spring-boot-docker/complete # ./mvnw package -Dmaven.test.skip
Insert some data into mysql
Before booting SpringBoot Project , we insert somedata into mysql (spring_db_git container).we create sample table in sample database ,and insert some data with next command
mysql> create table sample(ID int(10)); Query OK, 0 rows affected, 1 warning (0.09 sec) mysql> insert sample value(10); Query OK, 1 row affected (0.04 sec) mysql> select * from sample; +------+ | ID | +------+ | 10 | +------+ 1 row in set (0.00 sec)
Boot SpringProject and Access
If we succusess in building the Spring Project , let's boot the project.### check the target directory and which to exectute .jar file. #java -jar target/spring-boot-docker-complete-0.0.1-SNAPSHOT.jarAnd then , we try to access URL http://localhost:8080/db from browser in host.
If we are in successful in setting the environment, we must see the following message.
[{ID=10}]
0 件のコメント:
コメントを投稿