Docker Composeを使用してMySQLとSping Bootの複数サーバコンテナを作成する

## Docker Compose
Docker Compose は、マルチコンテナー アプリケーションを定義および実行するためのツールです。Compose を使用すると、アプリケーション スタック全体の制御が簡素化され、単一のわかりやすい YAML 構成ファイルでサービス、ネットワーク、ボリュームを簡単に管理できるようになります。
## Docker Composeを使用して複数サーバコンテナを作成する
Sping Bootアプリ作成
```java
@RestController
public class HelloController {

  // 「/hello」へアクセスがあった場合
  @GetMapping("/hello")
  public String helloWorld() {
    // 文字列を返す
    return "Hello Spring-Boot World!!";
  }
```
Sping BootのDockerfileを作成する
```Dockerfile
FROM eclipse-temurin:11
RUN mkdir /opt/app
COPY demo.jar /opt/app
CMD ["java", "-jar", "/opt/app/demo.jar"]
```
Docker Composeのyamlを作成する
```yaml
version: '3.8'

services:
  mysql:
    container_name: my-mysql
    image: mysql
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: admin
    restart: always
    networks:
      - my-network

  springDemo:
    container_name: spring-demo
    build: ./springboot
    image: spring-demo
    ports:
     - "8080:8080"
    working_dir: /app
    networks:
      - my-network

volumes:
  mysql-data:

networks:
  my-network:
```
MySQLとSping Bootのサーバを作成する
```shell_session
$ tree ./
./
├── docker-compose.yml
└── springboot
    ├── Dockerfile
    └── demo.jar
$ docker compose up -d
[+] Running 3/3
 ✔ Network compose-mysql_my-network  Created                                                             0.2s
 ✔ Container spring-demo             Started                                                             0.7s
 ✔ Container my-mysql                Started                                                             0.8s
```
Sping Bootのアプリを確認する
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3184769/06dae4c0-6940-758b-cfad-6bb2cb65c771.png)
MySQLを確認する
```shell_session
$ docker container exec -it my-mysql sh
sh-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.3.0 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
sh-4.4# exit
exit
$ docker container ls -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS
                              NAMES
3d6d4923451a   mysql         "docker-entrypoint.s…"   6 minutes ago   Up 6 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   my-mysql
de315cae95a4   spring-demo   "/__cacert_entrypoin…"   6 minutes ago   Up 6 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp              spring-demo
$ docker volume ls
DRIVER    VOLUME NAME
local     b371e62c3e10b85c9c1ef23a17fecef7c858283a6feef6f892cf39414985aef3
local     compose-mysql_mysql-data
local     mysql-data
$ docker network ls
NETWORK ID     NAME                       DRIVER    SCOPE
2b1c922114f4   bridge                     bridge    local
eec3c81527a8   compose-mysql_my-network   bridge    local
0ca730ab1c0e   host                       host      local
4b11b0190fa9   none                       null      local
$ docker compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
my-mysql            mysql               "docker-entrypoint.s…"   mysql               7 minutes ago       Up 7 minutes        0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
spring-demo         spring-demo         "/__cacert_entrypoin…"   springDemo          7 minutes ago       Up 7 minutes        0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
```
全てコンテナを停止する
```shell-session
$ docker compose stop
[+] Running 2/2
 ✔ Container spring-demo  Stopped                                                                        0.5s
 ✔ Container my-mysql     Stopped                                                                        3.3s
 $ docker compose ps
NAME                IMAGE               COMMAND             SERVICE             CREATED             STATUS              PORTS
$ docker container ls -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                        PORTS     NAMES
3d6d4923451a   mysql         "docker-entrypoint.s…"   9 minutes ago   Exited (0) 13 seconds ago
my-mysql
de315cae95a4   spring-demo   "/__cacert_entrypoin…"   9 minutes ago   Exited (143) 16 seconds ago
spring-demo
```
全て停止してコンテナーを完全に削除する
--volumes:使用されるデータ ボリュームも削除する
```shell-session
$ docker compose down --volumes
[+] Running 4/4
 ✔ Container spring-demo             Removed                                                             0.0s
 ✔ Container my-mysql                Removed                                                             0.0s
 ✔ Volume compose-mysql_mysql-data   Removed                                                             0.0s
 ✔ Network compose-mysql_my-network  Removed
$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
$ docker volume ls
DRIVER    VOLUME NAME
local     b371e62c3e10b85c9c1ef23a17fecef7c858283a6feef6f892cf39414985aef3
local     mysql-data
$ docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
2b1c922114f4   bridge    bridge    local
0ca730ab1c0e   host      host      local
4b11b0190fa9   none      null      local
```