Ubuntu安装Docker和Docker Compose

1. 安装前准备:

bash 复制代码
#安装前先卸载操作系统默认安装的docker,
sudo apt-get remove docker docker-engine docker.io containerd runc

#安装必要支持
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release


# 阿里源(推荐使用阿里的gpg KEY)
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg


#添加 apt 源:
#Docker官方源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


#阿里apt源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


#更新源
sudo apt update
sudo apt-get update

2. 安装Docker:

bash 复制代码
#安装最新版本的Docker
sudo apt install docker-ce docker-ce-cli containerd.io
#等待安装完成

#查看Docker版本
sudo docker -v

#查看Docker运行状态
sudo systemctl status docker

3. 安装Docker Compose插件 :

bash 复制代码
# 安装Docker Compose
sudo apt install docker-compose

Docker Compose

  • 是Docker生态系统中的一个工具,主要用于简化多个Docker容器的应用部署和服务编排。
  • 通过一个名为docker-compose.yml的YAML配置文件来定义一个多容器的应用栈,包含了多个服务、网络以及卷等资源的配置。
  • 使用Docker Compose,用户可以通过一条命令(如docker-compose up)来启动、停止和重启整个应用栈中包含的所有服务,并且这些服务之间可以根据配置进行通信和资源共享。
报错1:Package *** has no installation candidate

原因是修改了apt的镜像源后没有更新软件包:只执行了 apt update 没有执行 apt-get upgrade,apt源里没有docker,需要添阿里源

测试拉取镜像运行:

bash 复制代码
docker pull hello-world

docker run hello-world
报错2:拉取镜像报错,Error response from daemon: Get "https://registry-1.docker.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

解决:配置加速地址并重启docker

bash 复制代码
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.m.daocloud.io"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

测试拉取hello-world和运行没问题了说明已经装好了。

设置非root用户使用docker命令:
bash 复制代码
#创建docker组

sudo groupadd docker

#添加ithing进入docker组

sudo gpasswd -a ithing docker

#重启docker服务

sudo systemctl restart docker

#接下来就可以使用你添加的用户(ithing)进行使用docker命令了

exit 退出虚拟机重连生效

参考:

相关推荐
超级小忍4 分钟前
Spring Boot 与 Docker 的完美结合:容器化你的应用
spring boot·后端·docker
许白掰1 小时前
Linux入门篇学习——Linux 工具之 make 工具和 makefile 文件
linux·运维·服务器·前端·学习·编辑器
B1nna2 小时前
Docker学习
学习·docker·容器
缘友一世2 小时前
网安系列【15】之Docker未授权访问漏洞
docker·容器
longze_75 小时前
Ubuntu连接不上网络问题(Network is unreachable)
linux·服务器·ubuntu
Dirschs6 小时前
【Ubuntu22.04安装ROS Noetic】
linux·ubuntu·ros
qianshanxue116 小时前
ubuntu 操作记录
linux
AmosTian8 小时前
【系统与工具】Linux——Linux简介、安装、简单使用
linux·运维·服务器
慕木兮人可10 小时前
Docker部署MySQL镜像
spring boot·后端·mysql·docker·ecs服务器
这我可不懂11 小时前
Python 项目快速部署到 Linux 服务器基础教程
linux·服务器·python