什麼是docker?node
Docker 是一個開源的應用容器引擎,讓開發者能夠打包他們的應用以及依賴包到一個可移植的容器中,而後發佈到任何流行的 Linux 機器上,也能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口。linux
再具體的請自行百度~git
命令是紅字,配置是綠字,註釋和其餘爲黑色字體。如今讓咱們來安裝吧docker
服務器主機名及IP地址:vim
192.168.110.92 docker-registry服務器
192.168.110.22 docker-gitlabcurl
系統版本:tcp
CentOS Linux release 7.2.1511 (Core)gitlab
docker版本:測試
Docker version 1.12.6, build c4618fb/1.12.6
docker倉庫版本:
registry-2.4.1
docker認證版本:
docker_auth:1
基礎優化~略
關閉selinux:
sed -i s#'SELINUX=enforcing'#'SELINUX=disabled'#g /etc/selinux/config
setenforce 0
關閉防火牆:
systemctl stop firewalld
systemctl disable firewalld
安裝源:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安裝並啓動docker:
yum install docker docker-registry -y
systemctl enable docker
systemctl start docker
私有鏡像庫和認證搭建:
下載鏡像:
docker pull registry
docker pull docker_auth:1
打標記:
docker tag registry 192.168.110.92:5000/registry:2.4.1
docker tag docker_auth:1 192.168.110.92:5000/docker_auth
修改docker文件 加一行--insecure-registry 192.168.110.92:5000
vim /etc/sysconfig/docker
# /etc/sysconfig/docker
# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --insecure-registry 192.168.110.92:5000'
if [ -z "${DOCKER_CERT_PATH}" ]; then
DOCKER_CERT_PATH=/etc/docker
fi
建立目錄並進入:
mkidr /data/auth_server/ssl/ -p ##用於存放證書
mkidr /data/auth_server/config/ -p ##配置文件
cd /data/auth_server/ssl/
證書生成(server.key,server.pem和server.crt):
openssl genrsa -out server.key 2048
openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 3650 -out server.pem
cat server.pem | tee -a server.crt
拷貝:
scp server.crt 192.168.110.22:/etc/docker/certs.d/ ###scp 到其餘服務器用於測試從內部倉庫下載
如今建立配置文件:
cd /data/auth_server/config
vi auth_config.yml
server:
addr: ":5001"
certificate: "/ssl/server.pem"
key: "/ssl/server.key"
token:
issuer: "Auth Service" # Must match issuer in the Registry config.
expiration: 900
users:
# Password is specified as a BCrypt hash. Use htpasswd -B to generate.
"admin":
password: "$2y$05$LO.vzwpWC5LZGqThvEfznu8qhb5SGqvBSWY1J3yZ4AxtMRZ3kN5jC" #badmin
"test":
password: "123"
acl:
- match: {account: "admin"}
actions: ["*"]
comment: "Admin has full access to everything."
- match: {account: ""}
actions: ["pull"]
comment: "User \"user\" can pull stuff."
回到家目錄建立docker-compose配置文件
cd && vi docker-compose.yml
dockerauth:
image: cesanta/docker_auth:1
ports:
- "5001:5001"
volumes:
- /data/auth_server/config:/config:ro
- /var/log/docker_auth:/logs
- /data/auth_server/ssl:/ssl
command: /config/auth_config.yml
restart: always
registry:
image: registry:2.4.1
ports:
- "5000:5000"
volumes:
- /data/auth_server/ssl:/ssl
- /data/docker_registry/data:/var/lib/registry
- /data/auth_server/config:/auth
- /data/auth_server/ssl:/certs
restart: always
environment:
- REGISTRY_HTTP_TLS_CERTIFICATE=/certs/server.crt
- REGISTRY_HTTP_TLS_KEY=/certs/server.key
- REGISTRY_AUTH=token
- REGISTRY_AUTH_TOKEN_REALM=https://192.168.110.92:5001/auth ###本機ip
- REGISTRY_AUTH_TOKEN_SERVICE="Docker registry"
- REGISTRY_AUTH_TOKEN_ISSUER="Auth Service"
- REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE=/certs/server.pem
啓動並查看:
docker-compose up -d #-d後臺啓動
docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------
root_dockerauth_1 /docker_auth/auth_server / ... Up 0.0.0.0:5001->5001/tcp
root_registry_1 /bin/registry serve /etc/d ... Up 0.0.0.0:5000->5000/tcp
上傳打完包的鏡像到私有倉庫:
for n in `docker images |grep 192|awk '{print $1":"$2}'` ;do docker push $n;done
驗證鏡像是否在鏡像庫(沒有404就行):
for YZ in `docker images|awk -F "[/ ]+" '{print $2}'|grep -v TAG`;do curl -v -X GET http://192.168.110.92:5000/v2/$YZ/tags/list ;done
測試:
192.168.110.22服務器執行:
docker login 192.168.110.92:5000
帳號: admin #auth_config.yml 配置的
密碼: badmin #auth_config.yml配置的
Login Succeeded ##說明成功
docker pull 192.168.110.92:5000/docker_auth #不出意外的話應該是飛快的速度
網上查資料+同事幫忙+自我實踐,才弄出來,有問題及時聯繫我~