Linux 安装 Docker 和 Docker-Compose

Docker 由开源项目 moby(改名后的,之前叫 docker。先在的 docker 用于商业化) 而来:https://www.infoq.com/news/2017/05/Docker-Moby-LinuxKit

Docker 从 1.31.1 后分 CE(Community Edition 社区版)和 EE(Enterprise Edition 企业版)两个版本:https://zhuanlan.zhihu.com/p/305572519,这里安装 CE 版本。

 

一、安装

yum

# 卸载
sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
sudo yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
# 安装
sudo yum install -y yum-utils
# 阿里源:https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 华为源: https://mirrors.huaweicloud.com/docker-ce/linux
# daocloud 源:https://download.daocloud.io/docker/linux/centos/docker-ce.repo
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum-config-manager --disable docker-ce-test
sudo yum-config-manager --disable docker-ce-nightly
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 启动
sudo systemctl start docker
sudo systemctl enable docker
sudo docker version

apt

# 卸载
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
# 安装
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
# 阿里源:https://mirrors.aliyun.com/docker-ce/linux
# 网易源:https://mirrors.163.com/docker-ce/linux
# https://chinanet.mirrors.ustc.edu.cn/docker-ce/linux
sudo curl -fsSL https://mirrors.163.com/docker-ce/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://mirrors.163.com/docker-ce/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 启动
sudo systemctl start docker
sudo systemctl enable docker
sudo docker version

将当前用户添加至 docker 用户组(加入 docker 用户组后操作 docker 不用 sudo)

# 添加 docker 用户组
sudo groupadd docker
# 将当前登陆用户加入到 docker 用户组中
sudo gpasswd -a $USER docker
# 更新用户组
newgrp docker

 

二、切换镜像源

daemon.json 配置说明:https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file

关于守护进程:https://docs.docker.com/config/daemon

创建或修改 /etc/docker/daemon.json 文件

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json << EOF
{
  "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com", "https://hub-mirror.c.163.com"]
}
EOF
# 重启服务
sudo systemctl daemon-reload
sudo systemctl restart docker
# 查看当前配置信息
docker info

其它源

# 阿里云:https://help.aliyun.com/zh/acr/user-guide/accelerate-the-pulls-of-docker-official-images
https://registry.cn-hangzhou.aliyuncs.com
# 华为云:https://mirrors.huaweicloud.com
https://05f073ad3c0010ea0f4bc00b7105ec20.mirror.swr.myhuaweicloud.com
# 腾讯云:https://cloud.tencent.com/document/product/1207/45596
https://mirror.ccs.tencentyun.com
# DaoCloud:https://docs.daocloud.io/community/mirror,https://github.com/DaoCloud/public-image-mirror
http://docker.m.daocloud.io
# 亚马逊:https://gallery.ecr.aws

测试

docker run hello-world

 

三、安装 docker-compose

Compose V2

sudo apt-get update
sudo apt-get install docker-compose-plugin
docker compose version

yum 安装(可能不是最新版本),需要 epel 源:https://developer.aliyun.com/mirror/epel

sudo mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
sudo mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sudo yum install docker-compose -y

下载安装(最新版)

# 要安装不同版本的 Compose,请将 1.29.2 替换为想要使用的 Compose 版本,v2.x.x 开始版本号带 v
# 原地址:sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo curl -L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version
# docker-compose version 1.29.2, build 5becea4c

 

四、Windows WSL2 安装 Docker

安装与上面一样,这里记录一些问题

# 自启动,wsl systemd 支持:https://learn.microsoft.com/zh-cn/windows/wsl/wsl-config#systemd-support
sudo vim /etc/wsl.conf

[boot]
systemd=true

# WSL Debian Docker 启动失败
# https://superuser.com/questions/1468316/which-kernel-moduules-are-really-needed-for-docker-installation
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

# grep: /etc/fstab: No such file or directory
sudo touch /etc/fstab
 
# 重启 docker
sudo service docker restart
sudo service docker status

# 有些容器启动失败可能是文件权限问题
# https://blog.csdn.net/x356982611/article/details/108732844
# https://docs.microsoft.com/zh-cn/windows/wsl/wsl-config#per-distribution-configuration-options-with-wslconf
sudo vim /etc/wsl.conf

[automount]
enabled = true
options = "metadata"
mountFsTab = false

# 重启 wsl(windows 命令)
# https://docs.microsoft.com/zh-cn/windows/wsl/basic-commands#shutdown
wsl --shutdown

WSL Alpine Docker 安装

# 换源 https://mirrors.ustc.edu.cn/help/alpine.html
sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

# 安装
apk add --no-cache openrc docker docker-compose

# 启动
openrc
touch /run/openrc/softlevel
service docker start
# /etc/init.d/docker start
# rc-update add docker default

# 用户
apk add shadow sudo
# 用户添加到 wheel 组,只有在 wheel 组的用户才能使用 sudo
addgroup [username] wheel
# 去掉执行 sudo 命令时每次输入密码,找到行"# %wheel ALL=(ALL) NOPASSWD: ALL"去掉注释
visudo

其它子系统:https://wsldl-pg.github.io/docs/Using-wsldl

 


https://docs.docker.com/engine/install/centos

https://docs.docker.com/compose/install

https://help.mirrors.cernet.edu.cn/docker-ce

posted @ 2019-08-26 22:26  江湖小小白  阅读(2818)  评论(0编辑  收藏  举报