1、统计docker的日志大小:
ls -lh $(find /var/lib/docker/containers/ -name *-json.log)
2、在vi编辑器内每行后面添加&
:%s/$/ \&
3、在vi编辑器内每行前面添加docker pull
:%s/^/docker pull
4、统计系统的文件大小:
ls -lh $(find / -size +10M -size -2000M) | sort -rn
5、一键启动所有容器:
docker start $(docker ps -q -a)
6、获取docker镜像信息:
docker images |awk '{print $1":"$2}'
7、查看容器占用资源情况已时间格式保存:
docker stats --no-stream $(docker ps -a |awk '{print $NF}' |awk 'NR>2{print p}{p=$0}') >>docker_load.`date +%F.%H:%M`.txt
8、操作:容器增加mysql动态添加端口
步骤:
iptables -A DOCKER ! -i docker0 -o docker0 -p tcp --dport 3306 -d 172.17.0.15 -j ACCEPT
iptables -t nat -A POSTROUTING -p tcp --dport 3306 -s 172.17.0.15 -d 172.17.0.15 -j MASQUERADE
iptables -t nat -A DOCKER ! -i dokcer0 -p tcp --dport 8306 -j DNAT --to-destination 172.17.0.15:3306
9、更换docker存储路径(把/app/docker作为新的路径为例)
步骤1:添加或者修改daemon.json
/etc/docker/daemon.json
{"graph": "/app/docker"}
步骤2:拷贝/var/lib/docker到/app/docker
cp -rf /var/lib/docker /app/docker
步骤3:重启docker
systemctl daemon-reload && systemctl restart docker
10、开启docker调试模式
步骤1:添加或者修改daemon.json
/etc/docker/daemon.json
{ "debug": true }
步骤2:重启docker
systemctl daemon-reload && systemctl restart docker
11、删除docker资源清理:容器使用的镜像:docker image prune --all 、所有没有使用的资源:
docker system prune
12、清空docker日志文件:
cat /dev/null > f2a8646430bd5c5bb09cd67240e9363c28fa8498097db047287425ad56ab5330-json.log
13、格式化打印镜像信息:
docker images| grep image-name |awk '{print $1":"$2" || "$3" || "$4" "$5" || "$7 " "$8}'
14、查看容器占用资源情况已时间格式保存
docker stats --no-stream $(docker ps -a |awk '{print $NF}' |awk 'NR>2{print p}{p=$0}') >>docker_load.`date +%F.%H:%M`.txt
15、 批量修改镜像tag
1> 获取镜像信息:
images=$(docker images|grep 172 |awk '{print $1}' |awk -F 10001/ '{print $2}')
2>批量打把172镜像打tag成nexus3
for image in $images; do tagname=$(docker images|grep "/$image " |grep 172 |awk '{print $2}') && for a in $tagname; do docker tag 172.30.1.66:10001/$image:$a nexus3.onap.org:10001/$image:$a; done ; done
3>批量上传到172.30.1.66:10001
for image in $images; do tagname=$(docker images|grep "/$image " |grep 172 |awk '{print $2}') && for a in $tagname; do docker push 172.30.1.66:10001/$image:$a; done ; done
4>批量导出
docker save -o image.tar $(docker images|grep 172 |awk '{print $1}')
5>全部镜像导出
docker images|tail -n +2|awk '{print $1}'|xargs docker save -o rke-v1135-rancher1-2.tar
15-2 rancher镜像批量打包上传
效果:
rancher/rancher-agent:v2.3.3 -->harbor.onap.cmcc/rancher/rancher-agent:v2.3.3
docker images|tail -n +2|grep "rancher"|sed 's/rancher\//harbor.onap.cmcc\/rancher\//g' |awk '{print "docker tag " $3" ",$1":"$2}'|sh
docker images|grep harbor|awk '{print "docker push " $1":"$2}' >>a.sh
16、打包系统
tar --numeric-owner --exclude=/proc --exclude=/sys -cvf uplooking.centos6.6 / 打包根除了/proc和/sys 属主属组使用PID
docker加载镜像:docker load < uplooking.centos6.6 或者 cat uplooking.centos6.6 |docker import -centos6.6
17、build镜像配置国内apt源
1>ubuntu系统的codename,直接导致你更新的源是否对你的系统起效果,
查看方法: lsb_release -a
如果没有lsb_release命令需要安装
ubuntu: apt-get -y install lsb-core centos: yum install -y redhat-lsb
2>确认阿里源支持 登陆以下网页:http://mirrors.aliyun.com/ubuntu/dists/ 该网页显示了阿里云支持的ubuntu系统下各个Codename版本,确保自己的Codename在该网页中存在(一般都会有的)
3>备份系统源:
sudo mv /etc/apt/sources.list /etc/apt/sources.list_bak
4>添加新的源文件:sudo vi sources.list 并添加以下内容:注意,每一行的trusty应该用第一步查看得到的Codename来代替
(ubuntu14)
deb http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main multiverse restricted universe
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main multiverse restricted universe
(debian)
deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
18、重启docker 容器丢失
场景:需要配置镜像加速器。配置完成后两个容器丢失一个
原因:docker run -d --rm,–rm是在容器restart、stop、或者docker restart情况下创建的容器会被删除
19、pip使用 国内镜像(新版ubuntu要求使用https源,要注意。)
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/
豆瓣:http://pypi.douban.com/simple/
例如:pip install -i http://mirrors.aliyun.com/pypi/simple +软件名称
20、获取容器ip(test是容器名字)
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' test
21、开启docker远程访问
cat /etc/systemd/system/docker.service.d/docker.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
systemctl daemon-reload
systemctl restart docker
curl 127.0.0.1:2375/version
22、docker的oom功能
oom_kill_disable 0设置为0说明容器申请内存超出允许内存不会被oomkiller终止
cat /sys/fs/cgroup/memory/docker/memory.oom_control
oom_kill_disable 0
23、代理环境下容器连接外网的方法
https://docs.docker.com/network/proxy/
配置Docker客户端:
在Docker客户端上,~/.docker/config.json在启动容器的用户的主目录中创建或编辑该文件。添加如下所示的JSON,用httpsProxy或ftpProxy在必要时替换代理类型,并替换代理服务器的地址和端口。您可以同时配置多个代理服务器。您可以选择通过将noProxy密钥设置为一个或多个以逗号分隔的IP地址或主机来排除主机或范围通过代理服务器。支持使用该*字符作为通配符,如此示例所示
{
"proxies":
{
"default":
{
"httpProxy": "http://127.0.0.1:3001",
"httpsProxy": "http://127.0.0.1:3001",
"noProxy": "*.test.example.com,.example2.com"
}
}
}
24、如何找到docker中占用磁盘空间最多的容器
操作步骤
1、磁盘空间(文件系统)状态
df -Th /
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 99G 70G 25G 75% /
2、统计目录占用磁盘空间
du -h --max-depth=1 /
发现最大目录依次如下列表:
/var/
/var/lib/
/var/lib/docker/
/var/lib/docker/overlay2/
cd /var/lib/docker/overlay2/
du -h --max-depth=1 >>~/disk.txt
cat ~/disk.txt|grep G
最终找到 174G ./ae3c8de6f0340d988ef50bb6be1b8662ebc5573e1cce035b00abc5062b7af3e2
这个目录是某个容器临时存储层目录,其生命周期取决于这个容器的生命周期,目录的名称也是临时存储层的ID,我们可以根据这个ID找到目标容器
3、找到这个容器
for c in `docker ps -qa`; \
do \
docker inspect $c \
| grep -i 'ae3c8de6f0340d988ef50bb6be1b8662ebc5573e1cce035b00abc5062b7af3e2' && \
echo $c; \
done
返回结果
5e9b2fbe465e
4、根据容器id找到容器名称并进行恢复
docker ps -a|grep 5e9b2fbe465e
看名称是k8s_consul_dev-consul-consul这个容器,consul产生的大量临时文件导致磁盘占用过高,清理临时文件即可
25、批量打tag上传到本地仓库
需求:
192.168.121.102:10001/rancher/hyperkube:v1.13.5-rancher1
替换成
10.0.0.6:10000/rancher/hyperkube:v1.13.5-rancher1
执行重新tag命令
docker images|tail -n +2|awk '{print $1":"$2}'|awk -F '10001' '{print "docker tag "$1"10001"$2 " 10.0.0.6:10001"$2}'|bash
批量上传:
docker images|grep 10.0.0.6|awk '{print "docker push "$1":"$2" &"}'|bash
批量删除镜像:
docker images|tail -n +2|grep 10.0.0.6|awk '{print $3}'|xargs docker rmi -f
26、镜像调试 sleep 作为守护进程
docker run -itd --name busybox-test busybox:latest /bin/sh -c "while true; do sleep 3600; done"