Ubuntu16.04 安装 Docker 及 docker-compose

一、卸载旧版本的 Docker

旧版本的 Docker 被称作 docker 或者 docker-engine,Docker CE(社区版)包现在被叫做 docker-ce。如果之前安装过了,需要先卸载:

sudo apt-get remove docker docker-engine docker.io

二、使用存储库安装 Docker

1.设置存储库:
(1).更新 apt 安装包索引:
sudo apt-get update
(2).安装软件包以允许 apt 通过 HTTPS 使用存储库:
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
(3).添加 Docker 官方的 GPG 密钥:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

确保现在系统已经拥有密钥指纹的后八个字符串:9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
输入指令:

sudo apt-key fingerprint 0EBFCD88

显示结果:

pub   4096R/0EBFCD88 2017-02-22
      密钥指纹 = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) <docker@docker.com>
sub   4096R/F273FCD8 2017-02-22
(4).安装稳定版仓库:
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
2.安装 Docker CE 版:
(1).更新 apt 安装包索引:
sudo apt-get update
(2).安装最新版的 Docker CE:
sudo apt-get install docker-ce
(3).如果不想安装最新版的 Docker,可以先查看可安装版本:
apt-cache madison docker-ce

部分输出结果:

docker-ce | 17.12.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages  
docker-ce | 17.09.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages  
……  
docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages  
……  

列表的内容取决于启用了哪个存储库。第二列是Docker版本号。第三列是存储库名称,它指明了软件包来自哪个存储存储库,并通过扩展其稳定性级别。要安装特定版本,需要将本本字符串附加到包名称。

安装指令如下:

sudo apt-get install docker-ce=<VERSION>

然后 Docker 守护进程就会自动启动安装了。

(4).通过运行 hello-world 镜像验证 Docker CE 已被正确安装:
sudo docker run hello-world

这个命令下载一个测试图像并在容器中运行。容器运行时,会打印一条信息消息并退出。
Notice:这个时候可能会出现无法连接的情况,这是由于国内访问 Docker Hub 不稳定。我们可以注册一个阿里云账户,获得一个专属免费的加速器地址(传送门)。然后运用下面的命令配置我们的镜像加速器:

sudo mkdir -p /etc/docker
sudo vim /etc/docker/daemon.json

将以下内容写入文本:

{  
"registry-mirrors": ["自己的镜像地址"]
} 

输入以下命令后注销并重新登录:

sudo systemctl daemon-reload
sudo systemctl restart docker

再次运行hello-world:

sudo docker run hello-world

若出现以下信息则表明安装成功:

Unable to find image 'hello-world:latest' locally  
latest: Pulling from library/hello-world  
ca4f61b1923c: Pull complete   
Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751  
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://cloud.docker.com/  
  
For more examples and ideas, visit:  
 https://docs.docker.com/engine/userguide/  

检查版本信息:

sudo docker version

显示信息如下:

Client:
 Version:	17.12.0-ce
 API version:	1.35
 Go version:	go1.9.2
 Git commit:	c97c6d6
 Built:	Wed Dec 27 20:11:19 2017
 OS/Arch:	linux/amd64

Server:
 Engine:
  Version:	17.12.0-ce
  API version:	1.35 (minimum version 1.12)
  Go version:	go1.9.2
  Git commit:	c97c6d6
  Built:	Wed Dec 27 20:09:53 2017
  OS/Arch:	linux/amd64
  Experimental:	false

三、安装后续步骤:

1.以非 root 用户身份管理 Docker

docke r守护程序绑定到一个 Unix 套接字而不是 TCP 端口。默认情况下,Unix 套接字由用户拥有 root,其他用户只能使用 sudo 来访问它。该 docker 守护进程始终运行的 root 用户。如果不想在运行 docker 命令时使用 sudo,需要创建一个名为 docker 的 Unix Group 向其中添加用户。当 docker 守护进程启动时,它使得 Unix 套接字的所有权可以被 docker 组读/写。

要创建 docker 组并添加用户:

(1).创建 docker 组
sudo groupadd docker
(2).将自己的用户添加到 docker 组中:
sudo usermod -aG docker 用户名
(3).注销并重新登陆以重新验证组成员关系。

如果在虚拟机上进行测试,则可能需要重新启动虚拟机才能使更改生效。

sudo service docker restart
(4).验证不需要 sudo 运行 docker 命令:
docker run hello-world
2.配置 Docker 在启动时启动
sudo systemctl enable docker
3.卸载 Docker CE
(1).卸载 Docker CE 软件包:
sudo systemctl enable docker
(2).删除所有图像,容器和卷:
sudo rm -rf /var/lib/docker
4.安装 docker-compose
(1).运行以下命令下载最新版本的 docker-compose:
sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose  
(2).更改二进制文件的权限,使其能够运行:
sudo chmod +x /usr/local/bin/docker-compose
(3).测试安装:
docker-compose --version
(4).参考文章:

参考文档:https://docs.docker.com/compose/install/#install-compose
应用实例:https://docs.docker.com/compose/

参考文章:

https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/#upgrade-docker-ce
https://docs.docker.com/engine/installation/linux/linux-postinstall/
https://yq.aliyun.com/articles/110806?spm=a2c1q.8351553.0.0.5186d9c11wgjkh
http://blog.csdn.net/rickey17/article/details/72809384
http://www.tinylab.org/use-docker-without-sudo/
  • 20
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值