UBuntu 16.04下安装Docker(亲测),16.04docker


一、环境

因工作需要,要使用Docker,参考网上(BigManing 和linuxboy易立的博客Docker学习路线图 (持续更新中)) 的安装方法并记录下安装过程。 

安装环境:Ubuntu 16.04

1、查看系统信息

1)Ubuntu查看版本信息

输入lsb_release -a命令查看版本信息

lsb_release -a

当前系统的信息如下:

 

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.5 LTS
Release:    16.04  // 官方16.04LTS系统版本
Codename:   xenial // 16.04版本系统对应的代号 
2)查看当前系统相关信息

命令为:

uname [参数]

示例如下:

$ uname -a  //显示所有信息
Linux BigManing 4.4.0-83-generic #106-Ubuntu SMP Mon Jun 26 17:54:43 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

$ uname -s  //显示内核名称
Linux

$ uname -n  //显示网络节点上的主机名
BigManing

$ uname -r  //显示内核发行号
4.4.0-83-generic

$ uname -v   //显示内核版本号
#106-Ubuntu SMP Mon Jun 26 17:54:43 UTC 2017

$ uname -m   //显示机器硬件名称 显示i686说明你安装了32位操作系统   显示 x86_64说明你安装了64位操作系统
x86_64

$ uname -p   //显示处理器类型
x86_64

$ uname -i   //显示硬件平台
x86_64

$ uname -o   //操作系统
GNU/Linux

 

二、安装Docker(下面方式任选一)

1、使用官方安装脚本自动安装

命令:

sudo curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
安装完成后:
+ sudo -E sh -c 'apt-get install -y -qq --no-install-recommends docker-ce >/dev/null'
+ sudo -E sh -c 'docker version'
Client:
 Version: 18.09.0
 API version: 1.39
 Go version: go1.10.4
 Git commit: 4d60db4
 Built: Wed Nov 7 00:48:57 2018
 OS/Arch: linux/amd64
 Experimental: false

Server: Docker Engine - Community
 Engine:
  Version: 18.09.0
  API version: 1.39 (minimum version 1.12)
  Go version: go1.10.4
  Git commit: 4d60db4
  Built: Wed Nov 7 00:16:44 2018
  OS/Arch: linux/amd64
  Experimental: false
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker cyb

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

增加cyb到DOCKER的用户组

sudo usermod -aG docker cyb

安装校验

sudo docker version

结果如下:

Client:
 Version: 18.09.0
 API version: 1.39
 Go version: go1.10.4
 Git commit: 4d60db4
 Built: Wed Nov 7 00:48:57 2018
 OS/Arch: linux/amd64
 Experimental: false

Server: Docker Engine - Community
 Engine:
  Version: 18.09.0
  API version: 1.39 (minimum version 1.12)
  Go version: go1.10.4
  Git commit: 4d60db4
  Built: Wed Nov 7 00:16:44 2018
  OS/Arch: linux/amd64
  Experimental: false

2、手动安装帮助(未测试)

Ubuntu 14.04 16.04 (使用apt-get进行安装)

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装 Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# apt-cache madison docker-ce
# docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
# Step 2: 安装指定版本的Docker-CE: (VERSION 例如上面的 17.03.1~ce-0~ubuntu-xenial)
# sudo apt-get -y install docker-ce=[VERSION]

三、验证docker是否正确被安装

运行hello world镜像(直接运行即可 自动从服务器上拉取demo镜像)

sudo docker run hello-world

运行时发现没有,自动从服务器上拉取demo镜像

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete 
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
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

更新

sudo apt upgrade

卸载

sudo apt-get purge docker-ce 
sudo rm -rf /var/lib/docker

五、添加到用户组(可选项)

添加到用户组(so easy)

sudo groupadd docker 
sudo usermod -aG docker $USER

注销系统重新进入系统,就可以直接使用docker开头了。(注意:一定要注销后再进系统

sudo service docker restart

1 如果不添加到用户组会发生什么呢? 

如果直接运行:

docker run hello-world

你会发现下面的错误:

 

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'

这是因为:

docker守护程序绑定到Unix套接字而不是TCP端口。默认情况下,Unix套接字由用户root拥有,其他用户只能使用sudo访问它。 docker守护程序始终以root用户身份运行。 如果您不想在使用docker命令时使用sudo,请创建名为docker的Unix组,并将用户添加到该组。当docker守护进程启动时,它会使Docker组的Unix套接字的所有权读/写。

 

参考资料:

对于机器差异化不能安装的同学,请阅读官方文档。

 https://docs.docker.com/engine/installation/linux/ubuntu/#recommended-extra-packages-for-trusty-1404

其他关于旧版本Docker卸载以及测试开发版本Docker安装的帮助,可以参考官方文档的说明进行安装

  • CentOS帮助链接
  • Ubuntu帮助链接
  • Debian帮助链接
  • Fedora帮助链接

相关内容