CentOS 安裝 Docker
警告:切勿在沒有配置 Docker YUM 源的情況下直接使用 yum 命令安裝 Docker
準備工作
系統要求:
Docker CE 支持 64 位版本 CentOS 7,並且要求內核版本不低於 3.10。 CentOS 7 滿足最低內核的要求,但由於內核版本比較低,部分功能(如 overlay2 存儲層驅動)無法使用,並且部分功能可能不太穩定。
卸載舊版本:
舊版本的 Docker 稱爲 docker 或者 docker-engine ,使用以下命令卸載舊版本:
$ sudo 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 安裝:
執行以下命令安裝依賴包:$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
執行下面的命令添加 yum 軟件源:$ sudo yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
如果需要最新版本的 Docker CE 請使用以下命令:$ sudo yum-config-manager --enable docker-ce-edge
如果需要測試版本的 Docker CE 請使用以下命令:$ sudo yum-config-manager --enable docker-ce-test
安裝 Docker CE
更新 yum 軟件源緩存,並安裝 docker-ce(說明:docker ce爲社區版 docker ee爲企業版):
$ sudo yum makecache fast
$ sudo yum install docker-ce
啓動 Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker
建立 docker 用戶組
默認情況下, docker 命令會使用 Unix socket 與 Docker 引擎通訊。而只有 root 用戶和docker 組的用戶纔可以訪問 Docker 引擎的 Unix socket。出於安全考慮,一般 Linux 系統上不會直接使用 root 用戶。因此,更好地做法是將需要使用 docker 的用戶加入 docker 用戶組。
建立 docker 組:$ sudo groupadd docker
將當前用戶加入 docker 組:$ sudo usermod -aG docker $USER
退出當前終端並重新登錄,進行如下測試。
測試 Docker 是否安裝正確:
[root@work ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for docker.io/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
請添加內核配置參數以啓用這些功能:
$ sudo tee -a /etc/sysctl.conf <<-EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
然後重新加載 sysctl.conf 即可$ sudo sysctl -p
鏡像加速器:
對於使用 systemd 的系統,請在 /etc/docker/daemon.json 中寫入如下內容(如果文件不存在請新建該文件)
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
注意:一定要保證該文件符合 json 規範,否則 Docker 將不能啓動。
之後重新啓動服務:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker