Docker:安装配置

目录


官网

一、卸载旧版本

首先如果系统中已经存在旧版本的Docker,则先卸载:

linux 复制代码
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

二、配置Docker的yum库

首先要安装一个yum工具

linux 复制代码
yum install -y yum-utils

安装成功之后,执行命令,配置Docker的yum源

linux 复制代码
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

三、安装Docker

3.1 在线安装方式

linux 复制代码
yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

检查安装是否成功,查看版本号

linux 复制代码
docker -v

启动 docker 服务

linux 复制代码
systemctl start docker

查看 docker 服务的运行状态

linux 复制代码
systemctl status docker

将 docker 服务设置为开机自动启动

linux 复制代码
systemctl enable docker

列出本地所有镜像

linux 复制代码
docker images

查看和管理运行中或已停止的容器

linux 复制代码
docker ps

如果以上命令都不报错,则说明docker安装成功

3.2 离线安装方式

首先下载离线安装包,下载完成之后,解压

linux 复制代码
tar -zxvf docker-24.0.6.tgz

将解压出来的docker文件内容拷贝或者移动到/usr/bin/目录下

linux 复制代码
cp docker/* /usr/bin/

然后就可以使用docker -v或者docker info命令验证是否可以输出docker信息了。因为没有开启守护进程,docker 其他命令还不能使用。所以需要编写docker.service文件加入Linux服务当中并开启守护进程。

编辑文件:

linux 复制代码
vim /etc/systemd/system/docker.service

增加内容:

txt 复制代码
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
  
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock --selinux-enabled=false --default-ulimit nofile=65536:65536
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
  
[Install]
WantedBy=multi-user.target

如果需要开启远程服务ExecStart属性修改为以下命令:# -H tcp://0.0.0.0:2375 开启远程连接命令

txt 复制代码
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --selinux-enabled=false --default-ulimit nofile=65536:65536

添加文件可执行权限

linux 复制代码
chmod +x /etc/systemd/system/docker.service

配置成功后,重新加载 daemon 服务

linux 复制代码
systemctl daemon-reload

启动 docker 服务

linux 复制代码
systemctl start docker

查看 docker 服务的运行状态

linux 复制代码
systemctl status docker

将 docker 服务设置为开机自动启动

linux 复制代码
systemctl enable docker

列出本地所有镜像

linux 复制代码
docker images

查看和管理运行中或已停止的容器

linux 复制代码
docker ps

如果以上命令都不报错,则说明docker安装成功

四、配置阿里云镜像加速【选配】

按照官网里面镜像加速器中的文档进行配置即可。


Docker:安装配置镜像加速

五、Docker服务相关命令

重新加载配置文件

linux 复制代码
systemctl daemon-reload

启动 docker 服务

linux 复制代码
systemctl start docker

查看 docker 服务的运行状态

linux 复制代码
systemctl status docker

停止运行

linux 复制代码
systemctl stop docker

重新启动

linux 复制代码
systemctl restart docker

将 docker 服务设置为开机自动启动

linux 复制代码
systemctl enable docker

禁用开机自动启动

linux 复制代码
systemctl disabled docker

查看docker开机自动启动状态 enabled:开启, disabled:关闭

linux 复制代码
systemctl is-enabled docker.service

查看 docker 版本号

linux 复制代码
docker -v

Docker启动所有容器

linux 复制代码
docker start $(docker ps -a -q)

六、导出和导入镜像

首先在可以连接网络的服务器上面获取相关软件镜像,然后通过 saveload 命令导出和导入镜像。由于导入的镜像没有镜像名称和 tag 版本号,需要使用 docker tag 命令 修改导入的镜像命令。

docker导出镜像:

linux 复制代码
docker save 99ee9af2b6b1 > redis.tar # 99ee9af2b6b1 镜像ID

docker导入镜像:

linux 复制代码
docker load < redis.tar

docker修改镜像标签名称:

linux 复制代码
docker tag 99ee9af2b6b1 redis:3.2.0  #99ee9af2b6b1 镜像ID  镜像名称:版本号