Docker鏡像

4、Docker鏡像(image)

  • docker採用分層構建機制,最底層爲bootfs,次之爲rootfs
    • bootfs: 用於系統引導的文件系統,包括BootLoader和kernel,容器啓動完成後會被卸載以節約內存資源
    • rootfs: 位於bootfs之上,表現爲docker容器的根文件系統,docker採用聯合掛載,每一層都是隻讀的,在最上層有一個可寫層
  • Aufs:高級多層統一文件系統,由Junjiro Okajima開發,早期使用
  • overlayfs & overlay2fs:至3.18版本開始被合併到Linux內核
# docker info |grep "Storage Driver"
Storage Driver: overlay2
  • Docker Registry分類
Sponsor Registry
Mirror Registry
Vendor Registry
Private Registry
  • Docker Hub
Image Repositories  鏡像倉庫
Automated Build  自動構建
Webhooks  能夠將GitHub中的dockerfile自動構建爲鏡像
Organizations  組織
GitHub and Bitbucket integration
  • docker image
build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  • docker image的製做(commit)
  1. 手工組織文件系統,打包成docker image
  2. 基於容器製做,在運行的容器上commit將可寫層製做成image
  3. 基於Dockerfile手動build或者自動構建,Dockerfile基於Base image製做
# docker pull centos:centos7.5.1804
# docker run --name centos -it centos:centos7.5.1804
[root@xxxxxxxxxxx /]# yum install vim wget lftp bash-completion net-tools bind-utils telnet screen tree psmisc bc httpd -y
# docker commit -p centos
# docker image ls
<none>                   <none>              484173886091        22 seconds ago      362MB
# docker tag 484173886091 dongfeimg/mycentos:v0.1
# docker image ls
dongfeimg/mycentos       v0.1                484173886091        5 minutes ago       362MB
# docker run --name mycentos -it dongfeimg/mycentos:v0.1
[root@3c21ac1d0496 /]# mkdir -p /data/html/
[root@3c21ac1d0496 /]# vim /var/www/html/index.html
<h1>Welcome Dongfei website.</h1>
# docker commit -a "Dongfei" -c 'CMD ["/usr/bin/systemctl","start","httpd"]' -p mycentos dongfeimg/mycentos:v0.2
# docker run --name mycentos2 --privileged=true -d dongfeimg/mycentos:v0.2 /usr/sbin/init
[root@docker ~]# docker exec -it 7cec67f74460 bash
[root@7cec67f74460 /]# systemctl start httpd
# docker inspect 7cec67f74460 |grep IPAddress
"IPAddress": "172.17.0.4",
# curl 172.17.0.4
<h1>Welcome Dongfei website.</h1>
  • docker push
# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: dongfeimg
Password: ******
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
# docker push dongfeimg/mycentos:v0.1
# docker push dongfeimg/mycentos:v0.2
  • 鏡像打包和導入
# docker save -o myimages.gz centos:centos7.5.1804 dongfeimg/mycentos:v0.1
# docker load -i myimages.gz
  • 將docker images批量所有打包
# docker save $(docker images | grep -v REPOSITORY | awk 'BEGIN{OFS=":";ORS=" "}{print $1,$2}') -o images_name.gz
相關文章
相關標籤/搜索