如果之前安装过docker,执行下面命令删除
sudo apt-get remove docker docker-engine docker.io
- 删除后执行 sudo apt-get update 更新软件
- 安装必要的软件包以允许apt通过HTTPS使用存储库
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
- 添加GPG密钥,可以添加官方的和阿里的,我添加的阿里的,国内的快啊(墙裂推荐)
阿里
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
官方
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
添加完毕后可以执行以下命令验证
sudo apt-key fingerprint 0EBFCD88
- 正常情况下会输出如下内容
pub rsa4096 2017-02-22 [SCEA] 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 uid [ 未知 ] Docker Release (CE deb) docker@docker.com sub rsa4096 2017-02-22 [S]
- 设定稳定仓储库也可以不设置,不设置默认使用官方的,具体是:deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
同样可以用阿里 的镜像:设置命令如下(其中的lsb_release -cs相当于一个函数,直接获取Ubuntu下的最新版本)
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
- 设置完毕再次执行命令更新 软件包
sudo apt-get update
安装docker
sudo apt-get -y install docker-ce
- 或者
sudo apt-get install docker-ce docker-ce-cli containerd.io
启动docker
systemctl start docker
安装工具
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
重启docker
service docker restart
测试是否成功
liuxin@liuxin:/opt$ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 2db29710123e: Pull complete Digest: sha256:faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af 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/
查看版本
sudo docker version
查看镜像
sudo docker images
指定版本
apt-cache madison docker-ce
选择要安装的版本,执行sudo apt-get install -y docker-ce=<VERSION>命令即可。