0%

Debian 安装 Docker & Docker Compose

安装 Docker

正常安装

https://docs.docker.com/engine/install/debian/#install-using-the-repository

镜像安装

如果你过去安装过 docker,先删掉:

for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

首先安装依赖:

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

信任 Docker 的 GPG 公钥并添加仓库:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

最后安装

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/

设置加速镜像和代理

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [],
  "proxies": {
    "http-proxy": "http://<user>:<password>@<domain>:<port>",
    "https-proxy": "http://<user>:<password>@<domain>:<port>",
    "no-proxy": "<registry.domain>"
  }
}
EOF

截止 2024年6月,国内所有的加速镜像均已失效(强制下架),请设置为私有或考虑使用代理。

重启 docker 并设置开机自启

sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker

允许运行普通账号直接执行 docker 命令

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user

安装 Docker Compose

新版本的 Docker 已经自带 Compose 了. 安装 Docker 后可直接使用 docker compose {command} ! 如有特殊需求, 请使用下述方法安装.

export LATEST_VERSION=$(curl "https://api.github.com/repos/docker/compose/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
curl -o ./docker-compose -L https://github.com/docker/compose/releases/download/$LATEST_VERSION/docker-compose-linux-`uname -m`
sudo chmod +x ./docker-compose
sudo mv ./docker-compose /usr/local/bin/docker-compose

若下载缓慢请使用 curl --proxy socks5://ip:port -L {path} 参数设置代理.