Docker技术笔记:Ubuntu安装Docker CE

松洛华
2023-12-01

安装环境

操作系统:Ubuntu Xenial 16.04 (LTS)

Go语言版本:1.9.4

配置Ubuntu更新源(清华大学)

$ sudo vim /etc/apt/sources.list
# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse

卸载旧版本的Docker

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

安装新版本的Docker

步骤一:安装必要的系统工具

$ sudo apt-get update
$ sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

步骤二:安装GPG证书

$ curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

步骤三:写入软件源信息

$ sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

步骤四:更新并安装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客户端版本大于1.10.0的用户,可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器:

$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://irnlfwui.mirror.aliyuncs.com"]
}
EOF

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

测试Docker版本(验证安装是否成功)

命令:docker –version && docker version && docker info

$ sudo docker --version
Docker version 17.12.1-ce, build 7390fc6

$ sudo docker version
Client:
 Version:   17.12.1-ce
 API version:   1.35
 Go version:    go1.9.4
 Git commit:    7390fc6
 Built: Tue Feb 27 22:17:40 2018
 OS/Arch:   linux/amd64
Server:
 Engine:
  Version:  17.12.1-ce
  API version:  1.35 (minimum version 1.12)
  Go version:   go1.9.4
  Git commit:   7390fc6
  Built:    Tue Feb 27 22:16:13 2018
  OS/Arch:  linux/amd64
  Experimental: false

$ sudo docker info
Containers: 3
 Running: 0
 Paused: 0
 Stopped: 3
Images: 6
...

测试Docker安装

可以通过运行简单的Docker Image来进行安装测试:

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

列出所有的镜像image和容器container:

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              f2a91732366c        3 months ago        1.85kB

$ sudo docker container ls -all
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
c621f40a070c        hello-world         "/hello"            15 minutes ago      Exited (0) 15 minutes ago                       friendly_edison

Recap and Cheat Sheet

## List Docker CLI commands
docker
docker container --help

## Display Docker version and info
docker --version
docker version
docker info

## Excecute Docker image
docker run hello-world

## List Docker images
docker image ls

## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -a -q

## Stop all Docker containers
docker stop $(docker ps -a -q)

## Remove all Docker containers
docker rm $(docker ps -a -q)

## Remove specific Docker image
docker rmi <image_id>

## Remove all untagged Docker images (id is <none>)
docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

## Remove all Docker images
docker rmi $(docker ps -a -q)

参考资料

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

Ubuntu中安装Docker帮助链接

 类似资料: