Docker 之 CentOS 環(huán)境安裝
版本信息
-
docker -v
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker -v Docker version 18.05.0-ce, build f150324
-
uname -a
[root@f7106025b994 /]# uname -a Linux f7106025b994 4.9.93-linuxkit-aufs #1 SMP Wed Jun 6 16:55:56 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
-
本地磁盤掛載(資源文件共享)
docker run -i -t -v ~/software:/mnt/software centos /bin/bash xiazhaoyangdeMacBook-Pro:/ xiazhaoyang$ cd ~/software xiazhaoyangdeMacBook-Pro:software xiazhaoyang$ ls jdk-8u172-linux-x64.tar.gz xiazhaoyangdeMacBook-Pro:software xiazhaoyang$ pwd /Users/xiazhaoyang/software
centos軟件安裝目錄 /opt
安裝jdk
解壓
tar -zxvf /mnt/software/jdk-8u172-linux-x64.tar.gz -C /opt
創(chuàng)建映射
ln -s /opt/jdk1.8.0_172 /opt/jdk
# 測(cè)試映射路徑
ll /opt/jdk
/opt/jdk/bin/java -version
修改環(huán)境變量
vi /etc/profile
# JAVA_ENV_CONFIGURATION
export JAVA_HOME=/opt/jdk
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
激活配置并測(cè)試
source /etc/profile
[root@7941a20a85b6 opt]# java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
鏡像操作
鏡像上傳
# 將本地容器提交為鏡像
xiazhaoyangdeMacBook-Pro:software xiazhaoyang$ docker commit 7941a20a85b6 xiachaoyang/centos-java
sha256:905af87b4a5efcd0d7c5128700393d65f50c4575878cb250fae177c987d87964
xiazhaoyangdeMacBook-Pro:software xiazhaoyang$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiachaoyang/centos-java latest 905af87b4a5e 18 seconds ago 587MB
centos latest 49f7960eb7e4 2 weeks ago 200MB
xiazhaoyangdeMacBook-Pro:software xiazhaoyang$
鏡像驗(yàn)證
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiachaoyang/centos-java latest 905af87b4a5e 10 minutes ago 587MB
centos latest 49f7960eb7e4 2 weeks ago 200MB
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run --rm xiachaoyang/centos-java /opt/jdk/bin/java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$
<font color=red>[備注]</font>
1. 容器通過commit直接生成的鏡像使用`docker run -i -t xiachaoyang/centos-java`啟動(dòng)鏡像后,需要再次執(zhí)行source /etc/profile 才能激活環(huán)境變量
[root@288652cb51f3 /]# java -version
bash: java: command not found
[root@288652cb51f3 /]# source /etc/profile
[root@288652cb51f3 /]# java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
[root@288652cb51f3 /]#
推薦使用Dockerfile創(chuàng)建鏡像
2. 啟動(dòng)鏡像后可以多終端訪問同一運(yùn)行中容器
# 啟動(dòng)一個(gè)實(shí)例
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run -i -t xiachaoyang/centos-java /bin/bash
[root@288652cb51f3 /]#
# 開啟新終端查詢實(shí)例ID并進(jìn)入
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
288652cb51f3 xiachaoyang/centos-java "/bin/bash" 2 minutes ago Up About a minute nifty_bhabha
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker attach 288652cb51f3
[root@288652cb51f3 /]#
# 測(cè)試運(yùn)行中實(shí)例是否有變化
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
288652cb51f3 xiachaoyang/centos-java "/bin/bash" 2 minutes ago Up 2 minutes nifty_bhabha
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$
3. 多個(gè)終端鏈接同一鏡像,退出后整體退出锌蓄。
所有操作都是并行的驾荣,A輸入cmd1,B也同樣出現(xiàn)cmd1
4. 基于centos進(jìn)行的鏡像操作,本地centos是否會(huì)發(fā)生變化帆吻,能否提交覆蓋
不能,推送類似于git操作域那,需要權(quán)限。
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker push centos
The push refers to repository [docker.io/library/centos]
bcc97fbfc9e1: Layer already exists
errors:
denied: requested access to the resource is denied
unauthorized: authentication required
5. 對(duì)于本地庫中鏡像的操作(運(yùn)行容器并修改部分東西)猜煮,能否覆蓋提交次员,能否提交到倉庫同名下败许,還是說需要利用tag進(jìn)行標(biāo)記?
可以覆蓋更新鏡像淑蔚,參考下文:
創(chuàng)建本地鏡像(commit市殷、Dockerfile)
6. 同一鏡像生成的不同ID的容器,互相之間不受影響刹衫,修改在退出后也不會(huì)保存
鏡像推送
創(chuàng)建DockerHub賬號(hào)
創(chuàng)建倉庫
-
推送本地鏡像
非必須醋寝,沒有權(quán)限時(shí)需要登錄
docker login
推送
docker push xiachaoyang/env-jdk
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiachaoyang/env-jdk latest abe21e6640f3 13 hours ago 587MB
centos latest 49f7960eb7e4 2 weeks ago 200MB
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker push xiachaoyang/env-jdk
The push refers to repository [docker.io/xiachaoyang/env-jdk]
f65468761332: Pushed
5ed6bb2dd837: Pushed
bcc97fbfc9e1: Mounted from library/centos
latest: digest: sha256:a7f0324eae38d9ce41389bccd43c92f3fa9d8a1a1dbe5ff52d6be978815fb267 size: 949
-
搜索鏡像
docker search xiaochaoyang/env-jdk
Docker Hub會(huì)定時(shí)對(duì)已上傳的鏡像倉庫進(jìn)行索引,隨后可通過
docker search xiachaoyang/centos-java
進(jìn)行搜索自己推送到Docker Hub的鏡像倉庫
鏡像功能測(cè)試
# 查詢運(yùn)行中容器
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run -i -t xiachaoyang/centos-java /bin/bash
[root@288652cb51f3 /]#
Dockerfile 創(chuàng)建鏡像
xiazhaoyangdeMacBook-Pro:Dockerfile jdk-8u172-linux-x64.tar.gz
xiazhaoyangdeMacBook-Pro:jdk-8u172-linux-x64 xiazhaoyang$ docker build -t xiachaoyang/centos-java .
Sending build context to Docker daemon 190.9MB
Step 1/7 : FROM centos:latest
---> 49f7960eb7e4
Step 2/7 : MAINTAINER "xiachaoyang"<xiazhaoyang@live.com>
---> Using cache
---> c1009b593364
Step 3/7 : ADD jdk-8u172-linux-x64.tar.gz /opt
---> Using cache
---> 9c32f5eacb71
Step 4/7 : RUN ln -s /opt/jdk-8u172-linux-x64 /opt/jdk
---> Running in 23a59c89c7be
Removing intermediate container 23a59c89c7be
---> 99b3d5d9446c
Step 5/7 : ENV JAVA_HOME /opt/jdk
---> Running in f70d6dd4dda2
Removing intermediate container f70d6dd4dda2
---> 685cd05aa266
Step 6/7 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 934a8279744d
Removing intermediate container 934a8279744d
---> 75f551548c7a
Step 7/7 : CMD java -version
---> Running in 9ff68222bc64
Removing intermediate container 9ff68222bc64
---> f1992f5f7b4c
Successfully built f1992f5f7b4c
Successfully tagged xiachaoyang/centos-java:latest
標(biāo)記版本信息
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiachaoyang/centos-java latest f1992f5f7b4c 2 minutes ago 587MB
xiachaoyang/centos-java <none> 905af87b4a5e 10 hours ago 587MB
centos latest 49f7960eb7e4 2 weeks ago 200MB
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker tag 905af87b4a5e xiachaoyang/centos-java:1.0
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiachaoyang/centos-java latest f1992f5f7b4c 3 minutes ago 587MB
xiachaoyang/centos-java 1.0 905af87b4a5e 10 hours ago 587MB
centos latest 49f7960eb7e4 2 weeks ago 200MB
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$
提交(運(yùn)行中容器提交為鏡像)
查看運(yùn)行中容器
docker ps
將當(dāng)前容器提交為一個(gè)新的鏡像
docker commit [容器ID] xaichaoyang/centos-java
查看鏡像
docker images
鏡像刪除
-
容器刪除
dokcer rm [容器ID]
-
容器批量刪除
-f
表示強(qiáng)制刪除docker rm -f $(docker ps -a -q) docker rm -f `docker ps -a -q`
-
鏡像刪除
docker rmi [鏡像ID]
-
批量刪除
docker rmi -f `docker images -a -q`
Dockerfile 踩坑
-
解壓文件名
jdk文件解壓文件名非原tgz的文件名带迟,導(dǎo)致ln命令的符號(hào)鏈接顯示紅色閃爍音羞,是由于文件不存在導(dǎo)致,調(diào)整Dockerfile構(gòu)建文件后可以正常生成
# 運(yùn)行鏡像測(cè)試
xiazhaoyangdeMacBook-Pro:jdk-8u172-linux-x64 xiazhaoyang$ docker run --rm xiachaoyang/env-jdk java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
查看容器列表信息
docker container ls -a
-
批量刪除
docker rm -f
docker ps -a -q
搭建Docker Registry(本地鏡像倉庫)
-
本地搭建
下載并運(yùn)行registry
docker run -d -p 5000:5000 -v ~/docker-registry:/tmp/registry --name local-docker-res --restart=always registry
-d : 表示后臺(tái)啟動(dòng)該容器
-p : 表示對(duì)容器中應(yīng)用程序暴露的端口號(hào)進(jìn)行端口映射邮旷,冒號(hào)左邊的端口(50000)為宿主機(jī)的端口黄选,冒號(hào)右邊的端口(5000)為容器內(nèi)部需要報(bào)錄的端口
-v : 數(shù)據(jù)卷選項(xiàng),表示將宿主機(jī)的~/docker-registry 目錄映射為容器的/tmp/registry
-restart:docker服務(wù)重啟后總是重啟此容器
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run -d -p 50000:5000 -v ~/docker-registry:/tmp/registry --name local_res registry
5c08afe92712746db4fb48e02b5b274e205e7dce3cb38395846ca57b5d387c5e
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c08afe92712 registry "/entrypoint.sh /etc…" 27 seconds ago Up 26 seconds 0.0.0.0:50000->5000/tcp determined_banach
訪問http://127.0.0.1:5000/v2/_catalog?n=100
查看倉庫
> 下載web服務(wù)
docker run -it -p 8000:8080 --restart=always --name local-docker-res-web --link local-docker-res -e REGISTRY_URL=http://local-docker-res:5000/v2 -e REGISTRY_NAME=localhost:5000 hyper/docker-registry-web
`8000:8080` 內(nèi)置web服務(wù)是8080端口的婶肩,將內(nèi)部8080端口映射到本機(jī)的8000端口上办陷。
`REGISTRY_URL` 修改本地倉庫域名
> 運(yùn)行時(shí)遇到的問題
1. 8000:8000,內(nèi)部8080啟動(dòng)失敗:
- 查看運(yùn)行中容器
docker ps
- 關(guān)閉容器
docker stop [容器ID]
- 容器搜索
docker container ls -a
- 刪除容器
docker rm [容器ID]
- 再次運(yùn)行web服務(wù)
2. 倉庫名稱寫錯(cuò)
`I/O error on GET request for "http://registry-srv:5000/v2/_catalog?n=100":registry-srv; nested exception is java.net.UnknownHostException: registry-srv`
參考網(wǎng)上資料修改時(shí)未換成本地自定義倉庫名local-docker-res
修改命令重新執(zhí)行
- 操作時(shí)對(duì)于容器的狀態(tài)還原
批量關(guān)閉容器
docker stop `docker ps -a -q`
批量刪除容器
docker rm -f `docker ps -a -q`
刪除鏡像
docker rmi -f [鏡像ID]
本地倉庫web服務(wù)
-
界面效果
訪問
http://127.0.0.1:8000/
查看倉庫web界面
- 測(cè)試本地倉庫推送
可能出現(xiàn)的push失敗
$ docker push 127.0.0.1:5000/xiachaoyang/env-jdk
The push refers to a repository [127.0.0.1:5000/xiachoayang/env-jdk]
Get https://127.0.0.1:5000/v1/_ping: http: server gave HTTP response to HTTPS client
這是因?yàn)镈ocker在1.3.x之后默認(rèn)docker registry使用的是https律歼,為了解決這個(gè)問題民镜,Linux中修改本地主機(jī)的docker啟動(dòng)配置文件,添加
--insecure-registry 127.0.0.1:5000
Docker for Mac中在圖形化終端上修改如下险毁,填寫后“Apply&Restart”制圈。
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:50000/xiachaoyang/env-jdk latest abe21e6640f3 2 days ago 587MB
xiachaoyang/env-jdk latest abe21e6640f3 2 days ago 587MB
centos latest 49f7960eb7e4 3 weeks ago 200MB
registry latest d1fd7d86a825 5 months ago 33.3MB
hyper/docker-registry-web latest 0db5683824d8 20 months ago 599MB
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker tag abe21e6640f3 127.0.0.1:5000/xiachaoyang/env-jdk
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker push 127.0.0.1:5000/xiachaoyang/env-jdk
The push refers to repository [127.0.0.1:5000/xiachaoyang/env-jdk]
f65468761332: Pushed
5ed6bb2dd837: Pushing [===============> ] 120.4MB/387.4MB
bcc97fbfc9e1: Pushing [=======================> ] 94.75MB/199.7MB
- 查看推送結(jié)果
- 刪除鏡像后測(cè)試本地倉庫
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 49f7960eb7e4 3 weeks ago 200MB
registry latest d1fd7d86a825 5 months ago 33.3MB
hyper/docker-registry-web latest 0db5683824d8 20 months ago 599MB
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker pull 127.0.0.1:5000/xiachaoyang/env-jdk
Using default tag: latest
latest: Pulling from xiachaoyang/env-jdk
7dc0dca2b151: Already exists
71ed27c83409: Pull complete
b7e1a9577e86: Pull complete
Digest: sha256:a7f0324eae38d9ce41389bccd43c92f3fa9d8a1a1dbe5ff52d6be978815fb267
Status: Downloaded newer image for 127.0.0.1:5000/xiachaoyang/env-jdk:latest
- 運(yùn)行容器
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
127.0.0.1:5000/xiachaoyang/env-jdk latest abe21e6640f3 2 days ago 587MB
centos latest 49f7960eb7e4 3 weeks ago 200MB
registry latest d1fd7d86a825 5 months ago 33.3MB
hyper/docker-registry-web latest 0db5683824d8 20 months ago 599MB
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run --rm 127.0.0.1:5000/xiachaoyang/env-jdk java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$
<font color="red">docker restart 【容器ID】重新命令會(huì)保留掛參啟動(dòng)的配置信息 </font>
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b17f2a0c10b hyper/docker-registry-web "start.sh" 23 hours ago Up 23 hours 0.0.0.0:8000->8080/tcp local-docker-res-web
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b17f2a0c10b hyper/docker-registry-web "start.sh" 23 hours ago Up 23 hours 0.0.0.0:8000->8080/tcp local-docker-res-web
353ec8eb8c91 registry "/entrypoint.sh /etc…" 23 hours ago Exited (2) 2 minutes ago local-docker-res
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker restart 353ec8eb8c91
353ec8eb8c91
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b17f2a0c10b hyper/docker-registry-web "start.sh" 23 hours ago Up 23 hours 0.0.0.0:8000->8080/tcp local-docker-res-web
353ec8eb8c91 registry "/entrypoint.sh /etc…" 23 hours ago Up 3 seconds 0.0.0.0:5000->5000/tcp local-docker-res
xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$
-
查看本地磁盤掛載中鏡像
若未找到本地鏡像位置:需了解Docker在Mac中的數(shù)據(jù)卷掛載相關(guān)知識(shí)
鏡像源配置
Mac下修改鏡像源和阿里云鏡像加速
- 阿里云注冊(cè)和購買
- 產(chǎn)品和服務(wù)中選擇容器鏡像服務(wù)
Aliyun提供的Docker for Mac 安裝包下載目錄
如何配置鏡像加速器
右鍵點(diǎn)擊桌面頂欄的 docker 圖標(biāo),選擇 Preferences 畔况,在 Daemon 標(biāo)簽(Docker 17.03 之前版本為 Advanced 標(biāo)簽)下的 Registry mirrors 列表中將https://xxxxx.mirror.aliyuncs.com
加到"registry-mirrors"的數(shù)組里鲸鹦,點(diǎn)擊 Apply & Restart按鈕,等待Docker重啟并應(yīng)用配置的鏡像加速器跷跪。
docker info
# http://hub-mirror.c.163.com/為網(wǎng)易docker鏡像源
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
http://hub-mirror.c.163.com/
https://xxxx.mirror.aliyuncs.com/
Live Restore Enabled: false
配置后可以明顯加快鏡像拉取速度.
REFRENCES
更多
掃碼關(guān)注“架構(gòu)探險(xiǎn)之道”馋嗜,回復(fù)
文章名稱
獲取更多源碼和文章資源
知識(shí)星球(掃碼加入獲取源碼和文章資源鏈接)