1. 前言
本文主要介绍Ubuntu系统如何安装docker任意指定版本,可选择Ubuntu默认仓库的版本或者Docker官方源最新版本。本文教程基于Ubuntu Server 18.04编写。
2. 使用默认仓库里的版本
如果想安装最新版本,请直接跳到第下一步。
安装Ubuntu官方仓库的版本:
zcwyou@ubuntu1804:~$ sudo apt install docker.io
看到下图表示安装成功。
启动docker
zcwyou@ubuntu1804:~$ sudo systemctl start docker
查看docker运行壮态:
zcwyou@ubuntu1804:~$ sudo systemctl status docker
docker.service – Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-01-08 11:58:32 UTC; 55min ago
Docs: https://docs.docker.com
设置docker开机自启动:
zcwyou@ubuntu1804:~$ sudo systemctl enable docker
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable docker
查看已经安装的docker版本
zcwyou@ubuntu1804:~$ docker -v
Docker version 18.06.1-ce, build e68fc7a
可以看到当前默认仓库里的版本是18.0.6,当然它会自动持续更新的.
停止运行docker
zcwyou@ubuntu1804:~$ sudo systemctl stop docker
禁止docker开机自动运行:
zcwyou@ubuntu1804:~$ sudo systemctl disable docker
Synchronizing state of docker.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable docker
卸载刚刚安装的版本:
zcwyou@ubuntu1804:~$ sudo apt remove docker.io -y && sudo apt autoremove -y
3. 安装最新版的Docker
Ubuntu仓库里默认的版本一般不是最新的,会落后一段时间,如果要追求最新的,可以使用以下这种方法。但这种安装方式首先要保证Ubuntu服务器能够访问Docker仓库地址:https://download.docker.com/linux/ubuntu/
在服务器上使用命令行测试网页连通性
sudo apt install curl -y && curl -o /dev/null -s -w %{http_code} https://download.docker.com/linux/ubuntu/
如果你看到输出结果最后一行以200开头,表明网站连接正常。
200zcwyou@ubuntu1804:~$
如果连接正常,按照下面的操作步骤进行安装。
zcwyou@ubuntu1804:~$ sudo apt update
执行结果如下:
Hit:1 https://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 https://mirrors.aliyun.com/ubuntu bionic-updates InRelease
Hit:3 https://mirrors.aliyun.com/ubuntu bionic-backports InRelease
Hit:4 https://mirrors.aliyun.com/ubuntu bionic-security InRelease
Hit:5 http://repo.mysql.com/apt/ubuntu bionic InRelease
Reading package lists… Done
Building dependency tree
Reading state information… Done
安装相关依赖:
zcwyou@ubuntu1804:~$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
配置docker官方仓库地址:
zcwyou@ubuntu1804:~$ sudo vi /etc/apt/sources.list.d/docker.list
按键盘上的i进入编辑模式复制粘帖以下内容:
deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable
按ESC键,输入:wq保存退出vi
导入docker官方仓库公钥:
zcwyou@ubuntu1804:~$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
更新本地apt缓存
zcwyou@ubuntu1804:~$ sudo apt update
安装最新版docker-ce
zcwyou@ubuntu1804:~$ sudo apt install docker-ce -y
如果安装成功,看到最后的输出应该是这样的
Setting up libltdl7:amd64 (2.4.6-2) …
Processing triggers for man-db (2.8.3-2ubuntu0.1) …
Setting up docker-ce-cli (5:18.09.0~3-0~ubuntu-bionic) …
Setting up pigz (2.4-1) …
Setting up docker-ce (5:18.09.0~3-0~ubuntu-bionic) …
update-alternatives: using /usr/bin/dockerd-ce to provide /usr/bin/dockerd (dockerd) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /lib/systemd/system/docker.service.
Processing triggers for libc-bin (2.27-3ubuntu1) …
检查docker版本
zcwyou@ubuntu1804:~$ docker --version
Docker version 18.09.0, build 4d60db4
可以看到,现在安装的版本是18.09.0,比Ubuntu默认源里的版本是更新一点。
评论前必须登录!
注册