> For the complete documentation index, see [llms.txt](https://anida-huang.gitbook.io/cloud-communication/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://anida-huang.gitbook.io/cloud-communication/qi-zhong/20201103-zi-liao-juan-yu-wang-lu-san.md).

# 20201103 資料卷與網路 (三)

## 課堂資料

{% embed url="<https://github.com/yangshun2005/gitlab-cicd>" %}

{% embed url="<https://blog.techbridge.cc/2018/09/07/docker-compose-tutorial-intro/>" %}

## 課堂練習

### GitLab-CICD

{% tabs %}
{% tab title="vm1" %}

## step 1.

{% hint style="info" %}
本地端建立 Docker Images 並測試是否能正常運作
{% endhint %}

```
git clone https://github.com/yangshun2005/gitlab-cicd.git
```

![](/files/-MLBsvgrQxCDYAJIpfUd)

```
cd gitlab-cicd/
```

![](/files/-MLBt2ThEgtBvSQ2rU-t)

```
cd testgolang/
```

![](/files/-MLBtAIcwAU3ZFgWyxsz)

```
gedit Dockerfile main.go &
```

```
docker build -t testgo:1.0 .
```

![](/files/-MLBtICGTbmxoVKXpnev)

```
docker run -d -p 8001:8001 testgo:1.0
```

![](/files/-MLBtQdrhci-sFBWt-hY)

```
curl [IP 位置]:8001/hello
```

![](/files/-MLBte36oJzdEnEsIkdl)

## step 3.

{% hint style="info" %}
在 GitLab 上新增一個 Project ( test go )
{% endhint %}

![](/files/-MLC0ac7B6ZNMh-S223D)

> **Git global setup**

```
git config --global user.name "Anida-Huang"
```

![](/files/-MLCAOXRDca2Z2je9DQw)

```
git config --global user.email "xiaoji850312@gmail.com"
```

![](/files/-MLCAWQUG6tt2pBA55o2)

> **Push an existing folder**

```
git init
```

```
git remote add origin git@gitlab.com:Anida-Huang/testgo.git
```

![](/files/-MLCAgUF7PfKrGa27u5J)

```
git add .
```

![](/files/-MLCAml4IVFzc-r8xVb1)

```
git commit -m "Initial commit"
```

![](/files/-MLCAt-911hL0OJEigdA)

```
git push -u origin master
```

![](/files/-MLCA5thgX_pGzxXiDmT)

## Step 05.

```
gedit .gitlab-ci.yml
```

![](/files/-MLC_NY0ZnPWCH_qNmHx)

```
git add .gitlab-ci.yml
```

![](/files/-MLCaZnCeRG24IDMChGe)

```
git commit -m "submit a modified .gitlab-ci.yml"
```

![](/files/-MLCai4EcLKGKKXt6ME9)

```
git push -u origin master
```

![](/files/-MLCaw3a22GGn6uAhoJI)

![](/files/-MOoJaXWPbS3lt41FMBU)

![](/files/-MOoJgPRFsHYvMQfnSps)

![](/files/-MOoK724ESiOSDs6DGG3)

```
git add main.go
```

```
git commit -m "modified main.go"
```

```
git push -u origin master
```

![](/files/-MOoL5B67CyRUoRXX2r7)

![](/files/-MOoMOzOssna0OXuz2-l)

{% endtab %}

{% tab title="vm2" %}

## step 2.

{% hint style="danger" %}
在另一台機器 \[ vm2 ] 安裝 gitlab-runner，并產生 ssh-key。

把 public key 貼到 GitLab 上
{% endhint %}

```
cd
```

![](/files/-MLBxlDqk5bHJsR0KDE2)

```
cd .ssh
```

![](/files/-MLBxfU2qR8HLxPoB3TG)

```
cat id_rsa.pub
```

![](/files/-MLBxsKAtLfb3ZxbfY_l)

```
pwd
```

![](/files/-MLBxyXGlCaIbOci2J0k)

> 在 GitLab 貼上 SSH Key

![](/files/-MLC-egtOa4CA3BXskdx)

![](/files/-MLC-sBWHHkt9_vtRzos)

## step 4.

{% hint style="info" %}
在 \[ vm2 ] 執行`gitlab-runner register`
{% endhint %}

> Simply download one of the binaries for your system：

```
https://blog.techbridge.cc/2018/09/07/docker-compose-tutorial-intro/
```

> Give it permissions to execute：

```
chmod +x /usr/local/bin/gitlab-runner
```

> Create a GitLab CI user：

```
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
```

```
usermod -aG docker gitlab-runner
```

![](/files/-MLCW5Ici_acjGBznvcd)

```
gitlab-runner register
```

![](/files/-MLCWEEKe1lma5UuPzyU)

```
https://gitlab.com/
```

![](/files/-MLCWQFGQ66-07UUsbxf)

> token 值

```
7S3H_DR4rfkoP_hPHeMP
```

![](/files/-MLCXBIq19H0AMPI_1hu)

![](/files/-MLCWkjfH7hhxKLWT-Vu)

> 設置名稱

```
vm2
```

```
vm2
```

![](/files/-MLCWactZPze96cN0Wnl)

> 執行方式

```
shell
```

![](/files/-MLCWroFopkFrVvvacbh)

![](/files/-MLCXjjcSybSreU1oX3W)

![](/files/-MLlRDqkRJkTPxuUf1T0)

```
docker images
```

```
cd
```

```
cd /home/user
```

## Step 06.

```
docker images
```

```
gitlab-runner start
```

## Step 07.

```
docker ps
```

```
docker ps rm -f [容器ID]
```

{% endtab %}
{% endtabs %}

### Docker Compose

```
cd
```

```
mkdir test-dockercompose
```

```
cd test-dockercompose/
```

> #### 創建 ubuntu 並安裝 nginx 的 image

```
gedit Dockerfile
```

```
FROM ubuntu:16.04
MAINTAINER demo@gmail.com

RUN apt-get update
RUN apt-get install –y nginx
CMD ["echo", "Nginx Image created"]
```

![](/files/-MOoUX1LpR0z4L4m8yt_)

> #### 建立 app.py 當做 web app 進入點

```
gedit app.py
```

```
import time
import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def get_index():
    count = get_hit_count()
    return 'Yo! 你是第 {} 次瀏覽\n'.format(count)

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True) 
```

![](/files/-MOoUMDWRBdO_HjRCBdl)

> #### 建立套件 requirements.txt 安裝資訊讓 Dockerfile 可以下指令安裝套件

```
gedit requirements.txt
```

```
flask
redis
```

> #### 建立 Web App 的 Dockerfile

```
FROM Python
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
```

> #### Docker Compose file 描述 services 運作狀況

```
gedit docker-compose.yml &
```

```
version: '3'
services:
web:
    build: .
    ports:
    -"5000:5000"
    volumes:
    - .:/code
redis:
    image: "redis:alpine"
```

#### 在背景執行更新`docker-compose`

```
docker-compose up -d
```

```
curl 127.0.0.1:5000
```

> #### 終止並移除 container

```
docker-compose down
```

```
docker-compose up -d
```

```
curl 127.0.0.1:5000
```
