1. 程式人生 > >Docker 在 CentOS 下的安裝、使用

Docker 在 CentOS 下的安裝、使用

本文介紹了 Docker 在 CentOS 環境下的詳細安裝過程,並嘗試執行一個最簡單的 image 來感受下 Docker 的風采。

什麼是 Docker

Docker 是為開發人員和系統管理員用於構建、釋出、並執行分散式應用程式的開放式平臺。該平臺由 Docker 引擎(一個便攜、輕巧的執行時和打包工具) 和 Docker Hub (一個共享應用程式和自動化工作流的雲服務)等組成。Docker 可以使應用程式從元件迅速組裝並消除了開發、質量保證和生產環境之間的摩擦問題。這樣一來,IT部門可以更快地釋出,而這些應用程式不管是執行在膝上型電腦、資料中心的虛擬機器,還是任何的雲,其執行過程和結果都是一致的。

更多有關 Docker 的介紹,可以參閱《簡述 Docker》 一文。

前置條件

使用 uname -r 檢查 kernel 版本

$ uname -r
3.10.0-327.el7.x86_64

建議你使用最新的系統,以為一直的 bug 都會在新的 kernel 釋出中修復。

安裝

yum 方式安裝

1.使用 sudo 或 root 許可權的使用者登入系統。

2.確保你的 yum 是最新的

sudo yum update

3.新增 yum 倉庫

$ sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

注意,只能用[Ctrl]d結束,而不能用[Ctrl]c

4.安裝 Docker 包

$ sudo yum install docker-engine

出現下面錯誤

Loaded plugins: fastestmirror

就是 fastestmirror 外掛錯誤,可以禁用該外掛也可以暫時不理會,這並不影響 docker-engine 的安裝。

禁用該 fastestmirror 外掛

vi /etc/yum/pluginconf.d/fastestmirror.conf
enabled=1改成0

啟動 docker 守護程序

$ sudo  systemctl start docker.service

配置讓 docker 服務隨系統自動啟動

$ sudo chkconfig docker on

驗證 docker 是否安裝成功

$ sudo systemctl enable docker.service

如果出錯:

[[email protected] ~]$ sudo docker run hello-world
[sudo] password for waylau:
Sorry, try again.
[sudo] password for waylau:
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/hello-world/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fhello-world%3Apull&service=registry.docker.io: dial tcp: lookup auth.docker.io on 8.8.8.8:53: no such host.

添加當前使用者waylau到 docker 使用者組

$ sudo usermod -aG docker waylau

並重復多幾次(最好能翻牆,畢竟伺服器在國外)最後成功的介面如下:

[[email protected] ~]$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
Pulling repository docker.io/library/hello-world
975b84d108f1: Pull complete
3f12c794407e: Pull complete
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world: this image was pulled from a legacy registry.  Important: This registry version will not be supported in future versions of docker.

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.
 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 Hub account:
 https://hub.docker.com

For more examples and ideas, visit:
 https://docs.docker.com/userguide/

整個過程是這樣的:

  1. Docker 客戶端連線到 Docker 守護程序;
  2. Docker 守護程序從 Docker Hub 中拉取名為 “hello-world” 的 image(映象);
  3. Docker 守護程式從該 image 中建立新的容器,該容器執行輸出動作,輸出的內容就是上面所看到的;
  4. Docker 守護程式將輸出流到 Docker 客戶端併發送你的終端顯示。

檢視 docker image

可以看到 pull 回來的這個 image 的一個情況,大小不到 1k。

[[email protected] ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              690ed74de00f        4 months ago        960 B

參考