天天看點

Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)

自建DockerHub

        • Docker
          • 1. docker 安裝
          • 2. docker 管理工具:
            • 1.portainer如圖:
            • 2.portainer安裝
            • 3.portainer密碼: 123456
          • 3. dockerHub registry2搭建:
            • 1.registry2 添加
            • 2.registry2 安裝
            • 3.registry2 解釋
            • 4.registry2 登入
          • 4. docker遷移:
            • 1.将運作中的容器打包并上傳
            • 2.或者使用potainer管理工具上傳
            • 3.另一台機器拉取運作鏡像
            • 4.Dockerfile
            • 5.部分問題跟進

Docker

Docker官網

1. docker 安裝
  1. ubuntu( 如果是桌面版, 也可以手動添加倉庫位址)
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
           
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
           
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
           
sudo apt-get install docker-ce docker-ce-cli containerd.io
           
  1. centos
sudo yum install -y yum-utils

sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce docker-ce-cli containerd.io
           
  1. macos
    Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)
2. docker 管理工具:

docker有自己的管理工具和api,哪個工具都行:

docker本地api

,

tls遠端api

siwam

,

kitematic

,

docker-app

,

k8s

我這裡用portainer-ce

1.portainer如圖:

Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)

2.portainer安裝

ocker run -d -p 9000:9000 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /root/intbird/docker/portainer:/data \
--restart always \
portainer/portainer-ce \
--admin-password='$2a$10$752FJIPGCGYal52wccmoj.LY00lGmTnDxSmjeI48a.K6abTL.Zbj6'
           

3.portainer密碼: 123456

進去以後改個密碼

Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)
3. dockerHub registry2搭建:

1.registry2 添加

Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)

2.registry2 安裝

docker run -d \
  -p 5000:5000 \
  --restart=always \
  --name registry2 \
  -v /root/intbird/docker/registry2/auth:/auth \
  -v /root/intbird/docker/registry2/certs:/certs \
  -v /root/intbird/docker/registry2/data:/var/lib/registry \
  -e "REGISTRY_AUTH=htpasswd" \
  -e "REGISTRY_AUTH_HTPASSWD_REALM=intbird" \
  -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd.config \
  -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry.pem \
  -e REGISTRY_HTTP_TLS_KEY=/certs/registry.key \
  registry:2
           

3.registry2 解釋

/auth: 授權檔案所在目錄

/certs: registry2 https的證書位置

/var/lib/registry : registry2倉庫位置

注意: 1.REALM 為 intbird

即: 登入的使用者名

注意: 2. htpasswd

htpasswd: 認證類型

可以使用docker工具(xmartlabs/htpasswd)生成

docker run --rm -ti xmartlabs/htpasswd intbird $passwd@ > htpasswd.config
           

4.registry2 登入

就用預設的5000端口, 443端口留作他用

docker login registry.intbird.net
           
Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)

所需檔案如圖:

Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)
4. docker遷移:

兩種push方法都是一樣的,選一個用即可

1.将運作中的容器打包并上傳

docker commit -p intbird-nodejs intbird-nodejs-snapshot
docker tag intbird-nodejs-snapshot https://registry.intbird.net/intbird-nodejs-snapshot
docker push https://registry.intbird.net/intbird-nodejs-snapshot 
           

2.或者使用potainer管理工具上傳

  1. 打包上傳
    Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)
  2. 密碼正确則上傳成功
    Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)
  3. 檢查倉庫檔案
    Ubuntu: Docker初級使用之工具篇(potrainer管理工具 + registry2私有倉庫 + Dockerfile)

3.另一台機器拉取運作鏡像

docker login
intbird/$passwd
docker pull https://registry.intbird.net/intbird-nodejs-snapshot:latest
           

4.Dockerfile

關于build産物優化體積

仁者見仁,智者見智

FROM node:14.15.0-alpine

WORKDIR /data/

COPY package*.json ./
COPY ./node .

RUN npm install

EXPOSE 8081
CMD [ "node", "node/node-dev.js" ]

# docker build -t intbird.net/nodejs .
# docker run -p 8081:8081 -d intbird.net/nodejs
           
docker build -t intbird/intbird-nodejs .
docker run -p 8081:8081 -d intbird/intbird-nodejs
           

5.部分問題跟進

  1. 兩個常見的參數

    --restart always

    : 相當于守護程序

    --net-alias=intbird-nodejs

    : 允許别名通路同一網絡
  2. 提示磁盤空間占滿等

    嘗試了部分方法,效果我覺得不太理想

    最終确定為docker的一個版本有bug導緻

  3. 清理資源
docker stats

docker system prune
docker system prune -a

docker system prune --volumes
docker network prune
docker volume prune
docker container prune
docker image prune -a

           

更多多待後續跟進嘗試後繼續…

個人網站: https://intbird.net

文章來自: http://blog.csdn.net/intbird 轉載請說明出處