1. 程式人生 > >Cento系統下docker的安裝與解除安裝

Cento系統下docker的安裝與解除安裝

Docker簡介

Docker 是一個開源的應用容器引擎,讓開發者可以打包他們的應用以及依賴包到一個可移植的容器中,然後釋出到任何流行的 Linux 機器上,也可以實現虛擬化。容器是完全使用沙箱機制,相互之間不會有任何介面(類似 iPhone 的 app)。幾乎沒有效能開銷,可以很容易地在機器和資料中心中執行。最重要的是,他們不依賴於任何語言、框架包括系統。 
百度百科

前提

首先,你的Centos的系統一定要是64位的,不管版本是什麼。並且核心版本至少是3.10以上。 
用以下命令去檢視你的核心版本:

$ uname -r
3.10.0-229.el7.x86_64
  • 1
  • 2
  • 1
  • 2

最後,建議你更新你的系統,因為最新核心可能會修復了舊版本的一些bug。

安裝

yum安裝

用具有sudo或者root許可權的使用者登入系統。

確保你的yum包已經更新

$ sudo yum update
  • 1
  • 1

新增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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

安裝Docker包

$ sudo yum install docker-engine
  • 1
  • 1
  • 開啟docker deamon
$ sudo service docker start
  • 1
  • 1

驗證docker是否成功安裝

$ 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 it to 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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

開機自啟動

$ sudo chkconfig docker on
  • 1
  • 1

解除安裝

列出你安裝過的包

$ 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
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

刪除安裝包

$ sudo yum -y remove docker-engine.x86_64
  • 1
  • 1

刪除映象/容器等

$ rm -rf /var/lib/docker

Centos7 開啟防火牆或關閉防火牆(這一步很重要,否則本機的容器繫結主機埠提供服務時,本機的其它容器無法訪問該服務)

sudo firewall-cmd --permanent --zone=trusted --add-interface=docker0
sudo firewall-cmd --permanent --zone=trusted --add-port=xxxx/tcp# xxxx改為你希望的埠號
sudo firewall-cmd --reload

關閉firewall:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall開機啟動
firewall-cmd --state #檢視預設防火牆狀態(關閉後顯示notrunning,開啟後顯示running)

iptables防火牆(這裡iptables已經安裝,下面進行配置) vi/etc/sysconfig/iptables #編輯防火牆配置檔案 # sampleconfiguration for iptables service # you can edit thismanually or use system-config-firewall # please do not askus to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT[0:0] :OUTPUT ACCEPT[0:0] -A INPUT -m state--state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -jACCEPT -A INPUT -i lo -jACCEPT -A INPUT -p tcp -mstate --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -jACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080-j ACCEPT -A INPUT -j REJECT--reject-with icmp-host-prohibited -A FORWARD -jREJECT --reject-with icmp-host-prohibited COMMIT :wq! #儲存退出

備註:這裡使用80和8080埠為例。***部分一般新增到“-A INPUT -p tcp -m state --state NEW -m tcp--dport 22 -j ACCEPT”行的上面或者下面,切記不要新增到最後一行,否則防火牆重啟後不生效。 systemctlrestart iptables.service #最後重啟防火牆使配置生效 systemctlenable iptables.service #設定防火牆開機啟動