Docker容器测试-常见问题+解决

前言

问题1:创建 nginx 容器尝试挂载 nginx.conf 文件时报错: mounting “/root/nginx.conf” to rootfs at “/etc/nginx/nginx.conf” caused: mount through procfd: not a directory:

在自己的服务器上想通过 nginx 镜像创建容器,并挂载镜像自带的 nginx.conf 文件

docker run -it -d -v ~/nginx.conf:/etc/nginx/nginx.conf nginx

报错:

[root@poloyy ~]# docker run -it -d -v ~/nginx.conf:/etc/nginx/nginx.conf nginx
e0e4b40446a64927603b85854c3a6472b2dfa5681fcbfa0e170c16b15e5c8fdd
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:76: mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
[root@poloyy ~]# client_loop: send disconnect: Broken pipe

提取关键错误信息:

mounting "/root/nginx.conf" to rootfs at "/etc/nginx/nginx.conf" caused: mount through procfd: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)?

将“/root/nginx.conf”挂载到“/etc/nginx/nginx.conf”的rootfs导致:通过procfd挂载:不是目录:未知:您是否试图将目录挂载到文件上(反之亦然)

根因:
不支持直接挂载文件,只能挂载文件夹
想要挂载文件,必须宿主机也要有对应的同名文件

解决方法:
可以先不挂载 nginx.conf
先从容器中复制 nginx.conf 出来
然后可以自行修改 nginx.conf,自定义配置项
创建正式使用的 nginx 容器

从 test 容器中复制 nginx.conf 出来
当然也可以去网上随便找个 nginx.conf,最重要的是宿主机要有个 nginx.conf

docker run --name test -d nginx  
docker cp test:/etc/nginx/nginx.conf /data/

创建正式的 nginx 容器,挂载 nginx.conf 文件
可以赋予权限

docker run --privileged -it -p 80:80 \
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
-v /data/nginx/html:/usr/share/nginx/html:rw \
-v /data/nginx/logs:/var/log/nginx -d nginx

问题2:运行 Mysql 容器后报错:[ERROR] InnoDB: redo log file ‘./ib_logfile0’ exists

在本机 mac docker 运行启动 mysql 容器

docker run -d -p 3306:3306 --name mysql1 -v /Users/polo/data/conf:/etc/mysql/conf.d -v /Users/polo/data/mysql:/var/lib/mysql  -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

现象:
查看容器日志发现报错了,本地 navicat 也连不上

A1

重点就是第一句,某某某文件已存在,通常就是环境被覆盖了

根因:
之前通过 brew install 了 mysql,虽然 uninstall 了,但是本地文件并没有被删除

解决方案:

brew uninstall mysql
rm -rf /usr/local/var/mysql 

先卸载 mysql,再删掉本地的文件即可

正常启动 Mysql 容器的日志

A2

没有 ERROR 日志

问题3:容器内获取的时间和主机的时间不一样的问题

分别在容器和主机下执行 date 命令

A3

可以看到,时间是完全不一样的

解决方案:
方法1:
在运行容器时,挂载 /etc/localtime 目录

docker run -d -v /etc/localtime:/etc/localtime:ro --name git 

重点就是: -v /etc/localtime:/etc/localtime:ro

方法2:

docker cp /etc/localtime [容器ID或名字]:/etc/localtime

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

 

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值