Docker 事始め　Vagrant On rocky8

# etc docker

## docker install

[参考](https://zenn.dev/supersatton/articles/f18c3135234b2e)

```bash
[root@docker ~]# docker -v
Docker version 20.10.15, build fd82621
```

## docker-compose install

```bash
curl -L https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
```

```bash
chmod +x /usr/local/bin/docker-compose
[root@docker vagrant]# docker-compose --version
Docker Compose version v2.5.0
```

### Docker cmd list

[URL](https://qiita.com/nimusukeroku/items/72bc48a8569a954c7aa2)

```bash
# 強制削除
docker rm -f getting-started-docs-1
```

### httpd 

```bash
# Docker image pull
docker pull httpd
# Docker httpd start
docker run -d --name my-apache -p 8080:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd
# 確認
curl http://localhost:8080
## FROM HOST
http://192.168.56.10:8080/
```

```bash
docker run -d -p 80:80 docker/getting-started
# localhost
http://192.168.56.10/tutorial/
```

```bash
docker stop 0c57b84663b8
docker rm reverent_vaughan
```

```bash
docker container kill -s KILL jolly_wing
docker container kill -s KILL my-apache
docker container prune
```

```bash
docker images --filter "dangling=true"

docker rmi $(docker images -f "dangling=true" -q)
```

### Terminal

```bash
docker run --name test -it debian
```

### サンプル・アプリケーション

[Docker公式](https://docs.docker.jp/get-started/02_our_app.html#sample-application)

```bash
sudo su -
cd /share/vagrant/01.sample/
## git 無かったのでHOST側で
git clone https://github.com/docker/getting-started.git
cd getting-started/app
docker build -t getting-started .
# => REPOSITORY = getting-started
docker run -dp 3000:3000 --rm --name gs getting-started  
```

```bash
docker container ls
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                            NAMES
3799de15aa65   getting-started   "docker-entrypoint.s…"   18 seconds ago   Up 17 seconds   0.0.0.0:3000->3000/tcp   gs
```

```bash
docker container exec -it gs ls /bin/
docker container exec -it gs /bin/ash
```

```text
http://192.168.56.10:3000
curl http://localhost:3000
```

### DB の保持

```bash
docker volume create todo-db
docker run -dp 3000:3000 --rm --name gs -v todo-db:/etc/todos getting-started
```

```bash
docker container exec -it gs /bin/ash
```

```bash
docker volume inspect todo-db
ll /var/lib/docker/volumes/todo-db/_data/
total 8
-rw-r--r-- 1 root root 8192 May  9 21:39 todo.db
```

```text
# Docker\01.sample\getting-started\app\src\persistence\sqlite.js
 1 const sqlite3 = require('sqlite3').verbose();
 2 const fs = require('fs');
 3 const location = process.env.SQLITE_DB_LOCATION || '/etc/todos/todo.db';
```

### バインド マウント の使用

*vagrant の共有フォルダでは volume 上手く行かない

```bash
mkdir -p /share/work
cp -R /share/vagrant/01.sample/getting-started/app /share/work/.
cd /share/work/app
docker run -dp 3000:3000 --name sample \
-w /app -v "$(pwd):/app" \
node:12-alpine \
sh -c "yarn install && yarn run dev"

docker logs -f sample
```

```bash
vim /share/work/app/src/static/js/app.js
```

```text
# 109 行目
-  {submitting ? 'Adding...' : 'Add Item'}
+  {submitting ? 'Adding...' : 'Add'}
```

*結果*

```bash
docker logs -f sample
```

```text
[nodemon] 2.0.13
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/index.js`
Using sqlite database at /etc/todos/todo.db
Listening on port 3000
[nodemon] restarting due to changes...        # 変更を検知して再起動してくれる。
[nodemon] starting `node src/index.js`
Using sqlite database at /etc/todos/todo.db
Listening on port 3000
```

### 複数コンテナのアプリ

```bash
# pwd
/share/work/app
docker network create todo-app
```

* MySQL コンテナを起動

```bash
docker run -d --name mysql --rm \
--network todo-app --network-alias mysql \
-v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \
mysql:5.7
```

* db 確認

```bash
docker exec -it mysql mysql -u root -p'secret'
```

* network 確認

```bash
docker run -it --network todo-app --name net_tools nicolaka/netshoot
```

```text
;; ANSWER SECTION:
mysql.                  600     IN      A       172.19.0.2
```

* app 確認

```bash
docker run -dp 3000:3000 --name sample --rm \
  -w /app -v "$(pwd):/app" \
  --network todo-app \
  -e MYSQL_HOST=mysql \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=secret \
  -e MYSQL_DB=todos \
  node:12-alpine \
  sh -c "yarn install && yarn run dev"
```

```bash
docker logs -f sample
```

```text
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/index.js`
Waiting for mysql:3306.
Connected!
Connected to mysql db at host mysql
Listening on port 3000
```

### Docker Compose を使う

[URL](https://docs.docker.jp/get-started/08_using_compose.html)

```bash
[vagrant@docker ~]$ docker --version
Docker version 20.10.15, build fd82621
[vagrant@docker ~]$ docker-compose --version
Docker Compose version v2.5.0
```

```bash
cp /share/vagrant/01.sample/docker-compose/docker-compose.yml /share/work/app/docker-compose.yml
cd /share/work/app
```

```bash
[root@docker ap]# docker-compose up -d
[+] Running 4/4
 ⠿ Network docker-compose_default           Created   0.1s 
 ⠿ Volume "docker-compose_todo-mysql-data"  Created   0.0s
 ⠿ Container docker-compose-mysql-1         Started   1.9s 
 ⠿ Container docker-compose-app-1           Started   1.9s
 ```

```bash
[root@docker app]# docker ps -a                                                                                                                      
CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS        PORTS                    NAMES
4fdc0b024710   node:12-alpine   "docker-entrypoint.s…"   3 seconds ago   Up 1 second   0.0.0.0:3000->3000/tcp   app-app-1
5e3945cc7a4b   mysql:5.7        "docker-entrypoint.s…"   3 seconds ago   Up 1 second   3306/tcp, 33060/tcp      app-mysql-1
```

  ```text
   app という名前のグループが見えます。
   これは Docker Compose による「プロジェクト名」で、同じグループのコンテナに対して使われます。
   デフォルトでは、プロジェクト名はシンプルに docker-compose.yaml が置かれているディレクトリ名(app)です。
  ```

```bash
docker-compose logs -f app
docker-compose down
```

```bash
ll /var/lib/docker/volumes
total 24
drwx-----x  3 root root     19 May 10 22:08 app_todo-mysql-data
brw-------  1 root root 253, 0 May 10 21:54 backingFsBlockDev
drwx-----x  3 root root     19 May 10 22:02 docker-compose_todo-mysql-data
-rw-------. 1 root root  32768 May 10 22:08 metadata.db
drwx-----x  3 root root     19 May  9 21:34 todo-db
drwx-----x  3 root root     19 May  9 22:38 todo-mysql-data
```