本文已参与「新人创作礼」活动,一起开启掘金创作之路。
一、参考资料
官方文档 - Install Docker Engine on Ubuntu
二、卸载Docker
# 卸载可能存在的旧版本
sudo apt-get remove docker docker-engine docker.io containerd runc
# 删除安装时自动安装的所有包
sudo apt-get autoremove docker docker-ce docker-engine docker.io containerd runc
sudo apt-get purge docker-ce
# 删除相关目录和配置文件
# 卸载Docker CE
sudo apt-get purge docker-ce
# 卸载Docker EE
sudo apt-get purge docker-ee
# 删除Docker镜像、容器、数据卷等文件
sudo rm -rf /var/lib/docker
sudo rm -rf /etc/systemd/system/docker.service.d
sudo rm -rf /etc/docker
sudo rm /etc/apparmor.d/docker
sudo groupdel docker
sudo rm -rf /var/run/docker.sock
# 使用dpkg查询已安装包,逐个卸载
dpkg -l | grep docker
sudo apt-get autoremove docker-ce-*
# 检查系统中是否还存在docker文件
sudo find / -name '*docker*'
# 验证是否卸载成功
# 如果是下面的输出,说明已经卸载成功
# sh: /usr/bin/docker: 没有那个文件或目录
docker --version
三、安装Docker
# 更新软件列表
sudo apt-get update
# 允许apt命令可以使用HTTPS访问Docker repository
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
# 添加Docker官方的GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# 安装稳定版仓库
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# 更新软件列表
sudo apt-get update
# 列出可用版本的Docker-ce
sudo apt-cache madison docker-ce
# # 安装指定版本的Docker CE和containerd
sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io docker.io
# 安装最新版本的Docker CE和containerd
sudo apt-get install docker-ce docker-ce-cli containerd.io docker.io
# 查看docker版本
docker --version
# 重载docker配置
sudo systemctl daemon-reload
# 重启docker服务
sudo systemctl restart docker
四、安装bash-completion
通过bash_complete脚本,docker提供自动补全功能,在执行该命令时,敲tab即可自动补全参数,大大提高命令输入效率。
# 安装bash-completion
apt install bash-completion
# 加载bash-completion
source /etc/bash_completion
# 修改docker-compose二进制文件的权限
sudo chmod +x /usr/local/bin/docker-compose
# 查看版本
docker-compose --version
五、docker换源加速
参考资料 中科大 docker 腾讯云 docker 七牛云 docker
# 腾讯云
https://mirror.ccs.tencentyun.com
# 中科大
https://reg-mirror.qiniu.com/
# 七牛云
https://reg-mirror.qiniu.com
# 修改 /etc/docker/daemon.json 文件
{"registry-mirrors":["https://reg-mirror.qiniu.com/"]}
# 重新启动服务
sudo systemctl daemon-reload
sudo systemctl restart docker
# 查看加速配置是否生效
docker info
# 如果出现下面的提示,说明加速配置生效
Registry Mirrors:
https://reg-mirror.qiniu.com
Docker镜像加速
参考资料 Docker实践(一):Ubuntu16.04安装Docker
六、运行Docker镜像
1. 运行hello-world
# 运行hello-world镜像
sudo docker run hello-world
运行成功的结果
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/
2. 运行apache服务
# 运行apache容器
docker run -d -p 80:80 httpd
# 访问apache
0.0.0.0:8080
七、可能出现的问题
- 权限问题 启动容器出错
docker run -d -p 80:80 httpd
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.24/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
参考资料
[解决Ubuntu18.04启动Docker“Got permission denied while trying to connect to the Docker daemon socket“问题](https://blog.csdn.net/liangllhahaha/article/details/92077065)
[[问题解决]Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock](https://www.cnblogs.com/everfight/p/docker_sock.html)
方法一(推荐)
sudo chmod 777 /var/run/docker.sock
方法二
sudo groupadd docker #添加docker用户组
sudo gpasswd -a $XXX docker #检测当前用户是否已经在docker用户组中,其中XXX为用户名,例如我的,yichao
sudo gpasswd -a $USER docker #将当前用户添加至docker用户组
newgrp docker #更新docker用户组
检查是否更新成功
再次执行"docker version"命令,发现不再出现"Got permission denied"权限报错
- docker卸载的问题
bash: /usr/bin/docker: 没有那个文件或目录
错误原因:
手动删除了 /usr/bin/docker 目录,重新安装docker失效
解决办法:
卸载docker,删除docker相关的包,重新安装