20201103 資料卷與網路 (三)

課堂資料

課堂練習

GitLab-CICD

step 1.

本地端建立 Docker Images 並測試是否能正常運作

git clone https://github.com/yangshun2005/gitlab-cicd.git
cd gitlab-cicd/
cd testgolang/
gedit Dockerfile main.go &
docker build -t testgo:1.0 .
docker run -d -p 8001:8001 testgo:1.0
curl [IP 位置]:8001/hello

step 3.

在 GitLab 上新增一個 Project ( test go )

Git global setup

git config --global user.name "Anida-Huang"
git config --global user.email "xiaoji850312@gmail.com"

Push an existing folder

git init
git remote add origin git@gitlab.com:Anida-Huang/testgo.git
git add .
git commit -m "Initial commit"
git push -u origin master

Step 05.

gedit .gitlab-ci.yml
git add .gitlab-ci.yml
git commit -m "submit a modified .gitlab-ci.yml"
git push -u origin master
git add main.go
git commit -m "modified main.go"
git push -u origin master

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

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

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

Last updated

Was this helpful?