一姨伤、鏡像
??Docker 鏡像是 Docker 容器運行時的只讀模板靶瘸,每一個鏡像由一系列的層 (layers) 組成撇吞。Docker運行容器前需要在本地存在對應的鏡像酒甸,如果鏡像不存在本地浮声。Docker會嘗試先從默認鏡像倉庫下載(默認使用Docker Hub公共注冊服務器中的倉庫)虚婿,用戶也可以通過配置,使用自定義的鏡像倉庫泳挥。
- 獲取鏡像
[root@localhost ~]# docker pull ubuntu:13.10
- 手動配置Docker加速器
??在國內訪問 Docker 官方的鏡像然痊,一直以來速度都慢如蝸牛。為了快速訪問 Docker 官方鏡像都會配置三方加速器屉符,目前常用三方加速器有: 網易 剧浸、 USTC 锹引、 DaoCloud 、 阿里云 唆香。
配置Docker加速器的本質就是把Docker配置文件中的鏡像下載地址由默認的Docker Hub地址變?yōu)閲鴥如R像的加速地址嫌变。
阿里開發(fā)者平臺:
https://dev.aliyun.com
阿里開發(fā)者平臺
Docker客戶端版本大于1.10:
- 方法一
國內加速站點
網易
http://hub-mirror.c.163.com
ustc
https://docker.mirrors.ustc.edu.cn
中國科技大學
https://docker.mirrors.ustc.edu.cn
配置鏡像:
[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# vi /etc/docker/daemon.json
# 添加以下內容
{
"registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn","https://docker.mirrors.ustc.edu.cn"]
}
# 重新加載配制:
[root@master ~]# systemctl daemon-reload
# 重新啟動服務:
[root@localhost ~]# systemctl restart docker
重新使用docker pull可以看到速度變快了。
- 方法二:
用systemctl enable docker
啟用服務后躬它,
編輯/etc/systemd/system/multi-user.target.wants/docker.service
文件腾啥,
找到ExecStart=
這一行,在這行最后添加加速器地址–registry-mirror=<加速器地址>
虑凛,如:
ExecStart=/usr/bin/dockerd --registry-mirror= https://pvhlb2dr.mirror.aliyuncs.com
重新加載配置并且重新啟動碑宴。
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
二、鏡像操作
- 搜索鏡像
我們可以從 Docker Hub 網站來搜索鏡像桑谍,Docker Hub 網址為: https://hub.docker.com/
例:我們需要一個httpd的鏡像來作為我們的web服務
# 搜索httpd鏡像
[root@localhost ~]# docker search httpd
# 顯示官方
docker search [包名] --filter "is-official=true"
# 過濾熱度
docker search [包名] --stars=3
- 拖取鏡像
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
8d691f585fa8: Pull complete
8eb779d8bd44: Pull complete
574add29ec5c: Pull complete
9ccffbf4a714: Pull complete
166e14b82905: Pull complete
Digest: sha256:649bd29cc9284f06cf1a99726c4e747a83679e04eea3311b55022dd247026138
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
docker pull mysql:tag
:tag 是你想從官方拉取的版本(例如5.5, 5.6, 5.7 或者latest )延柠。如果省略 tag 則拉取當前最新的版本, 即latest。
- 刪除鏡像
使用鏡像的標簽與ID刪除鏡像
- 停止所有的container锣披,這樣才能夠刪除其中的images:
[root@localhost ~]# docker stop $(docker ps -a -q)
- 使用標簽刪除鏡像
標簽為:repository+tag
# 使用標簽刪除鏡像
[root@localhost ~]# docker rmi docker.io/ubuntu:13.10
- 根據ID刪除:
# 根據ID刪除
[root@localhost ~]# docker rmi 05a3bd381fc2
因為沒有停止運地的鏡像:
可以使用-f強制刪除:(不推薦使用贞间,因為有依賴的話會產生一些遺留問題)
[root@localhost ~]# docker rmi -f 05a3bd381fc2
正確做法是,先刪除依賴該鏡像的所有容器雹仿,再來刪除鏡像增热。
- 刪除所有鏡像:
[root@localhost ~]# docker rmi $(docker images -q)
- 根據格式刪除所有鏡像:
刪除狀態(tài)為 exited的鏡像
[root@localhost ~]# docker rm $(docker ps -qf status=exited)
- 創(chuàng)建鏡像
- 基于已有的鏡像的容器創(chuàng)建
用commit 命令創(chuàng)建鏡像:
該方法是使用docker commit命令,其命令格式為:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
主要參數選項包括:
-a 胧辽,–author=”” 作者信息
-m峻仇,–message=”” 提交信息
-p,–pause=true 提交時暫停容器運行
創(chuàng)建一個新容器命令:
# 創(chuàng)建一個新容器
[root@localhost ~]# docker run -it centos /bin/bash
添加一個文件:
[root@918afd3c90de /]# touch test
[root@918afd3c90de /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys test tmp usr var
# 退出centos容器
[root@918afd3c90de /]# exit
exit
[root@localhost ~]#
commit命令來提交為一個新的鏡像:
[root@localhost ~]# docker commit -m "添加一個新文件" -a "hjj" 918afd3c90de test
sha256:9878c9eaafd5f8bf7baa286f35e0038f5d0ae7ebb7b7fd20ec039a7de61cc18d
命令中邑商,指定了要提交的修改過的容器的ID摄咆、目標鏡像倉庫、鏡像名
人断。commit提交的知識創(chuàng)建容器的鏡像與容器的當前狀態(tài)之間的差異部分吭从,很輕量。
查看新創(chuàng)建的本地鏡像:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test latest 9878c9eaafd5 43 seconds ago 220MB
- 基于本地模板導入
也可以直接從一個操作系統(tǒng)模板文件導入一個鏡像恶迈。
centos安裝wget:
[root@localhost ~]# yum -y install wget
[root@localhost ~]# wget -c http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz
--2019-10-24 21:26:07-- http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz
正在解析主機 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, 2402:f000:1:408:8100::1
正在連接 mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:80... 已連接涩金。
已發(fā)出 HTTP 請求,正在等待回應... 200 OK
長度:10982406 (10M) [application/x-gzip]
正在保存至: “apache-tomcat-9.0.27.tar.gz”
100%[=================================================================================================================================================>] 10,982,406 4.78MB/s 用時 2.2s
2019-10-24 21:26:10 (4.78 MB/s) - 已保存 “apache-tomcat-9.0.27.tar.gz” [10982406/10982406])
[root@localhost ~]# ls
anaconda-ks.cfg apache-tomcat-9.0.27.tar.gz
-c參數為可以斷點續(xù)傳暇仲。
下載一個tomcat:9.0.27的模板壓縮包后步做,可以使用以下命令導入:
[root@localhost ~]# cat apache-tomcat-9.0.27.tar.gz | docker import - tomcat:9.0.27
import: - (自定義的名字)
# 查看鏡像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 9.0.27 41e52d7345b6 About a minute ago 15.5MB
- 基于Dockerfile創(chuàng)建
Dockerfile是一個文本格式的配置文件,用戶可以使用Dockerfile快速創(chuàng)建自定義的鏡像奈附。
Docker 官方并不推薦使用 docker commit 的方法來構建鏡像辆床,而是推薦使用 Dockerfile 定義的文件和 docker build 命令來構建鏡像。Dockerfile 使用基于 DSL 語法的指令來構建一個 Docker 鏡像桅狠,之后使用 docker build命令基于改 Dockerfile 中的指令構建一個新的鏡像讼载。
Dockerfile 內容:
# Version: 0.0.1
FROM ubuntu:latest
MAINTAINER Allan Ma
RUN yum update
RUN yum install -y nginx
RUN echo 'Hi, I am in your container' \
>/usr/share/nginx/html/index.html
EXPOSE 80
1)基鏡像為ubuntu
2)作者設置為我自己的名字
3) 容器安裝nginx,并將Hi, I am in your container 添加到/usr/share/nginx/html/index.html轿秧。
- 運行鏡像的容器監(jiān)聽 80端口
運行,在有Dockerfile 的目錄中執(zhí)行構建:
docker build -t houjianjun/nginx .
(鏡像名稱, 最后一個點代表 dockerfile 存在的目錄 設置為在當前目錄下咨堤,也可以單獨 指定)菇篡。
等待運行完成 使用docker images
查看本地的鏡像, 就可看見自己創(chuàng)建的鏡像了一喘。
然后運行:
docker run -d --name nginx -p 80:80 houjianjun/nginx
(鏡像名稱)
??Dockerfile 由一系列指令和參數組成驱还。每條指令,如 FROM凸克,都必須為大寫字母议蟆,且后面跟隨一個參數:FROM ubuntu:latest。Dockerfile 中的指令會按順序從上到下執(zhí)行萎战,所以應該根據需要合理安排指令的順序咐容。
每條指令都會創(chuàng)建一個新的
鏡像層
并對鏡像進行提交。
Docker 大體上按照如下流程執(zhí)行 Dockerfile 中的指令:
1)Docker 從基礎鏡像運行一個容器蚂维;
2)執(zhí)行一條指令戳粒,對容器做出修改;
3)執(zhí)行類似 docker commit 的操作虫啥,提交一個新的鏡像層蔚约;
4)Docker 再基于剛提交的鏡像運行一個新容器;
5)執(zhí)行 Dockerfile 中的下一條指令涂籽,直到所有指令都執(zhí)行完畢苹祟。
- 導出和載入鏡像
- 導出鏡像
如果要導出鏡像到本地文件,可以使用docker save命令评雌。
例:導出centos鏡像為文件centos_test.tar
# 導出鏡像
[root@localhost ~]# docker save -o centos_test.tar docker.io/centos:latest
- 載入鏡像
# 載入鏡像
[root@localhost ~]# docker load --input centos_test.tar
或:
# 載入鏡像
[root@localhost ~]# docker load<centos_test.tar
- 上傳鏡像
??可以使用docker push命令上傳鏡像到倉庫树枫,默認上傳到DockerHub官方倉庫(需要登錄),命令為:
docker push NAME[:TAG]
用戶在DockerHub
網站注冊后柳骄,即可上傳自制的鏡像团赏。
例如:用戶marquis上傳本地的test:latest鏡像箕般,可以先添加新的標簽marquis/test:latest耐薯,然后用docker push命令上傳鏡像:
[root@localhost ~]# docker tag test:latest marquis/test:latest
[root@localhost ~]# docker push marquis/test:latest
第一次上傳必須登錄驗證。