JenkinsのJobでdocker-composeを動かすよ

- docker-composeでJenkins動かすはあったけどJenkins Jobの中でdocker-compose動かすはなんか見つからなかったのでめもめも
- ほんとはCircleCIで動かしたいが、docker-composeをCircleCIで動かすにはmachineで動かさなくちゃいけない。
    - ~~ただしそのpyenvでは3.7.0a2までしか出てこずこのバージョンでは開発中のアプリが動かないのでしかたなくJenkinsで環境構築した。~~
    - [Python 3.8系も指定できました](https://qiita.com/cossy70461807/items/e9bfed68503f2d264306)

# 環境
Amazon Linux2
Jenkins 2.159

# 手順

１. Jenkinsプラグイン管理画面から[「Docker Compose Build Step Plugin」] (https://plugins.jenkins.io/docker-compose-build-step/)をJenkinsにインストールを今回はした

- ぶっちゃけシェルを実行で `docker-compose build` とか書いちゃってもOK


２. Dockerをインストール

```
$ sudo yum install docker
```

３. Dockerデーモンの起動と再起動時に自動的にデーモンが立ち上がるように

```

$ docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

$ systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://docs.docker.com

$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

$ sudo systemctl start docker

$ systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-04-07 01:04:09 JST; 2s ago
     Docs: https://docs.docker.com
  Process: 15467 ExecStartPre=/usr/libexec/docker/docker-setup-runtimes.sh (code=exited, status=0/SUCCESS)
  Process: 15452 ExecStartPre=/bin/mkdir -p /run/docker (code=exited, status=0/SUCCESS)
 Main PID: 15474 (dockerd)
    Tasks: 10
   Memory: 37.1M
   CGroup: /system.slice/docker.service

$ docker version
Client:
 Version:           19.03.6-ce
 API version:       1.40
 Go version:        go1.13.4
 Git commit:        369ce74
 Built:             Fri Mar  6 23:25:53 2020
 OS/Arch:           linux/amd64
 Experimental:      false
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2
Fvar%2Frun%2Fdocker.sock/v1.40/version: dial unix /var/run/docker.sock: connect: permission denied
# ↑今回はJenkins君がDockerいじれればいいのでその他ユーザーはJenkinsグループに入れない
```

４. Jenkins君をDockerグループに追加

```
$ cat /etc/group | grep docker
docker:x:990:

$ sudo gpasswd -a jenkins docker
Adding user jenkins to group docker

$ cat /etc/group | grep docker
docker:x:990:jenkins

$ sudo systemctl restart docker

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS
 NAMES
# ↑成功
```

```
↓これだと正しくグループに追加されていない
$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2
Fvar%2Frun%2Fdocker.sock/v1.40/containers/json: dial unix /var/run/docker.sock: connect: permission denied
```


５. Docker-composeのインストール
  - yumだとなぜかエラーになっていたのでソースから

```
$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

$ sudo chmod +x /usr/local/bin/docker-compose

$ docker-compose --version
docker-compose version 1.23.2, build 1110ad01
```


６. jenkins君にPATHを追加してあげる

```
$ printenv PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/${UserName}/.composer/vendor/bin:/home/${UserName}/.local/bin:/home/${UserName}/bin

$ which docker-compose
/usr/local/bin/docker-compose

$ sudo su jenkins
bash-4.2$ export PATH=$PATH:/usr/local/bin/

bash-4.2$ printenv PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin/

bash-4.2$ docker-compose --version
docker-compose version 1.23.2, build 1110ad01

```


７. 念のためサービス再起動

```
$ sudo systemctl restart docker
$ sudo systemctl restart jenkins
```