docker杂记

docker build时无法yum安装包,因为无法访问外网,无法ping通外网。

解决办法宿主机执行:

systemctl stop NetworkManager.service
firewall-cmd --permanent --zone=trusted --change-interface=docker0
systemctl start NetworkManager.service

#yum clean all && yum makecache

#删除不必要的报错yum源
systemctl restart docker.service

在Dockerfile中增加调试命令:

COPY ./Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo

#fastestmirror.conf修改成enabled=0

COPY ./fastestmirror.conf   /etc/yum/pluginconf.d/fastestmirror.conf

#yum.conf修改成plugins=0
COPY ./yum.conf /etc/yum.conf

RUN ls /etc/yum.repos.d/

RUN ping -c 3  ping mirrors.aliyun.com
COPY ./hosts /etc/hosts
RUN cat /etc/hosts
RUN cat /etc/resolv.conf

在执行docker build时指定使用主机网络:

docker build --network=host --progress=plain --no-cache -t mynginx:v1.0 .

docker save mynginx:v1.0 -o mynginx_v1.0.tar

另一种方式:build时不用yum,全量下载rpm包

通过yum将依赖的rpm包都下载到指定目录(系统已有的依赖不下载):

yum reinstall mypackagename --downloadonly --downloaddir=./yum-downloaddir/

通过yum将依赖的rpm包都下载到指定目录(下载全部依赖):

#下载
yum -y install yum-utils
repotrack mypackagename
#安装
rpm -Uvhi --force --nodeps *.rpm

dockerd启动报错:failed: iptables: No chain/target/match by that name.

firewalld[717]: ERROR: ZONE_CONFLICT: 'docker0' already bound to a zone

failed to start daemon: Error initializing network controller: error creating default "bridge" network: cannot create network 99980437d225378945a203704227c1da34b034388f432024b510ab3a94f6735f (docker0): conflicts with network 07443d7857d6eccfe85e622e7cfc779ba7f1d2f0778c67ed86c5fafac1cf318f (docker0): networks have same bridge name

解决办法:

systemctl stop firewalld

ifconfig docker0 down

yum -y install bridge-utils

brctl delbr docker0

rm -rf /var/lib/docker/network

docker执行报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

解决办法:

编辑文件:/usr/lib/systemd/system/docker.service

#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

将dockerd启动命令修改为:
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock --containerd=/run/containerd/containerd.sock

容器内获取本机IP:

$ hostname -i
10.244.1.2
$ 
$ awk '/32 host LOCAL/ { print i } {i=$2}' /proc/net/fib_trie
10.244.1.2
127.0.0.1
10.244.1.2
127.0.0.1
$ awk '/32 host LOCAL/ { print i } {i=$2}' /proc/net/fib_trie | grep -v '127.0.0.1' | sort | uniq
10.244.1.2

容器内不借助ipvsadm查看ipvs的情况:

$ cat /proc/net/ip_vs 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP  0A6D1102:1F90 rr  
  -> 0AF40003:1F40      Masq    1      0          0         
  -> 0AF40102:1F40      Masq    1      0          0         
TCP  0A600001:01BB rr  
  -> C0A84302:20FB      Masq    1      0          0         
UDP  0A60000A:0035 rr  
TCP  0A60000A:0035 rr  
TCP  C0A84302:7918 rr  
  -> 0AF40003:1F40      Masq    1      0          0         
TCP  0AF40001:7918 rr  
  -> 0AF40003:1F40      Masq    1      0          0         
TCP  AC110001:7918 rr  
  -> 0AF40003:1F40      Masq    1      0          0         
TCP  0A60000A:23C1 rr  
TCP  0A6D1102:1F90 rr  
  -> 0AF40102:1F40      Masq    1      0          0         
  -> 0AF40003:1F40      Masq    1      0          0         
TCP  0A60000A:0035 rr  
UDP  0A60000A:0035 rr  
TCP  0A600001:01BB rr  
  -> C0A84302:20FB      Masq    1      0          6         
TCP  0A60000A:23C1 rr  
TCP  C0A84303:7918 rr  
  -> 0AF40102:1F40      Masq    1      0          0         
TCP  AC110001:7918 rr  
  -> 0AF40102:1F40      Masq    1      0          0         
TCP  0AF40101:7918 rr  
  -> 0AF40102:1F40      Masq    1      0          0         

$ cat /proc/net/ip_vs_stats
   Total Incoming Outgoing         Incoming         Outgoing
   Conns  Packets  Packets            Bytes            Bytes
       3       12        C              4B6              40B

 Conns/s   Pkts/s   Pkts/s          Bytes/s          Bytes/s
       0        0        0                0                0

然后将16进制IP转为10进制:

#16进制转10进制
echo "C0A84303" | sed -e 's/\(..\)/\1\n/g' | head -4 | awk '{printf("%d.","0x"$1)}' | sed 's/\.$//'

#10进制转16进制
echo "192.168.67.3" | awk -F "." '{printf("%02X%02X%02X%02X\n",$1,$2,$3,$4)}'

在宿主机perf监测容器内进程的性能:

#安装bindfs工具
yum install -y bindfs

#容器内进程在宿主机的pid
PID=$(pidof gohello)

#挂载容器fs
mkdir /tmp/foo
bindfs /proc/$PID/root /tmp/foo

#perf设置符号fs
perf report --symfs /tmp/foo

#使用完成卸载fs
umount /tmp/foo

容器启动报错:Failed to get D-Bus connection: Operation not permitted

原因:需要开特权privileged并启动/usr/sbin/init,而启动init报错:Couldn't find an alternative telinit implementation to spawn.

原因:需要只读挂载/sys/fs/cgroup。

解决办法:

docker run --privileged=true -v /sys/fs/cgroup:/sys/fs/cgroup:ro --net=host -d app:v1 /usr/sbin/init

容器内keepalived报错:el7 Keepalived_healthcheckers[88]: IPVS: Can't initialize ipvs: Protocol not available

解决办法在宿主机执行:

#!/bin/sh

have=`lsmod | grep ip_vs | wc -l`
if [ "$have" == "0" ]; then
        echo "install ip_vs mod"
        sudo modprobe ip_vs
fi

ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

解决:

#查看sock权限
ll /var/run/docker.sock 
#srw-rw---- 1 root docker 0 9月   6 15:17 /var/run/docker.sock

#查看用户所属组
id work
#uid=1000(work) gid=1000(work) 组=1000(work)

#给用户添加附属组
sudo usermod -aG docker work

#再次查看用户所属组
id work
#uid=1000(work) gid=1000(work) 组=1000(work),992(docker)

echo 注意:登出用户再登录用户才能生效!!!

非交互方式登录并push镜像:

#!/bin/bash
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin registry.example.com
docker push USER/REPO

docker build报错:failed to solve: failed to compute cache key: failed to calculate checksum of ref ***

两种解决办法:

1、缓存失效,在docker build时增加 --no-cache 参数。

2、检查目录结构,报错文件的引用位置是否正确。

容器启动报错:exec /gen_file: no such file or directory

原因:并非 /gen_file 不存在,而是里面引用的动态库不存在。

解决:

CGO_ENABLED=0 go build gen_file.go

CGO_ENABLED=0 的核心作用是让 Go 程序完全脱离 C 代码和系统 C 库,生成独立、可移植的静态链接二进制,是构建跨平台、低依赖应用的常用配置。

docker run 覆盖 Dockerfile 中的 ENTRYPOINT:

覆盖 ENTRYPOINT 语法:

docker run [optional:value] --entrypoint [new_command] [docker_image] [arg...]

示例:

docker run -d --entrypoint /bin/sleep image_url:image_tag 86400

--end--

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值