Ubuntu 16.04 And CentOS 7 Install Docker

Ubuntu Install Docker

1.系統要求

64bit操作系統,內核版本至少爲3.10。

爲了讓Docker使用AUFS存儲,推薦安裝linux-image-extra。CentOS使用的是DeviceMapper.
$ sudo apt-get install -y linux-image-extra-$(uname -r)
執行以上安裝命令,報錯如下:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
linux-image-extra-4.10.0-32-generic is already the newest version (4.10.0-32.36~16.04.1).
The following packages were automatically installed and are no longer required:
  linux-headers-4.8.0-36 linux-headers-4.8.0-36-generic linux-image-4.8.0-36-generic linux-image-extra-4.8.0-36-generic
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 178 not upgraded.
3 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up linux-image-extra-4.10.0-32-generic (4.10.0-32.36~16.04.1) ...
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.10.0-32-generic /boot/vmlinuz-4.10.0-32-generic
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.10.0-32-generic /boot/vmlinuz-4.10.0-32-generic
update-initramfs: Generating /boot/initrd.img-4.10.0-32-generic
W: Possible missing firmware /lib/firmware/i915/kbl_dmc_ver1_01.bin for module i915

gzip: stdout: No space left on device
E: mkinitramfs failure find 141 cpio 141 gzip 1
update-initramfs: failed for /boot/initrd.img-4.10.0-32-generic with 1.
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 1
dpkg: error processing package linux-image-extra-4.10.0-32-generic (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of linux-image-generic-hwe-16.04:
 linux-image-generic-hwe-16.04 depends on linux-image-extra-4.10.0-32-generic; however:
  Package linux-image-extra-4.10.0-32-generic is not configured yet.

dpkg: error processing package linux-image-generic-hwe-16.04 (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of linux-generic-hwe-16.04:
 linux-generic-hwe-16.04 depends on linux-image-generic-hwe-16.04 (= 4.10.0.32.34); however:
  Package linux-image-generic-hwe-16.04 is not configured yet.

dpkg: error processing package linux-generic-hwe-16.04 (--configure):
 dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                          No apport report written because the error message indicates its a followup error from a previous failure.
                                       Errors were encountered while processing:
 linux-image-extra-4.10.0-32-generic
 linux-image-generic-hwe-16.04
 linux-generic-hwe-16.04
E: Sub-process /usr/bin/dpkg returned an error code (1)

解決方法:
$ sudo ap-get autoremove

然後重新執行安裝命令:

$ sudo apt-get install -y linux-image-extra-$(uname -r)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
linux-image-extra-4.10.0-32-generic is already the newest version (4.10.0-32.36~16.04.1).
0 upgraded, 0 newly installed, 0 to remove and 178 not upgraded.

2.添加鏡像源

首先安裝apt-transport-https包以支持HTTPS協議:

$ sudo apt-get install -y apt-transport-https
Reading package lists... Done
Building dependency tree       
Reading state information... Done
apt-transport-https is already the newest version (1.2.24).
0 upgraded, 0 newly installed, 0 to remove and 178 not upgraded.

添加源的gpg密鑰

$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
Executing: /tmp/tmp.Ly3pzwSQtG/gpg.1.sh --keyserver
hkp://p80.pool.sks-keyservers.net:80
--recv-keys
58118E89F3A912897C070ADBF76221572C52609D
gpg: requesting key 2C52609D from hkp server p80.pool.sks-keyservers.net
gpg: key 2C52609D: public key "Docker Release Tool (releasedocker) " imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

獲取當前操作系統的代號:

$ lsb_release -c
Codename:   xenial

一般情況下,12.04(LTS)代號爲precise,14.04(LTS)爲trusty,15.04爲ivid,15.10爲wily。
此處爲xenial。

接下來添加Docker的官方apt軟件源,創建文件,並寫入源的地址內容:
$ sudo touch /etc/apt/sources.list.d/docker.list

$ cat /etc/apt/sources.list.d/docker.list
deb https://apt.dockerproject.org/repo ubuntu-xenial main
Ubuntu version Repository
Precise 12.04 deb https://apt.dockerproject.org/repo ubuntu-precise main
Trusty 14.04 deb https://apt.dockerproject.org/repo ubuntu-trusty main
Xenial 16.04 deb https://apt.dockerproject.org/repo ubuntu-xenial main

更新apt軟件包緩存:
$ sudo apt-get update

3.安裝Docker

$ sudo apt-get install -y docker-engine

安裝完成之後查看docker服務狀態:

$ sudo systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled
   Active: active (running) since Fri 2017-08-18 15:11:32 CST; 1min 10s ago
     Docs: https://docs.docker.com
 Main PID: 17393 (dockerd)
   CGroup: /system.slice/docker.service
           ├─17393 /usr/bin/dockerd -H fd://
           └─17422 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-co

Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.514522054+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.514593142+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.514612318+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.515014487+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.667623200+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.721589597+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.888903127+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.888968656+08:00" lev
Aug 18 15:11:32 Guiwang dockerd[17393]: time="2017-08-18T15:11:32.909135681+08:00" lev
Aug 18 15:11:32 Guiwang systemd[1]: Started Docker Application Container Engine.

看到‘Active: active (running)’,說明docker已經啓動。至此,安裝結束。

CentOS 7 Install Docker

首先添加yum軟件源:

sudo tee /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

$ sudo yum update
$ sudo yum install -y docker-engine

對於CentOS 7系統,CentOS-Extras源中已內置Docker,如果已經配置了CentOS-Extras源,可以直接通過上面的yum命令進行安裝。

配置Docker服務

爲了避免使用docker命令都要用特權身份,可以將當前用戶加入安裝中自動創建的docker用戶組,重新登陸後生效:
$ sudo usermod -aG docker USER_NAME
從docker組中移除用戶:
$ sudo gpasswd -d USER_NAME docker

當然,也可以用創建別名的方式來使用docker,在.bashrc文件中追加:

# dkr爲別名名稱,可自行定義
alias dkr='sudo docker'

Ubuntu下,Docker的默認配置文件爲/etc/default/docker, 服務管理腳本爲/etc/init.d/docker

對於CentOS/RedHat,可通過systemctl命令來管理Docker服務。

Linux下Docker代理配置

創建目錄/etc/systemd/system/docker.service.d ,並創建http-proxy.conf文件,輸入以下內容:

[Service]
Environment="HTTP_PROXY=http://proxyServer:PORT"
Environment="HTTPS_PROXY=http://proxyServer:PORT"

刷新配置:
sudo systemctl daemon-reload
重啓docker服務:
sudo systemctl restart docker

检查变量是否加载:
systemctl show docker --property Environment

systemctl show docker | grep Envir*

你可能感兴趣的:(Linux)