Docker安装

147 阅读2分钟

在线安装

# 卸载旧版本
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
# 安装GCC
yum -y install gcc
yum -y install gcc-c++
# 安装软件包
yum install -y yum-utils
# 设置镜像仓库
## 官方设置
yum-config-manager --add-repo https://download.docker.com/linux/centos/dockerce.repo
## 阿里云
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 更新yum索引
yum makecache fast
# 安装
yum -y install docker-ce docker-ce-cli containerd.io
# 启动
systemctl start docker
# 设置自启动
systemctl enable docker
# 信息查看
docker version
# 测试
docker run hello-world

调整默认DATA路径

Docker 默认安装的情况下,会使用/var/lib/docker/目录作为存储目录,用以存放拉取的镜像和创建的容器等。不过由于此目录一般都位于系统盘,遇到系统盘比较小,而镜像和容器多了后就容易出问题,这里说明一下如何修改 Docker 的默认存储目录

1、查看当前docker的默认存储目录

docker info

image.png

2、停止docker服务

systemctl stop docker

3、目录迁移

3.1创建新的docker目录,执行命令df -h,找一个大的磁盘。 比如在/home目录下面建了 /home/docker目录,执行命令:

mkdir -p /home/docker

3.2迁移/var/lib/docker目录下面的文件到 /home/docker

rsync -avz /var/lib/docker /home/docker    
#和cp类似,只不过是会自动过滤,目标目录有的文件不会重新拷贝

4、编辑/etc/docker/daemon.json文件

vi /etc/docker/daemon.json

默认情况下这个配置文件是没有的,这里实际也就是新建一个,然后写入以下内容:

{ 
 "registry-mirrors": ["http://hub-mirror.c.163.com"],  
"data-root/graph""/home/docker"
}
#data-root/graph取决于具体的ubuntu版本或者kernel版本决定要用data-root还是graph
#/home/docker --> docker的存储路径

5、保存退出,然后重启 docker 服务

systemctl restart docker

6、检查docker存储路径是否配置成功

docker info 
#Docker Root Dir/home/docker

7、启动成功后,再确认之前的镜像还在

docker ps -a
docker images

8、确定容器、镜像没问题后删除/var/lib/docker/目录中的文件。

rm -rf /var/lib/docker/*

离线安装

官方指南:docs.docker.com/engine/inst…

Install static binaries

  1. Download the static binary archive. Go to download.docker.com/linux/stati…, choose your hardware platform, and download the .tgz file relating to the version of Docker Engine you want to install.

  2. Extract the archive using the tar utility. The dockerd and docker binaries are extracted.

    $ tar xzvf /path/to/FILE.tar.gz
    
  3. Optional: Move the binaries to a directory on your executable path, such as /usr/bin/. If you skip this step, you must provide the path to the executable when you invoke docker or dockerd commands.

    $ sudo cp docker/* /usr/bin/
    
  4. Start the Docker daemon:

    $ sudo dockerd &
    

    If you need to start the daemon with additional options, modify the above command accordingly or create and edit the file /etc/docker/daemon.json to add the custom configuration options.

  5. Verify that Docker is installed correctly by running the hello-world image.

    $ sudo docker run hello-world
    

    This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.