使用 CentOS 官方提供的 Docker image,是可以匯出成 *.tar 或 *.tar.gz 的離線檔案,除了可以離線使用外,還可以自行修改 Docker image 的內容。
以下是在 CentOS 7.0 64 位元
安裝 docker RPM 套件
yum install docker –y
啟動 docker 服務
systemctl start docker
systemctl enable docker
觀察 docker 的 information
docker info
搜尋 centos 的 image
docker search centos
下載 CentOS 官方的 Docker image
docker pull ‘docker.io/centos’
確認 CentOS 官方 Docker Image 是否下載成功?
docker images
直接將 CentOS 官方的 Docker Image 完全不修改就直接轉出,安裝 docker-registry RPM 套件
yum install docker-registry -y
啟動服務
systemctl start docker-registry
systemctl enable docker-registry
設定 Firewall
firewall-cmd –permanent –add-port=5000/tcp
firewall-cmd –reload
將 CentOS 官方 Docker Image,匯出成檔案
docker save –output=/tmp/centos7.tar docker.io/centos
【docker.io/centos】是透過 docker images 茶到的 image 名稱
使用 gzip 壓縮,變成 tar ball file
cd /tmp
gzip /tmp/centos7.tar
MD5 紀錄一下
md5sum /tmp/centos7.tar.gz
找一台 RHEL 7.0 測試一下
yum install docker -y
因為 RHEL 7.0 的 bug,必須要停用 firewalld 服務
systemctl stop firewalld
systemctl disable firewalld
參考 bugzilla ID:1098281
啟動 RHEL 7.0 的 docker 服務
systemctl start docker
systemctl enable docker
觀察 RHEL 7.0 docker 的 information
其實 RHEL 7.0 的 docker,還是可以搜尋 https://hub.docker.com 上的 Docker Image,跟剛剛在 CentOS 7.0 找到的是一模一樣!
這裡要測試剛剛匯出的 Docker Image,就不抓 Online 的 image 了
看看有沒有跟剛剛在 CentOS 7.0 上看到的一樣呢?
docker load -i /tmp/centos7.tar.gz
使用剛剛匯入的 Image,產生一個 Container
顯示 Container
Container ID 是隨機產生的【6b972edc676a】,名稱也是隨機產生的【jovial_hawking0】
確認在本機沒 /tmp/helloworld.txt,只有在剛剛產生的 Container 才有【Container是紀錄差異】
在 CentOS 的 Docker Image 安裝 httpd
docker run -i ‘docker.io/centos’ bash -c “yum install httpd -y"
Container 是紀錄差異,看看是不是還是只有一個 Image,但是,多出一個 Container?
docker images
docker ps -a
將剛剛安裝 httpd 的 Container,Commit 成一個新的 image
docker commit -m “CentOS7 + httpd" sleepy_brown5 centos7_httpd
【sleepy_brown5】是剛剛安裝 httpd 的 image 名稱
【centos7_httpd】是自己指定的新 Image 名稱,只能使用小寫+數字,不可以使用大寫命名
如果沒有要匯出新的 Image,幹嘛要 Commit 新的 Image 呢?因為啟動 httpd 需要指定 Image,而不是指定 Container,所以,為了要啟動 httpd,必須要先 Commit 一個有包含 httpd 的新 Image
docker run -p 8080:80 -d centos7_httpd /usr/sbin/httpd -DFOREGROUND
開 Firefox 看比較清楚,是 CentOS Docker Container 提供的網頁,並不是 RHEL 7.0 的 WWW
現在總共有以下 Container
正常停止 Container
docker stop dd1c9bcc5354
docker kill dd1c9bcc5354【送出 kill 訊號【signal】】
停止 Container 之後,才可以移除 Container,Container是紀錄差異,當然就表示,剛剛紀錄的安裝 httpd 就消失不見了~~~
docker rm dd1c9bcc5354
centos7_httpd 這個 Docker Image,已經沒有任何的 Container 在使用,這時就可以移除 Docker Image
多謝!