系統要求
docker版本介紹
docker官方提供2個版本,一個是docker企業版docker-EE,另外一個則是社區版docker-ce,我們在學習或者測試環境使用docker-ce版本即可。
操作系統版本要求
要想在centos上安裝並運行docker,那么你需要centos-7及以上的版本,舊於這個版本的系統將不支持。
其次,你必須啟用centos-extras
存儲庫。默認情況下是開啟的,但如果您禁用了它,則需要重新啟用它。
建議使用overlay2存儲驅動程序。
卸載舊的docker版本
老版本的docker也被叫做:docker或docker-engine。 如果你安裝這些版本,那么請卸載它,以及它的依賴項目:
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
如果yum反饋這些包都沒有安裝,也沒有關系,忽略它;
安裝docker-ce
docker的安裝完成后,所有的內容都保存在/var/lib/docker
目錄下,保存了和docke
r相關的images, containers, volumes, networks
等信息,Docker Engine - Community
的包叫做docker-ce
安裝docker-ce有很多種方法:
- 選擇使用docker的repositories來安裝docker,這樣可以簡化安裝和升級任務。比較推薦,這是大多人的選擇
- PRM包安裝,下載RPM包手動安裝,並完全手動管理升級。在
no access to the internet
的環境下是不錯的選擇 - 自動化腳本,通常在測試環境和開發環境,會選擇使用自動化的腳本來安裝docker
設置yum倉庫
要使用docker的repo來安裝docker,我們首先需要安裝yum-utils
,device-mapper-persistent-data
,lvm2
。其中yum-utils
提供了 yum-config-manager
功能,后2者用於服務設備映射存儲驅動程序。
$ sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
添加yum倉庫
$ sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
啟動docker-nightly版本,docker-ce提供了3種方式用於docker的更新,穩定,測試和夜間(官網原文):
- Stable gives you latest releases for general availability.
- Test gives pre-releases that are ready for testing before general availability.
- Nightly gives you latest builds of work in progress for the next major release.
# 啟動nightly更新方式
$ sudo yum-config-manager --enable docker-ce-nightly
# 禁用
$ sudo yum-config-manager --disable docker-ce-nightly
安裝docker
安裝最新版本:
$ sudo yum install docker-ce docker-ce-cli containerd.io
如果你不想安裝最新版本,那么可以查看倉庫中包含哪些版本,選擇特定的版本進行安裝:
$ yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, priorities
Installed Packages
docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.2-3.el7 @docker-ce-stable
docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable
...
# 選擇特定版本安裝
$ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
啟動docker服務
$ systectm star docker
驗證docker服務是否安裝成功,運行系統提供的一個鏡像,輸出Hello from Docker!
,容器啟動過程可能較慢,請耐心等待。
$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00
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 Hub 拉取鏡像有時會遇到困難,此時可以配置鏡像加速器。國內很多雲服務商都提供了國內加速器服務,例如:
說明:
由於鏡像服務可能出現宕機,建議同時配置多個鏡像。
國內各大雲服務商均提供了 Docker 鏡像加速服務,建議根據運行 Docker 的雲平台選擇對應的鏡像加速服務,具體請參考官方文檔。
下面使用Azure的鏡像來說明怎么配置:系統為centos:
~] cat /etc/docker/daemon.json
{
"registry-mirrors": [
"https://dockerhub.azk8s.cn",
"https://reg-mirror.qiniu.com"
]
}
之后重新啟動服務。
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
卸載docker
有可能有一天你不想再運行docker服務了,或者該主機有別的用途,不想docker占用主機資源,那么你可以卸載docker服務。
# stop docker
$ systemctl stop docker
# Uninstall the Docker package
$ systemctl remove docker
# 刪除主機上的映像、容器、卷或自定義配置文件
$ sudo rm -rf /var/lib/docker