Centos7.6 安裝應用docker,docker-compose (個人Centos是用Hyper-V跑的分了8G的內存,阿里雲2G根本跑不起來gitlab)html
爲了保證個人Centos環境乾淨因此個人gitlab與gitlab-runner都是採用docker服務運行,包括後續的runner的工做形式(executor)也是選的docker。mysql
準備工做:linux
拉取鏡像:這步驟耗時挺長的,耐心等待吧(若是這個鏡像沒有了,你能夠去hub.docker.com搜一下對應的鏡像)git
docker pull gitlab/gitlab-ce:latest docker pull gitlab/gitlab-runner:latest docker pull docker:stable docker pull mcr.microsoft.com/dotnet/core/sdk
在Centos服務器上建立docker-compose.yml文件並運行web
docker-compose run -d
gitlab: image: 'gitlab/gitlab-ce:latest' restart: always hostname: '192.168.2.2' environment: GITLAB_OMNIBUS_CONFIG: | external_url 'http://hts92.wicp.vip:8989'#這裏須要更換成你的固定ip或局域網IP地址(我我的作法是用的動態域名。作的端口映射,若是你是內網作demo無所謂) ports: - '8989:8989' volumes: - '/srv/gitlab/config:/etc/gitlab' - '/srv/gitlab/logs:/var/log/gitlab' - '/srv/gitlab/data:/var/opt/gitlab' gitlab-runner: image: 'gitlab/gitlab-runner:latest' container_name: 'gitlab-runner' restart: 'always' volumes: - '/srv/gitlab-runner/confg:/etc/gitlab-runner' - '/var/run/docker.sock:/var/run/docker.sock'
到此 gitlab 與gitlab-runner 已經搭建好了。(第一次登錄時須要你設置root用戶密碼這裏我就不截圖了,由於我已經設置完了。)sql
接下來進入gitlab 新建個項目。項目名隨意,docker
進入剛建好的項目shell
dotnet new webapi -n user.api --no-https
添加鏡像檢測刪除腳本到項目根目錄(後續ci構建腳本會用到,每次重新編譯docker file 時 會幫你刪除掉以前的實例跟鏡像):保存爲check-images.sh 放到項目根目錄api
if [ $(docker ps -a --format {{.Names}} | grep user-api) ] then docker rm -f user-api docker rmi user-api fi
stages: - build - deploy # 構建 build-job: stage: build only: - master cache: untracked: true script: - dotnet restore - dotnet publish -o ./out -c Release artifacts: # 能夠緩存在gitlab的流水線記錄中,供直接下載 expire_in: 30 days paths: - out/ tags: - 01-user-api-builder # 發佈正式 deploy-job: stage: deploy only: - master dependencies: - build-job # 這裏必定要依賴build-job,否則dockerfile裏面的out目錄沒法使用 script: - ls out/ - docker ps - sh ./check-images.sh - docker build -t user-api . # 這裏能夠添加將生成好的image上傳到dockerhub或者docker本地倉庫 ### 若是生成的鏡像須要統一上傳到倉庫管理,則後面的邏輯能夠分離到另一個runner去執行 # 這裏能夠添加從dockerhub或本地倉庫拉取指定鏡像 - docker run -d --name user-api -p 8080:80 user-api tags: - 01-user-api-deploy
建立 Dockerfile文件 放到項目根目錄 (這裏值得注意的是mcr.microsoft.com/dotnet/core/sdk 鏡像名,要跟咱們準備環境時候的鏡像名保持一致,要否則build 時還須要在拉取 浪費時間,固然你可能夠換成runtime環境的。好處就是編譯鏡像小,用我這個編譯鏡像大)緩存
FROM mcr.microsoft.com/dotnet/core/sdk WORKDIR /app COPY out/ /app ENTRYPOINT [ "dotnet", "/app/user.api.dll" ]
以上內容一同傳至 gitlab剛建好的項目
gitlab項目目錄結構以下
找到rnner信息
記得替換吊對應信息。(--url,--registration-toke)
docker exec -it gitlab-runner gitlab-runner register -n \ --url http://hts92.wicp.vip:8989/ \ --registration-token QJiAZYz3KSJyhWfsHKhC \ --executor docker \ --tag-list "01-user-api-builder" \ --description "01-user-api-builder" \ --docker-image "mcr.microsoft.com/dotnet/core/sdk" \
(值得注意的是: --docker-volumes /var/run/docker.sock:/var/run/docker.sock,當時沒有這句話 個人docker實例沒法跟docker容器(docker run docker)自己通信 。這個問題讓我找了進一天的時間)
docker exec -it gitlab-runner gitlab-runner register -n \ --url http://hts92.wicp.vip:8989/ \ --registration-token QJiAZYz3KSJyhWfsHKhC \ --executor docker \
--tag-list "01-user-api-deploy" \ --description "01-user-api-deploy" \ --docker-image "docker:stable" \ --docker-volumes /var/run/docker.sock:/var/run/docker.sock
以下表明runner 已經開始工做了而且執行成功。
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE user-api latest 62eafc3e4bf6 About a minute ago 1.74GB mcr.microsoft.com/dotnet/core/sdk 20190726 3af77ac73731 2 days ago 1.74GB mcr.microsoft.com/dotnet/core/sdk latest 3af77ac73731 2 days ago 1.74GB gitlab/gitlab-runner-helper x86_64-d0b76032 f8d183475601 2 days ago 52.4MB docker stable c4154a2b47a1 4 days ago 216MB mysql/mysql-server latest 12a8d88596c0 4 days ago 294MB gitlab/gitlab-runner latest 4142c6fc05d4 2 weeks ago 410MB gitlab/gitlab-ce latest 15563c211d40 3 weeks ago 1.8GB microsoft/mssql-server-linux latest 314918ddaedf 7 months ago 1.35GB registry 2.3 83139345d017 3 years ago 166MB [root@localhost ~]#
2ced458eea91 user-api "dotnet /app/User.Ap…" 21 seconds ago Up 20 seconds 0.0.0.0:8080->80/tcp user-api cfed5894c526 microsoft/mssql-server-linux "/opt/mssql/bin/sqls…" 3 minutes ago Up 3 minutes 0.0.0.0:1433->1433/tcp sqlserver d713e32ee388 gitlab/gitlab-ce:latest "/assets/wrapper" 3 days ago Up 39 minutes (healthy) 22/tcp, 80/tcp, 443/tcp, 0.0.0.0:8989->8989/tcp gitlab_gitlab_1 e0cf226629d3 registry:2.3 "/bin/registry /etc/…" 3 days ago Up 39 minutes 0.0.0.0:5000->5000/tcp gitlab_registry_1 eab855f64938 gitlab/gitlab-runner:latest "/usr/bin/dumb-init …" 3 days ago Up 39 minutes gitlab-runner
以上容器已經運行成功
(個人Centos虛擬機地址192.168.2.2)
看着幾行代碼搞定,可是因爲第一次作也耗時將近兩天,隨後在作就簡單多了。整理出以上內容給你們分享。 如下爲參考文章。有問題留言。
參考文章
https://www.lizenghai.com/archives/5180.html#Runner
https://docs.gitlab.com/ee/ci/docker/using_docker_build.html
https://www.jianshu.com/p/43ffba076bc9