Skip to content

一、系统环境

Centos7

二、安装

安装DockerCE版本,官方教程

1、移除旧版本

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2、设置docker安装仓库

$ sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
$ sudo yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
$ sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
$ sudo yum makecache

3、安装

  • 安装默认版本
$ sudo yum install docker-ce
  • 安装特定版本

如果需要安装特定版本,按下面的方式执行。 先查看现有的版本

$ yum list docker-ce --showduplicates | sort -r

显示如下

docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable ......

安装18.09.8 版本

$ sudo yum install docker-ce-18.09.8 docker-ce-cli-18.09.8 containerd.io

4、启动并设置

$ sudo systemctl start docker

设置开机启动

$ sudo systemctl enable docker

上面2条命令可以合并

$ sudo systemctl enable docker --now

5、验证安装结果

$ sudo docker run hello-world

输出如下

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

三、修改国内镜像源

修改文件 /etc/docker/daemon.json,文件不存在就添加一个。添加配置(2024年8月9号还能使用)

{
    "registry-mirrors": [
      "https://hub.uuuadc.top",
      "https://docker.anyhub.us.kg",
      "https://dockerhub.icu",
      "https://docker.awsl9527.cn",
      "https://docker.m.daocloud.io",
      "https://docker.gs"
    ]
}

重载服务即可

$ sudo systemctl reload docker

四、卸载

  • 卸载
$ sudo yum remove docker-ce
$ sudo yum autoremove
  • 删除目录
$ rm -rf /var/lib/docker

删除 /var/lib/docker目录后,原先的镜像和容器都会被删除,请谨慎操作。如果只是为了更新,那么 /var/lib/docker 不需要删除。