Docker

Centos 7 离线安装Docker

  1. 下载安装文件

image-20220919093815660

  1. 选择自己需要的版本下载并上传到服务器
  2. 安装docker
1
2
3
4
# 解压
tar -zxvf docker-18.06.3-ce.tgz
# 将解压出来的docker文件复制到/usr/bin/目录下
cp docker/* /usr/bin/
  1. 配置docker
1
2
3
4
# 进入/etc/systemd/system/目录
cd /etc/systemd/system/
# 创建docker.service文件
touch docker.service
1
2
# 打开docker.service文件,粘贴以下内容复制
vim docker.service
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
29
30
31
32
33
[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 --selinux-enabled=false --insecure-registry=0.0.0.0
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
  1. 重新加载配置
1
2
3
4
# docker.service文件添加执行权限
chmod 777 /etc/systemd/system/docker.service
# 重新加载配置文件(每次有修改docker.service文件时都要重新加载下)
systemctl daemon-reload

注意: --insecure-registry=0.0.0.0 此处改为你自己服务器ip

  1. 启动
1
2
3
4
5
6
# 启动
systemctl start docker
# 查看docker状态
systemctl status docker.service
# 设置开机启动
systemctl enable docker.service

离线安装docker-compose

  1. 下载docker-compose的离线包

    https://github.com/docker/compose/releases

    image-20221118113432203

  2. 下载好离线包,把离线包放在指定目录下(就不需要配置环境变量)

1
2
3
4
5
6
7
8
9
10
mv docker-compose-linux-x86_64 /usr/local/bin/

//修改文件名
mv docker-compose-linux-x86_64 docker-compose

//授权
sudo chmod +x /usr/local/bin/docker-compose

//查看安装是否成功
docker-compose -v

Docker 镜像导入导出

导出

docker save -o /保存路径/文件名称.tar [镜像名称]:[镜像标签]

导入

docker load -i xxx.tar

镜像名称重命名

docker tag [镜像id] [新镜像名称]:[新镜像标签]

Docker 常见问题

容器id-json.log太大严重占用磁盘空间问题解决

  1. 应急解决
1
$ echo '' > 容器id-json.log
  1. 创建容器时控制
1
2
3
# max-size 最大数值
# max-file 最大日志数
$ docker run -it --log-opt max-size=10m --log-opt max-file=3 redis
  1. 全局配置

创建或修改文件 /etc/docker/daemon.json,并增加以下配置

1
2
3
4
5
6
{
"log-driver":"json-file",
"log-opts":{
"max-size" :"50m","max-file":"1"
}
}

重启dokcer

特别注意:已存在的容器不会生效,需要重建该容器才可以使配置生效