安装 docker 环境

安装 Docker

  • 删除旧版本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    sudo yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-selinux \
    docker-engine-selinux \
    docker-engine
    # 或者
    sudo yum remove flannel docker* -y
    sudo yum -y remove docker docker-common docker-selinux docker-engine docker-engine-selinux container-selinux docker-ce
    sudo rm -rf /var/lib/docker
  • 配置 repo

    yum-config-manager

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    sudo yum install -y yum-utils \
    device-mapper-persistent-data \
    lvm2

    sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

    # 或者使用阿里云
    sudo yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

    # Optional
    sudo yum-config-manager --enable docker-ce-edge
    sudo yum-config-manager --enable docker-ce-test
    sudo yum-config-manager --disable docker-ce-edge

    yum 加速和代理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    # 加速配置(阿里云加速)
    ## 备份原来的yum源
    cd /etc/yum.repos.d/
    sudo mv CentOS-Base.repo CentOS-Base.repo_bak
    ## 获取阿里云yum源
    sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    # sudo wget -e "http_proxy=192.168.123.1:1234" -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

    # 代理配置
    sudo sed -i '/^proxy=/d' /etc/yum.conf
    sudo sh -c "echo proxy=http://192.168.123.1:1234 >> /etc/yum.conf"
    sudo cat /etc/yum.conf

    # 清除原有 yum 源缓存
    sudo yum clean all
    ## 生成 yum 源缓存
    sudo yum makecache
    ## 或者
    sudo rm -f /var/lib/rpm/__*
    sudo rpm --rebuilddb -v -v
    sudo yum clean dbcache
    sudo yum clean metadata
    sudo yum clean rpmdb
    sudo yum clean headers
    sudo yum clean all
    sudo rm -rf /var/cache/yum/timedhosts.txt
    sudo rm -rf /var/cache/yum/*
    sudo yum makecache
  • 安装 docker

    创建 docker 和 data 目录(可不创建,使用默认路径)

    1
    2
    3
    4
    5
    6
    7
    8
    # docker 默认下载目录 /var/lib/docker/
    sudo mkdir -p /data/docker/lib/docker
    # 创建软连接
    sudo ln -s /data/docker/lib/docker /var/lib/docker
    # 删除软连接(注意:最后没有/,否则会把/var/lib/docker下的内容全部删除)
    # rm /var/lib/docker
    # sudo chmod 770 -R /var/lib/docker/
    # sudo chmod 777 -R /data/docker

    在线安装

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 安装最新版本
    sudo yum install docker-ce
    #安装指定版本 docker
    # 可以查看所有仓库中所有docker版本,并选择特定版本安装
    yum list docker-ce --showduplicates | sort -r
    docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
    sudo yum install docker-ce-<VERSION STRING>

    # 或者
    sudo curl https://releases.rancher.com/install-docker/20.10.sh | sh
    sudo curl --proxy 192.168.123.1:1234 https://releases.rancher.com/install-docker/20.10.sh | sh

离线安装

1
2
3
4
5
6
7
8
9
# 需要按顺序执行
sudo yum install -y docker-ce-cli-19.03.12-3.el7.x86_64.rpm
# 下载路径:http://mirror.centos.org/centos/7/extras/x86_64/Packages/
sudo yum install -y container-selinux-2.107-3.el7.noarch.rpm
sudo yum install -y containerd.io-1.3.7-3.1.el7.x86_64.rpm
sudo yum install -y docker-ce-19.03.12-3.el7.x86_64.rpm

# 或者将以上 rpm 包放到一个文件夹,然后执行一下命令,会自动判断依赖
yum localinstall -y *.rpm

  • 启动并加入开机启动

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # 启动 docker
    sudo systemctl start docker
    # 重启
    sudo systemctl restart docker
    # 停止运行 docker
    sudo systemctl stop docker
    # 开机启动
    sudo systemctl enable docker
    # 禁止开机启动
    sudo systemctl disable docker
  • 版本锁定

1
2
3
sudo yum install yum-plugin-versionlock
sudo yum versionlock add docker-ce docker-ce-cli
sudo yum versionlock list
  • 将当前用户加入docker组,并重启docker
1
2
sudo gpasswd -a ${USER} docker
sudo systemctl restart docker
  • 验证安装

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    $ docker version
    Client:
    Version: 18.06.1-ce
    API version: 1.38
    Go version: go1.10.3
    Git commit: e68fc7a
    Built: Tue Aug 21 17:23:03 2018
    OS/Arch: linux/amd64
    Experimental: false

    Server:
    Engine:
    Version: 18.06.1-ce
    API version: 1.38 (minimum version 1.12)
    Go version: go1.10.3
    Git commit: e68fc7a
    Built: Tue Aug 21 17:25:29 2018
    OS/Arch: linux/amd64
    Experimental: false

Docker 配置代理

1
2
3
4
5
6
7
8
9
10
11
# 配置 docker pull 代理
# sudo rm -rf /etc/systemd/system/docker.service.d
# cat /etc/systemd/system/docker.service.d/http-proxy.conf
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf <<-'EOF'
[Service]
Environment="HTTP_PROXY=http://192.168.123.1:1234" "NO_PROXY=localhost,127.0.0.1,172.16.120.0/24"
EOF
sudo systemctl daemon-reload
sudo systemctl enable docker
sudo systemctl restart docker

其中172.16.120.0/24为宿主机 ip

Docker 配置加速源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# sudo rm -rf /etc/docker/daemon.json
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"],
"default-address-pools": [
{
"base": "192.49.0.0/16",
"size": 24
}
],
"log-driver":"json-file",
"log-opts":{ "max-size" :"50m","max-file":"10"}
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

安装 docker-compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 安装
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

curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

# https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64

# sudo curl --proxy 192.168.123.1:1234 -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 --proxy 192.168.123.1:1234 -L "https://github.com/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 chmod 755 /usr/local/bin/docker-compose
# sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

# 验证是否安装成功
docker-compose --version

# 安装后,如果提示 bash: docker-compose: command not found...,则执行以下命令
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

进入容器

1
2
3
4
5
# centos
docker exec -it <container-name> /bin/bash

# alpine
docker exec -it <container-name> sh
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/2fff8f5e.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!