Dockerの勉強をしているものです。
Spring BootアプリのコンテナからMaria DBのコンテナに接続したいのですが、Spring Bootアプリのコンテナを起動させている最中に
以下のエラーが発生します。
Caused by: java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:127.0.0.1, port:3306. Connection refused at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:188) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createSocket(AbstractConnectProtocol.java:257) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createConnection(AbstractConnectProtocol.java:521) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1389) ~[mariadb-java-client-2.7.4.jar!/:na] ... 57 common frames omitted Caused by: java.net.ConnectException: Connection refused at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na] at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:658) ~[na:na] at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na] at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na] at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na] at java.base/java.net.Socket.connect(Socket.java:648) ~[na:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createSocket(AbstractConnectProtocol.java:252) ~[mariadb-java-client-2.7.4.jar!/:na] ... 59 common frames omitted
Spring Bootのアプリケーションは以下のサイトを参考に実装しています。
https://qiita.com/rei_012/items/106024b61685cd361234
Controllerクラス
java
1@RestController 2@RequestMapping("/api/users") 3public class UserController { 4 @Autowired 5 UserService userService; 6 7 @RequestMapping(method = RequestMethod.GET) 8 public List<User> getUsers() { 9 return userService.findUsers(); 10 } 11}
Serviceクラス
java
1@Service 2@Transactional 3public class UserService { 4 @Autowired 5 UserRepository userRepository; 6 7 public List<User> findUsers(){ 8 return userRepository.findAll(); 9 } 10}
Repositoryクラス
java
1@Repository 2public interface UserRepository extends JpaRepository<User, Long> { 3}
Entityクラス
java
1@Data 2@Entity 3@Table(name = "users") 4public class User { 5 @Id 6 @GeneratedValue 7 private Long id; 8 private String name; 9 private String email; 10}
spring.properties
spring.datasource.url=jdbc:mariadb://127.0.0.1:3306/sampleapi_development spring.datasource.username=hogeuser spring.datasource.password=password spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
pom.xml
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.6.1</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.sampleApi</groupId> 12 <artifactId>sampleApi</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <packaging>jar</packaging> 15 <name>sampleApi</name> 16 <description>Demo project for Spring Boot</description> 17 <properties> 18 <java.version>15</java.version> 19 </properties> 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-data-jdbc</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-data-jpa</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-web</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>mysql</groupId> 36 <artifactId>mysql-connector-java</artifactId> 37 <scope>runtime</scope> 38 </dependency> 39 <dependency> 40 <groupId>org.mariadb.jdbc</groupId> 41 <artifactId>mariadb-java-client</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>org.projectlombok</groupId> 45 <artifactId>lombok</artifactId> 46 <scope>provided</scope> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-starter-test</artifactId> 51 <scope>test</scope> 52 </dependency> 53 </dependencies> 54 55 <build> 56 <plugins> 57 <plugin> 58 <groupId>org.springframework.boot</groupId> 59 <artifactId>spring-boot-maven-plugin</artifactId> 60 </plugin> 61 </plugins> 62 </build> 63 64</project>
Dockerfileとdocker-compose.yamlはそれぞれ以下のような設定です。
Dockerfile
FROM openjdk:15-jdk-alpine VOLUME /tmp RUN mkdir /app WORKDIR /app ENV JAVA_OPTS="" ENV JAR_TARGET "sampleApi-1.0.0-SNAPSHOT.jar" COPY ./target/sampleApi-0.0.1-SNAPSHOT.jar sampleApi-0.0.1-SNAPSHOT.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","sampleApi-0.0.1-SNAPSHOT.jar"]
docker-compose.yaml
yaml
1version: "3" 2services: 3 db: 4 image: mariadb:10.2 5 restart: always 6 command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 7 environment: 8 MYSQL_ROOT_PASSWORD: password 9 MYSQL_USER: hogeuser 10 MYSQL_PASSWORD: password 11 MYSQL_DATABASE: sampleapi_development 12 ports: 13 - "3306:3306" 14 volumes: 15 - ./docker/tmp/mysql:/var/lib/mysql 16 - ./docker/mysql/initdb.d:/docker-entrypoint-initdb.d 17 expose: 18 - "3306" 19 networks: 20 - app-net 21 22 java-sample: 23 image: java-app2 24 container_name: java-app2-container 25 build: . 26 ports: 27 - 8080:8080 28 depends_on: 29 - db 30 links: 31 - mysql:db 32 tty: true 33 networks: 34 - app-net 35 36networks: 37 app-net: 38 driver: bridge
docker-compose up -dコマンドでイメージの作成と起動を行おうとしますが、Spring Bootのアプリのコンテナの起動に失敗します。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e64252a107af java-app2 "java -jar sampleApi…" 16 seconds ago Exited (1) 6 seconds ago java-app2-container cbcf328feca0 mariadb:10.2 "docker-entrypoint.s…" 16 seconds ago Up 13 seconds 0.0.0.0:3306->3306/tcp sampleapi_db_1
docker logsコマンドでログを確認すると以下の出力となる。
docker logs e64252a107af Caused by: java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:127.0.0.1, port:3306. Connection refused at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:188) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createSocket(AbstractConnectProtocol.java:257) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createConnection(AbstractConnectProtocol.java:521) ~[mariadb-java-client-2.7.4.jar!/:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:1389) ~[mariadb-java-client-2.7.4.jar!/:na] ... 57 common frames omitted Caused by: java.net.ConnectException: Connection refused at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na] at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:658) ~[na:na] at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na] at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na] at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na] at java.base/java.net.Socket.connect(Socket.java:648) ~[na:na] at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.createSocket(AbstractConnectProtocol.java:252) ~[mariadb-java-client-2.7.4.jar!/:na] ... 59 common frames omitted
MariaDBのコンテナのみ起動し、Eclipseからアプリケーションを起動するとエラーなく起動できます。
その状態でhttp://localhost:8080/api/usersにアクセスするとテーブルの取得結果が画面に表示できます。
外部からmysqlコンテナ、mariadbコンテナに接続というキーワードでいろいろと調べていますが、未だに解決できていません。
Dockerにお詳しい方がいましたらご回答いただけないでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/16 20:51
2021/12/18 02:37 編集
2021/12/18 02:59 編集
2021/12/18 05:45 編集
2021/12/19 04:53
2021/12/20 13:05