1. 程式人生 > >Docker簡介&&安裝

Docker簡介&&安裝

前言

本文整理自:http://www.docker.org.cn/book/docker以及自己進行操作的部分內容。

Docker是一個開源的引擎,可以輕鬆的為任何應用建立一個輕量級的、可移植的、自給自足的容器。開發者在筆記本上編譯測試通過的容器可以批量地在生產環境中部署,包括VMs(虛擬機器)、bare metal、OpenStack叢集和其他的基礎應用平臺。

Docker通常用於如下的場景:

  • web應用的自動化打包和釋出;
  • 自動化測試和持續整合、釋出;
  • 在服務型環境中部署和調整資料庫或其他的後臺應用;
  • 從頭編譯或者擴充套件現有的OpenShift或Cloud Foundry平臺來搭建自己的PaaS環境。

在CentOS7中安裝Docker

依賴性檢查

Docker需要一個64位系統的紅帽系統,核心的版本必須大於3.10。可以用下面的命令來檢查是否滿足docker的要求。CentOS是紅帽系統的開源社群版,所以基本一致。

安裝Docker

方式一

sudo yum update # 確認yum包最新

curl -sSL https://get.docker.com/ | sh # 執行安裝

sudo systemctl start docker.service # 啟動docker

方式二

sudo yum update # 更新yum包

編輯docker.repo的源到/etc/yum.repo.d/

[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg

執行命令如下:

yum clean all
yum makecache
yum install docker-engine

然後啟動服務。

確認安裝完成

配置Docker使用者組

為什麼需要建立docker使用者組?

Docker守候程序繫結的是一個unix socket,而不是TCP埠。這個套接字預設的屬主是root,其他是使用者可以使用sudo命令來訪問這個套接字檔案。因為這個原因,docker服務程序都是以root帳號的身份執行的。

為了避免每次執行docker命令的時候都需要輸入sudo,可以建立一個docker使用者組,並把相應的使用者新增到這個分組裡面。當docker程序啟動的時候,會設定該套接字可以被docker這個分組的使用者讀寫。這樣只要是在docker這個組裡面的使用者就可以直接執行docker命令了。

操作步驟

使用sudo許可權的賬號登入系統,比如:root

建立docker分組,並且將相應的使用者新增到這個分組裡面

sudo usermod -aG docker your_username

退出,重新登入使用者,以便許可權生效。

使用新增到Docker的使用者執行Docker命令。

docker run hello-world

此時出現的結果:

[[email protected] root]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
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服務自啟動

[[email protected] ~]# chkconfig docker on
Note: Forwarding request to 'systemctl enable docker.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

上面是老版本的CentOS,CentOS 7請使用:

systemctl enable docker.service

CentOS刪除Docker

可以使用yum來刪除docker。

列出docker包的具體的名字

$ yum list installed | grep docker
yum list installed | grep docker
docker-engine.x86_64 1.7.1-0.1.el7

刪除docker

$ sudo yum -y remove docker-engine.x86_64 

備註:該命令只是刪除docker執行環境,並不會刪除映象,容器,卷檔案,以及使用者建立的配置檔案。

清除映象和容器檔案

$ rm -rf /var/lib/docker

然後手工查詢並刪除使用者建立的配置檔案。

堅壁清野