Docker 常见安装步骤,包括安装最新版本 Docker 和安装指定版本 Docker
安装最新 Docker
官方安装脚本
curl https://get.docker.io/ | sh
Daocloud 安装脚本
curl -sSL https://get.daocloud.io/docker | sh
CentOS
安装指定版本的 docker 的 rpm 发布在:https://yum.dockerproject.org/repo/main/centos
# 我们以安装docker 17.03.1为例
DOCKER_VERSION=17.03.1
# 下载 docker-engine
wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-${DOCKER_VERSION}-1.el7.centos.x86_64.rpm
# 下载 docker-engine-selinux
wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-selinux-${DOCKER_VERSION}-1.el7.centos.noarch.rpm
yum install -y libtool-ltdl policycoreutils-python
# 先安装 docker-engine-selinux,再安装 docker-engine-selinux
rpm -ivh docker-engine-selinux-${DOCKER_VERSION}-1.el7.centos.noarch.rpm
rpm -ivh docker-engine-${DOCKER_VERSION}-1.el7.centos.x86_64.rpm
# 下面就可以开启docker服务了
systemctl start docker
systemctl enable docker
Ubuntu
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
sudo apt update
apt list -a docker-ce
sudo apt install docker-ce=<VERSION> docker-ce-cli=<VERSION> containerd.io
docker 配置
以下内容均需要修改 /etc/docker/daemon.json
配置文件
配置私有镜像仓库
{
"registry-mirrors": [
"https://registry.docker-cn.com"
],
"insecure-registries": [
"registry.docker-cn.com"
]
}
镜像加速
Docker 镜像拉取加速器
配置日志
{
"max-concurrent-downloads": 10,
"log-driver": "json-file",
"log-level": "warn",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}
其中 log-opts
用于限制容器日志的大小
配置网段
{
"bip": "172.17.0.1/16"
}
systemctl restart docker
docker in docker
- docker dind(docker in docker) 方式:在 docker 容器内启动 docker daemon 服务,创建一个与宿主机隔离的环境
docker run -it -d --rm --privileged docker:dind
docker run -v /usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock ...
普通用户执行 Docker 命令
- 正在将用户
xiexianbin
加入到 docker
组中
sudo gpasswd -a xiexianbin docker
sudo systemctl restart docker
$ ls -lhart /var/run/docker.sock
srw-rw---- 1 root docker 0 Dec 15 13:31 /var/run/docker.sock
$ sudo chmod a+rw /var/run/docker.sock
$ ls -lhart /var/run/docker.sock
srw-rw-rw- 1 root docker 0 Dec 15 13:31 /var/run/docker.sock
$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied