# 雲端通訊整合實務(11/03) ###### tags: `docker` ## gitlab應用2 下載gitlab-cicd: ``` git clone https://github.com/yangshun2005/gitlab-cicd.git ``` 安裝完之後,進入到`gitlab-cicd/testgolang`資料夾下,並執行: ``` docker build -t testgo:1.0 . ``` 完成後可以透過`docker images`來查看鏡像: ``` root@vm2:~/gitlab-cicd/testgolang# docker images REPOSITORY TAG IMAGE ID CREATED SIZE testgo 1.0 405137fa7a3c 4 seconds ago 845MB golang latest 8e2ffcb73e11 7 hours ago 839MB iris 1.0 69efff386461 20 hours ago 922MB k8s.gcr.io/kube-proxy v1.19.4 635b36f4d89f 3 weeks ago 118MB k8s.gcr.io/pause 3.2 80d28bedfe5d 9 months ago 683kB nitincypher/docker-ubuntu-python-pip latest a6659c7f1508 2 years ago 922MB ``` 啟動testgo容器: ``` docker run -d -p 8001:8001 testgo:1.0 ``` ``` root@vm2:~/gitlab-cicd/testgolang# docker run -d -p 8001:8001 testgo:1.0 375421dbdf31d245c191c9d2bc381a90619e8943eabd650e0a852218fed371ec ``` 接下來可以做個測試,來測試容器是否可執行: ``` curl 127.0.0.1:8001/hello ``` 結果若長這樣就代表成功了: ``` root@vm2:~/gitlab-cicd/testgolang# curl 192.168.102.138:8001/hello hello chinaase willim ,I running in docker-container and buit by gitlabroot@vm2:~/gitlab-cicd/testgolang# ``` 創建一個ssh-key: ``` cd .ssh cat id_rsa.pub ``` 根據上一週的教學,我們到vm1上進行註冊的動作: ``` gitlab-runner register ``` ``` root@ubuntu:~# gitlab-runner register Runtime platform arch=amd64 os=linux pid=5274 revision=ece86343 version=13.5.0 Running in system-mode. Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): https://gitlab.com/ Please enter the gitlab-ci token for this runner: 2dsV-fWRTxKdCnNE1pzq Please enter the gitlab-ci description for this runner: [ubuntu]: vm2 Please enter the gitlab-ci tags for this runner (comma separated): vm2 Registering runner... succeeded runner=2dsV-fWR Please enter the executor: ssh, virtualbox, kubernetes, custom, docker, parallels, shell, docker-ssh, docker+machine, docker-ssh+machine: shell Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! ``` 而當你註冊完後,到VM2進行上傳的動作,並且確認你的VM1是否有成功自動部署: ``` git add . git commit -m "submit a file." git push -u origin master ``` ``` root@vm2:~/gitlab-cicd/testgolang# git add . root@vm2:~/gitlab-cicd/testgolang# git commit -m "send a file." On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working directory clean root@vm2:~/gitlab-cicd/testgolang# git push -u origin master Branch master set up to track remote branch master from origin. Everything up-to-date root@vm2:~/gitlab-cicd/testgolang# ``` 當出現這樣的結果時,代表你的VM1已經部署完成了: ![](https://i.imgur.com/JjZuuVT.png) 接著到VM1上,去查看容器是否啟動並且進行測試: ``` root@vm1:~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b3e97042c8f5 testgolang "./testgolang" 2 minutes ago Up 2 minutes 0.0.0.0:8001->8001/tcp testgolang b8f4ca51448e iris "/bin/sh -c 'python …" 18 hours ago Up 18 hours 0.0.0.0:5000->5000/tcp iris becebb97850a httpd "httpd-foreground" 23 hours ago Up 23 hours 0.0.0.0:8080->80/tcp laughing_tereshkova root@vm1:~# curl 127.0.0.1:8001/hello hello chinaase willim ,I running in docker-container and buit by gitlabroot@vm1:~# ``` ## docker-compose 創建一個資料夾test-dockercompose: Dockerfile: ``` FROM python ADD . /code WORKDIR /code RUN pip install -r requirements.txt CMD ["python", "app.py"] ``` docker-compose.yml: ``` version: '3' services: web: build: . ports: - "5000:5000" volumes: - .:/code redis: image: "redis:alpine" ``` 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) ``` requirements.txt: ``` flask redis ``` 啟動docker-compose: ``` root@vm2:/home/user/test-dockercompose# docker-compose up -d WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating test-dockercompose_web_1 ... done Creating test-dockercompose_redis_1 ... done ``` 結果你會看到: ``` root@vm2:/home/user/test-dockercompose# docker ps -a | grep test 905f605a7cb8 test-dockercompose_web "python app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp test-dockercompose_web_1 eeae994db869 redis:alpine "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp test-dockercompose_redis_1 root@vm2:/home/user/test-dockercompose# ``` 接著你可以到網頁上進行測試: ![](https://i.imgur.com/ZJe4vOh.png) ## Reference [上課影片](https://drive.google.com/drive/folders/1oM_ejAeSIhGbDGAVBDnVRv4DuoXaDwYw?usp=sharing)