CentOS
Docker 運行在CentOS 7.X上。 Docker可以在一個兼容EL7系統(如:Scientific Linux)上安裝成功,但是,Docker 對這些發行版不提供任何的測試和支持。
這個章節會指導你使用Docker-managed發佈包和安裝機制去安裝。使用這個包確保你獲得Docker的最新發行版。如果你希望使用CentOS-managed 包,請查閱Centos的相關文檔。
先決條件
Docker 的安裝,需要一個不管任何版本的64位CentOS系統。並且,你的內核必須要3.10以上,例如,CentOS 7。
檢查你當前系統的內核版本,打開一個終端並執行 uname -r
命令去顯示你的內核版本:
$ uname -r
3.10.0-229.el7.x86_64
最後,建議你全面升級你的系統。務必牢記,給你的系統打補丁以修復內核任何潛在的bug。任何已經報告的內核bug,可能在最新的內核包中已經修復了。
安裝
這裏提供兩種方法去安裝Docker引擎。你可以使用安裝 yum
包管理器進行安裝。或者,你可以使用 curl
連接到 get.docker.com
網站進行安裝。第二種方法執行一個安裝腳本,底層其實也是通過 yum
包管理器進行安裝。
使用 yum 安裝
-
攜帶着
sudo
或root
的權限登陸到你的機器。 -
確保你的當前的yum包都是最新的。
$ sudo yum update
-
添加 yum repo(倉庫)。
$ cat >/etc/yum.repos.d/docker.repo <<-EOF [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7 enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF
-
安裝Docker 包。
$ sudo yum install docker-engine
-
啓動Docker daemon(守護進程)。
$ sudo service docker start
-
通過運行一個測試鏡像到一個容器裏,覈實安裝是否成功。
$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from hello-world a8219747be10: Pull complete 91c95931e552: Already exists hello-world:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security. Digest: sha256:aa03e5d0d5553b4c3473e89c8619cf79df368babd1.7.1cf5daeb82aab55838d 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. (Assuming it was not already locally available.) 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 itto your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash For more examples and ideas, visit: http://docs.docker.com/userguide/
使用腳本安裝
- 攜帶着
sudo
或root
的權限登陸到你的機器。 -
$ sudo yum update
-
執行 Docker 安裝腳本。
$ curl -sSL https://get.docker.com/ | sh
這個腳本添加了
docker.repo
倉庫並安裝Docker。 -
啓動 Docker daemon。
$ sudo service docker start
-
通過運行一個測試鏡像到一個容器裏,覈實安裝是否成功。
$ sudo docker run hello-world
創建一個 docker 組
docker
daemon 綁定到一個 Unix socket 替代一個 TCP 端口。默認情況下,Unix
socket 是歸 root
用戶所有,其他用戶要在命令前面追加 sudo
才能訪問。由此, docker
daemon
總是作爲 root
用戶的專屬。
爲了避免每次使用命令的時候都要追加 sudo
,可以創建一個名爲 docker
的Unix組並將非根用戶添加進去。當 docker
daemon
啓動時,通過 docker
組可以把Unix Socket的讀寫權限共享給組中的用戶。
警告:
docker
組等同root
用戶;勢必會對你的系統注入安全隱患,可以移步到 Docker Daemon 攻擊面 獲取更多的相關信息。
創建 docker
組並添加你的用戶:
-
使用
sudo
權限登陸到你的CentOS中。 -
創建
docker
組並添加你的用戶。sudo usermod -aG docker your_username
-
登出並重新登陸。
這樣保證你的用戶使用正確的權限運行。
-
通過執行不帶
sudo
的docker
命令來覈實上述配置是否生效。$ docker run hello-world
設置 docker daemon 自動啓動
確保 Docker 在你的系統啓動時跟隨西東,可以執行如下指令:
$ sudo chkconfig docker on
如果你需要一個HTTP代理,爲Docker運行時文件設置一個不同的目錄或分區,或者其他的自定義需求,請參考customize your Systemd Docker daemon options。
卸載
你可以使用 yum
刪除Docker。
-
列出你已經安裝的包。
$ yum list installed | grep docker yum list installed | grep docker docker-engine.x86_64 1.7.1-1.el7 @/docker-engine-1.7.1-1.el7.x86_64.rpm
-
移除包。
$ sudo yum -y remove docker-engine.x86_64
這條命令不會移除在你宿主機上的鏡像、容器、數據卷或用戶創建的配置文件。
-
執行如下命令,刪除所有的鏡像、容器、數據卷:
$ rm -rf /var/lib/docker
-
查找並刪除所有用戶創建的配置文件。