# 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
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBsvgrQxCDYAJIpfUd%2Fimage.png?alt=media\&token=e1166777-5515-45aa-a1ea-a848d8b2b2a2)

```
cd gitlab-cicd/
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBt2ThEgtBvSQ2rU-t%2Fimage.png?alt=media\&token=fcfa8d65-a8bb-475d-baf4-90547678eba4)

```
cd testgolang/
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBtAIcwAU3ZFgWyxsz%2Fimage.png?alt=media\&token=88ce50ae-4ba8-4924-99b7-9225f85b6fdb)

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

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBtICGTbmxoVKXpnev%2Fimage.png?alt=media\&token=53889b1d-42b7-4a20-9224-ce3fd7ac8afd)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBtQdrhci-sFBWt-hY%2Fimage.png?alt=media\&token=b874a973-7822-4687-8dce-0d73d8ca66cc)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBte36oJzdEnEsIkdl%2Fimage.png?alt=media\&token=647f3c0b-bff6-4631-bb48-f99f2014a878)

## step 3.

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLC0ac7B6ZNMh-S223D%2Fimage.png?alt=media\&token=c9fb8c0a-0e08-440c-8982-bbc8e95e5539)

> **Git global setup**

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLCAOXRDca2Z2je9DQw%2Fimage.png?alt=media\&token=b65e8447-84d9-4dd8-a6e7-83a16dcae53c)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLCAWQUG6tt2pBA55o2%2Fimage.png?alt=media\&token=7bb5fec2-faa4-417b-b3fa-5a26ed7483e0)

> **Push an existing folder**

```
git init
```

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLCAgUF7PfKrGa27u5J%2Fimage.png?alt=media\&token=d941cea6-a192-4a85-a9ac-28352bfe90fd)

```
git add .
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLCAml4IVFzc-r8xVb1%2Fimage.png?alt=media\&token=579029a9-a2c2-4ad0-8157-6fa9ee42ba72)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLCAt-911hL0OJEigdA%2Fimage.png?alt=media\&token=4f896dfc-b0f8-4ca7-bee4-1cf17b39f2b6)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLCA5thgX_pGzxXiDmT%2Fimage.png?alt=media\&token=410bb228-243a-4247-ba26-ae12b3fd5410)

## Step 05.

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLC_NY0ZnPWCH_qNmHx%2Fimage.png?alt=media\&token=b985b2b5-cc06-4787-8ff1-b89c19a7f514)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC_sfZ0Tnas9hX7J3x%2F-MLCaZnCeRG24IDMChGe%2Fimage.png?alt=media\&token=51a22282-c1d2-4f03-9f73-461ac2ada162)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC_sfZ0Tnas9hX7J3x%2F-MLCai4EcLKGKKXt6ME9%2Fimage.png?alt=media\&token=c730f3c2-dd57-4685-8820-7ff97d57f0bb)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC_sfZ0Tnas9hX7J3x%2F-MLCaw3a22GGn6uAhoJI%2Fimage.png?alt=media\&token=c2034804-c7bc-426c-922e-6598e6ec1bf3)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MOo89Oi5tb8lezgytqZ%2F-MOoJaXWPbS3lt41FMBU%2Fimage.png?alt=media\&token=5355d060-afb0-4bfc-9ad7-f104699a9036)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MOo89Oi5tb8lezgytqZ%2F-MOoJgPRFsHYvMQfnSps%2Fimage.png?alt=media\&token=a5bab106-dd34-4c87-979e-d16eb6b84120)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MOo89Oi5tb8lezgytqZ%2F-MOoK724ESiOSDs6DGG3%2Fimage.png?alt=media\&token=ea05b738-b40f-496b-8e5a-38a0c1008b96)

```
git add main.go
```

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

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MOo89Oi5tb8lezgytqZ%2F-MOoL5B67CyRUoRXX2r7%2Fimage.png?alt=media\&token=0959da64-5b26-4a39-aa85-90e0e964027e)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MOo89Oi5tb8lezgytqZ%2F-MOoMOzOssna0OXuz2-l%2Fimage.png?alt=media\&token=ef6c813c-9da9-4b4c-a39c-eac6799e3049)

{% endtab %}

{% tab title="vm2" %}

## step 2.

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

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

```
cd
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBxlDqk5bHJsR0KDE2%2Fimage.png?alt=media\&token=c888c056-f103-4313-9013-225f60b09d2b)

```
cd .ssh
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBxfU2qR8HLxPoB3TG%2Fimage.png?alt=media\&token=dec5f487-1805-431b-8982-abc8f98954cd)

```
cat id_rsa.pub
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBxsKAtLfb3ZxbfY_l%2Fimage.png?alt=media\&token=10e9d030-06b1-4b44-aa5a-15500c3ba593)

```
pwd
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLBs9LQNrfEn7IqTRpu%2F-MLBxyXGlCaIbOci2J0k%2Fimage.png?alt=media\&token=ae4cae36-419e-49d3-b554-dc453ba6a1cf)

> 在 GitLab 貼上 SSH Key

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLByKbD7CVI9DHv9or_%2F-MLC-egtOa4CA3BXskdx%2Fimage.png?alt=media\&token=d34c1b71-499d-4ec3-8f9d-0f54b227ea23)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLC-iLaNco_DDffuBeM%2F-MLC-sBWHHkt9_vtRzos%2Fimage.png?alt=media\&token=d381c88a-4fa7-4c0c-aab2-f3f935861841)

## 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
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCW5Ici_acjGBznvcd%2Fimage.png?alt=media\&token=ea6dff05-b834-49e9-8943-395d98f8ee79)

```
gitlab-runner register
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCWEEKe1lma5UuPzyU%2Fimage.png?alt=media\&token=8ed2018e-07bc-46bc-9645-fc596af3d9b0)

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

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCWQFGQ66-07UUsbxf%2Fimage.png?alt=media\&token=45a966f2-c58f-497e-a6a9-f949fa1f136c)

> token 值

```
7S3H_DR4rfkoP_hPHeMP
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCXBIq19H0AMPI_1hu%2Fimage.png?alt=media\&token=15ca883e-8d35-4a34-aa70-ace2526fadea)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCWkjfH7hhxKLWT-Vu%2Fimage.png?alt=media\&token=532ac6b1-ef89-4278-a064-c789a90ef498)

> 設置名稱

```
vm2
```

```
vm2
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCWactZPze96cN0Wnl%2Fimage.png?alt=media\&token=51691ca5-e60f-4d3e-a039-7a7248bd7ad9)

> 執行方式

```
shell
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCWroFopkFrVvvacbh%2Fimage.png?alt=media\&token=e108522a-5bce-401c-b2cf-5845a9a4d590)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLCUl2Znk37N3g4YAU8%2F-MLCXjjcSybSreU1oX3W%2Fimage.png?alt=media\&token=510bdfc4-793f-4be0-b3f0-728637f51762)

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MLl5g406TAVxuUWdCuo%2F-MLlRDqkRJkTPxuUf1T0%2Fimage.png?alt=media\&token=62d67573-a8ec-4197-ac56-9d18fe7345e9)

```
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"]
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MOo89Oi5tb8lezgytqZ%2F-MOoUX1LpR0z4L4m8yt_%2Fimage.png?alt=media\&token=d551f4c4-6f96-4740-8cb7-4e8f8e5f88db)

> #### 建立 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) 
```

![](https://483685739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MHFSfnhkNCUTnJ2hcGc%2F-MOo89Oi5tb8lezgytqZ%2F-MOoUMDWRBdO_HjRCBdl%2Fimage.png?alt=media\&token=0bfaa024-1b98-43e2-9c7e-f69b80f608cf)

> #### 建立套件 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://anida-huang.gitbook.io/cloud-communication/qi-zhong/20201103-zi-liao-juan-yu-wang-lu-san.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
