colima+dockerで環境構築した時のメモ

M1 macでcolima+dockerで環境構築した時に変なとこではまったのでメモ。

### colima, docker, docker-composeをinstall
```
brew install docker docker-compose colima
```

### colima start
```
colima start --profile x64 --arch x86_64 --cpu 8 --memory 8 --disk 128 --mount '~/workspace/my/colima/docker-compose:w' --dns 8.8.8.8
```

### パッケージ構成
```
.
├── docker
│   ├── centos7
│   │   ├── Dockerfile
│   │   └── src
│   └── mysql
│       ├── Dockerfile
├── docker-compose.yml
└── server
```

### はまった箇所
docker-compose.ymlの中身
```dockerfile
version: '3'
services:
    # ##### Go #####
    golang:
        container_name: sample-golang
        build:
            context: .
            dockerfile: ./docker/centos7/Dockerfile
        environment:
            - TZ=Asia/Tokyo
        volumes:
            - ./server:/usr/local/src
        privileged: true
        ports:
            - 8082:8082
        security_opt:
            - apparmor:unconfined
        cap_add:
            - SYS_PTRACE
        tty: true

    # ##### mysql #####
    mysql:
        image: mysql:5.7
        build:
            context: .
            dockerfile: ./docker/mysql/Dockerfile
        container_name: sample-mysql
        environment:
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_DATABASE=sample
            - MYSQL_USER=admin
            - MYSQL_PASSWORD=docker
            - TZ=Asia/Tokyo
        command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --innodb-use-native-aio=0
        volumes:
            - ./docker/mysql/data:/var/lib/mysql
            - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
            - ./docker/mysql/sql:/docker-entrypoint-initdb.d
        ports:
            - 3306:3306
            - 3307:3306
```

docker-compose起動
```
$ docker-compose up -d
...
[+] Running 3/3
 ⠿ Network docker-compose_default  Created 0.5s
 ⠿ Container sample-golang         Started 4.5s
 ⠿ Container sample-mysql          Started 4.5s
...

# mysqlコンテナに入ってみると...
$ docker exec -it sample-mysql bash
Error response from daemon: Container 5bfdd77c0ab0171658fabd4db2a33ef83890cf31487f50f05619b8e963d7d319 is not running

# ... Σ mysqlのコンテナが動いてない
```

ログを確認
```
chown: changing ownership of '/var/lib/mysql': Permission denied
chown: changing ownership of '/var/lib/mysql/private_key.pem': Permission denied
chown: changing ownership of '/var/lib/mysql/ca-key.pem': Permission denied
chown: changing ownership of '/var/lib/mysql/ibdata1': Permission denied
chown: changing ownership of '/var/lib/mysql/ib_logfile1': Permission denied
chown: changing ownership of '/var/lib/mysql/ib_buffer_pool': Permission denied
...
```
Permission deniedがもの凄い出てた...

色々と調べてみたら、docker-composeのmysqlの箇所に
「user:1000:1000」を追加するという対応策を見つけたので、追加して実行。
```docker
~~
        volumes:
            - ./docker/mysql/data:/var/lib/mysql
            - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf
            - ./docker/mysql/sql:/docker-entrypoint-initdb.d
        user: 1000:1000 # ← これを追加
        ports:
            - 3306:3306
            - 3307:3306
~~
```

無事にmysqlコンテナは起動してくれてコンテナ内にも入れた。
```
$ docker exec -it sample-mysql bash
I have no name!@XXXXXXXXXX:/$ mysql -u admin -h localhost -p -P 3306 sample
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 tables;
Empty set (0.01 sec)

mysql> CREATE TABLE sample (id bitint autoincrement primary_key not null);
mysql> CREATE TABLE sample (id bigint not null primary key auto_increment, name varchar(128) not null);
Query OK, 0 rows affected (0.07 sec)

mysql> show tables;
+------------------+
| Tables_in_sample |
+------------------+
| sample           |
+------------------+
1 row in set (0.01 sec)
```

ちなみに、
```
        volumes:
            - ./docker/mysql/data:/var/lib/mysql:rw
            - ./docker/mysql/my.cnf:/etc/mysql/conf.d/my.cnf:rw
            - ./docker/mysql/sql:/docker-entrypoint-initdb.d:rw
```
これでは解決しなかった。。。


### 参考URL
https://bmf-tech.com/posts/Ubuntu%2020.04.2%20LTS%E3%81%A7mysql%E3%82%B3%E3%83%B3%E3%83%86%E3%83%8A%E3%82%92%E8%B5%B7%E5%8B%95%E3%81%99%E3%82%8B%E3%81%A8Permission%20denied%E3%81%A7%E6%80%92%E3%82%89%E3%82%8C%E3%81%9F

* colima
https://github.com/abiosoft/colima
https://qiita.com/awrznc/items/617651c94585f55c4719
https://qiita.com/muramasa2/items/02bdf7dddd160c8f1d8a