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 退出虚拟机重连生效

参考:

相关推荐
Christal_pyy29 分钟前
树莓派4基于Debian GNU/Linux 12 (Bookworm)添加多个静态ipv4网络
linux·网络·debian
csbDD2 小时前
2025年网络安全(黑客技术)三个月自学手册
linux·网络·python·安全·web安全
Natsuagin4 小时前
轻松美化双系统启动界面与同步时间设置(Windows + Ubuntu)
linux·windows·ubuntu·grub
我们的五年4 小时前
【Linux网络编程】应用层协议HTTP(请求方法,状态码,重定向,cookie,session)
linux·网络·http
_院长大人_4 小时前
Docker Mysql 数据迁移
mysql·adb·docker
我们的五年6 小时前
【Linux网络】TCP/IP地址的有机结合(有能力VS100%???),IP地址的介绍
linux·运维·网络·tcp/ip
davenian7 小时前
< OS 有关 > Ubuntu 24 SSH 服务器更换端口 in jp/us VPSs
linux·ubuntu·ssh
诚信爱国敬业友善7 小时前
GUI编程(window系统→Linux系统)
linux·python·gui
sekaii7 小时前
ReDistribution plan细节
linux·服务器·数据库
YH_DevJourney8 小时前
Linux-C/C++《C/8、系统信息与系统资源》
linux·c语言·c++