Try   HackMD

在Ubuntu裝Docker+Gitlab


安裝Docker

  1. 安裝指令
sudo apt-get install docker.io
  1. 安裝後,查看docker有沒有正常啟動
service docker status

正常的話,應該可以看到綠色的狀態:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 接著將自己的使用者帳號加入至 docker 群組,登出再重新登入之後,就可以開始使用 Docker 了。
sudo usermod -aG docker yourID

首先查看 Docker 的版本資訊

docker version

正常來說輸出會類似這樣

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如果最下方出現Cannot connect to the Docker daemon. Is the docker daemon running on this host?,就表示有問題,通常是因為帳號沒有加入 docker 群組,權限不足的因素,只要按照上述的方式將帳號加入 docker 群組即可解決。


在Docker安裝Gitlab

  1. 我們可以利用 docker 的 search 指令來尋找自己需要的 container 映像檔,例如我要找gitlab
docker search gitlab

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如上圖,我們先下載NAME是gitlab/gitlab-ce的image檔

docker pull gitlab/gitlab-ce

下載完,可以用下方的指令列出所有的image檔

docker images -a

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 新建並啟用 Container
  • detach: 在背景執行一個 Container
  • public: 設定一個 port 來做 docker 裡頭的 web 接口。這邊我們把外部的port 8080設定給Container的port 80、外部的port 8443設定給container的port 443、外部的port 8022設定給container的port 22
  • name: 自訂一個 Container 的名稱,這邊我們叫gitlab
docker run --detach --publish 8080:80 --publish 8443:443 --publish 8022:22 --name gitlab gitlab/gitlab-ce

第一次執行Container通常要一段時間(約十分鐘),gitlab才會完全啟動完畢。我們可以用指令看一下所有Container的執行情形

docker ps

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

因為我們剛剛設定了public 8080:80,代表我們在瀏覽器要輸入http://IP:8080去連接到GitLab首頁

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

然後,我們需要知道預設的root密碼是什麼。這個部份在Gitlab的文件有提示

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

應該要執行指令去獲得目前的root預設密碼(這個密碼每次GitLab重啟都會刷新)

sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password

然後在首頁用root+預設密碼去登入,再去重設自己的密碼即可。

開啟Docker的Gitlab容器的HTTPS

基本觀念跟自己寫的GilLab 14 設定HTTPS差不多,先參考Gitlab官方文件

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 執行官方文件第一個指令
  • 進入docker裡gitlab的ssh(登入身份是root)
sudo docker exec -it gitlab /bin/bash
  1. 去處理OpenSSL自簽憑證

  2. 先斷開步驟1的ssh連線

exit
  1. 接著用官方文件第二個指令,去執行GilLab 14 設定HTTPS的第1步
  • 用docker的text editor去編輯/etc/gitlab/gitlab.rb
sudo docker exec -it gitlab editor /etc/gitlab/gitlab.rb
  • 這裡我選擇第二個指令去編輯/etc/gitlab/gitlab.rb,因為之前用第一個指今後發現gitlab沒有vim可以用,然後用apt install vim後,gitlab就開始出一堆怪問題了‥
  1. GilLab 14 設定HTTPS的第2~4步,再回到用上面Gitlab官方文件的第一個指令去處理。
  • 進入docker裡gitlab的ssh(登入身份是root)
sudo docker exec -it gitlab /bin/bash
  1. 用docker指令重啟Container的gitlab
docker restart gitlab
  • 目前用 restart 去重啟gitlab時,會發現gitlab重啟沒1分鐘就自動停掉了,然後要再重新 start 一次,原因不明‥‥請善用指定檢查gitlab有沒有啟動成功
docker ps

Docker的GitLab設定SSH

因為我們用docker安裝gitlab的container時,把外部的port 8022指給gitlab的port 22,所以在用Git UI設定SSH時,也要變更為port 8022。

以TortoiseGit為例,假設我們在Gitlab取得的ssh url如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

那麼,在TortoiseGit裡,就要把原來的ssh url

git@192.168.88.228:yang/tools.git

改成如下

ssh://git@192.168.88.228:8022/yang/tools.git

TortoiseGit最後設定面如下

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


參考文件

  • Ubuntu Linux 安裝 Docker 步驟與使用教學

https://blog.gtwang.org/virtualization/ubuntu-linux-install-docker-tutorial/

  • How to Remove Images and Containers in Docker

https://www.freecodecamp.org/news/how-to-remove-images-in-docker/

  • 簡易教學 — 利用 Docker 架設 Gitlab

https://medium.com/appworks-school/簡易教學-利用-docker-架設-gitlab-b392f75584dd

  • 如何用 SSH 連進遠端主機內的 docker container?

https://jimmylab.wordpress.com/2018/12/05/ssh-docker-container/

  • DOCKER 移除 CONTAINERS、IMAGES、VOLUMES 與 NETWORKS 的方法

https://blog.clarence.tw/2019/09/10/docker-removing-containers-images-volumes-and-networks/

  • GitLab官方文件_Docker

https://docs.gitlab.com/ee/install/docker.html

  • TortoiseGit change default port 22

https://stackoverflow.com/questions/29038153/tortoisegit-change-default-port-22

  • DOCKER 的 MEMORY 與 CPU 資源管理

https://hoohoo.top/blog/docker-resource-memory-and-cpu-setting/