Centos 7 安裝操做最新版docker-ce

使用非Root用戶前要增長sudo 命令node

卸載舊版本Docker

舊版本的 Docker 稱爲 docker 或者 docker-engine,使用如下命令卸載舊版本:linux

yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

使用yum 安裝docker依賴包

執行如下命令安裝依賴包:
yum install -y yum-utils device-mapper-persistent-data lvm2nginx

配置國內鏡像站

鑑於國內網絡問題,強烈建議使用國內源,官方源請在註釋中查看。
執行下面的命令添加 yum 軟件源:
阿里雲:docker

sudo yum-config-manager     --add-repo     https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

163:json

yum-config-manager     --add-repo     https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo

官方源ubuntu

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安裝Docker CE或者指定版本安裝

若是須要最新版本的 Docker CE 請使用如下命令:centos

sudo yum-config-manager --enable docker-ce-edge
sudo yum-config-manager --enable docker-ce-test

更新 yum 軟件源緩存,並安裝 docker-ce。緩存

yum makecache fast
yum install docker-ce

安裝指定版本的Docker:
yum list docker-ce --showduplicates | sort -r
Centos 7 安裝操做最新版docker-ce安全

yum install docker-ce-18.06.3.ce-3.el7.centos.x86_64bash

注:安裝過程當中的問題:

Centos 7 安裝操做最新版docker-ce
解決方法:
下載container-selinux:
wget http://mirrors.atosworldline.com/public/centos/7/extras/x86_64/Packages/container-selinux-2.68-1.el7.noarch.rpm
安裝container-selinux(使用–nodeps --force跳過依賴檢查):
rpm -ivh container-selinux-2.68-1.el7.noarch.rpm --nodeps --force
而後從新安裝Docker

啓動Docker CE

systemctl enable docker
systemctl start docker

創建Docker用戶組

默認狀況下,docker 命令會使用 Unix socket 與 Docker 引擎通信。而只有 root 用戶和 docker 組的用戶才能夠訪問 Docker 引擎的 Unix socket。出於安全考慮,通常 Linux 系統上不會直接使用 root 用戶。所以,更好地作法是將須要使用 docker 的用戶加入 docker 用戶組。

創建 docker 組:
groupadd docker
將當前用戶加入 docker 組:
usermod -aG docker $USER
退出當前終端並從新登陸,進行以下測試。

# 測試docker是否正確安裝
docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:

  1. The Docker client contacted the Docker daemon.
  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
  3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
  4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/

For more examples and ideas, visit:
https://docs.docker.com/get-started/

鏡像加速

若是在使用過程當中發現拉取 Docker 鏡像十分緩慢,能夠配置 Docker 國內鏡像加速。
添加內核參數
若是在 CentOS 使用 Docker CE 看到下面的這些警告信息:
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
請添加內核配置參數以啓用這些功能。

mkdir -p /etc/docker
tee -a /etc/sysctl.conf <<-EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

而後從新加載 sysctl.conf 便可
sysctl -p

編輯文件/etc/docker/daemon.json 添加如下代碼:

{
    "registry-mirrors": ["https://registry.docker-cn.com","http://hub-mirror.c.163.com"]
}

導入導出命令介紹

涉及的命令有export、import、save、load
Save 命令 :
docker save [options] images [images...]
示例 :
docker save -o nginx.tar nginx:latest

docker save &gt; nginx.tar nginx:latest
其中-o和>表示輸出到文件,nginx.tar爲目標文件,nginx:latest是源鏡像名(name:tag)
Load 命令 :
docker load [options]
示例
docker load -i nginx.tar

docker load &lt; nginx.tar
其中-i和<表示從文件輸入。會成功導入鏡像及相關元數據,包括tag信息
Export 命令 :
docker export [options] container
示例
docker export -o nginx-test.tar nginx-test
其中-o表示輸出到文件,nginx-test.tar爲目標文件,nginx-test是源容器名(name)
import命令:
docker import [options] file|URL|- [REPOSITORY[:TAG]]
示例
docker import nginx-test.tar nginx:imp

cat nginx-test.tar | docker import - nginx:imp
區別
export命令導出的tar文件略小於save命令導出的

export命令是從容器(container)中導出tar文件,而save命令則是從鏡像(images)中導出
基於第二點,export導出的文件再import回去時,沒法保留鏡像全部歷史(即每一層layer信息,不熟悉的能夠去看Dockerfile),不能進行回滾操做;而save是依據鏡像來的,因此導入時能夠完整保留下每一層layer信息。以下圖所示,nginx:latest是save導出load導入的,nginx:imp是export導出import導入的。
建議:
能夠依據具體使用場景來選擇命令
如果只想備份images,使用save、load便可
如果在啓動容器後,容器內容有變化,須要備份,則使用export、import

Docker中如何刪除image(鏡像)

docker中刪除images的命令是docker rmi,但有時候執行此命令並不能刪除images;(確認刪除竟像時鏡像沒有在容器中運行,爲中止狀態)

docker images 查看鏡像

Centos 7 安裝操做最新版docker-ce

docker rmi IMAGE ID
Centos 7 安裝操做最新版docker-ce
docker images 從新查看鏡像列表

發現image並無被刪除,只是他的tag被刪除了,再次執行docker rmi IMAGE_ID只會報錯

docker rmi 53912975086f
Error: image_delete: Conflict, 53912975086f wasn't deleted
2019/07/20 16:16:27 Error: failed to remove one or more images

查看docker的幫助會發現有兩個與刪除有關的命令rm和rmi

rm Remove one or more containers
rmi Remove one or more images

這裏有兩個不一樣的單詞,images和container。其中images很好理解,跟日常使用的虛擬機的鏡像一個意思,至關於一個模版,而container則是images運行時的的狀態。docker對於運行過的image都保留一個狀態(container),可使用命令docker ps來查看正在運行的container,對於已經退出的container,則可使用docker ps -a來查看。 若是你退出了一個container而忘記保存其中的數據,你可使用docker ps -a來找到對應的運行過的container使用docker commit命令將其保存爲image而後運行。
回到以前的問題,因爲image被某個container引用(拿來運行),若是不將這個引用的container銷燬(刪除),那image確定是不能被刪除。
因此想要刪除運行過的images必須首先刪除它的container。繼續來看剛纔的例子,
執行命令 docker ps -a
Centos 7 安裝操做最新版docker-ce

能夠看出53912975086f的image被cc6b344ed27f的container使用着,因此必須首先刪除該container

docker rm cc6b344ed27f
Error: container_delete: Impossible to remove a running container, please stop it first
2019/07/20 16:36:44 Error: failed to remove one or more containers

現錯誤,這是由於該container正在運行中(運行docker ps查看),先將其關閉

docker stop cc6b344ed27f

docker rm cc6b344ed27f

docker rmi 53912975086f

docker images

Centos 7 安裝操做最新版docker-ce能夠看出,image已經被刪除。

相關文章
相關標籤/搜索