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

参考:

相关推荐
冼紫菜36 分钟前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
Chuncheng's blog2 小时前
RedHat7 如何更换yum镜像源
linux
爱莉希雅&&&2 小时前
shell脚本之条件判断,循环控制,exit详解
linux·运维·服务器·ssh
wei_work@2 小时前
【linux】Web服务—搭建nginx+ssl的加密认证web服务器
linux·服务器·ssl
扶尔魔ocy3 小时前
【Linux C/C++开发】轻量级关系型数据库SQLite开发(包含性能测试代码)
linux·数据库·c++·sqlite
追赶sun4 小时前
Ubuntu 添加系统调用
linux·ubuntu·操作系统·系统调用
北漂老男孩4 小时前
在 Linux 上安装 MATLAB:完整指南与疑难解决方案
linux·运维·matlab
Why not try?!4 小时前
Centos7 中 Docker运行配置Apache
运维·docker·容器
hnlucky4 小时前
如何彻底清空docker里面不使用的容器?
运维·docker·容器
像风一样的男人@5 小时前
Linux --systemctl损坏
linux·运维·服务器