Docker for Windows 中“executable file not found”和“no such file”问题

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: 在使用 Docker for Windows 时,遇到“executable file not found”和“no such file or directory”问题的原因与解决方法

screenshot

引子

昨天收到一封读者来信,他参考“使用Docker Compose部署Redis集群“一文,在Windows环境中执行 docker-compose up 命令时遇到了下面的问题

ERROR: for sentinel  Cannot start service sentinel: oci runtime error: container_linux.go:247: starting container proces
s caused "exec: \"sentinel-entrypoint.sh\": executable file not found in $PATH"

其中“sentinel”容器的 Dockerfile 如下,

FROM redis:3

MAINTAINER Li Yi <denverdino@gmail.com>

EXPOSE 26379
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
ENV SENTINEL_QUORUM 2
ENV SENTINEL_DOWN_AFTER 30000
ENV SENTINEL_FAILOVER 180000
COPY sentinel-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["sentinel-entrypoint.sh"]

这个问题在Mac/Linux上不存在,因为sentinel-entrypoint.sh文件已经拥有了可执行权限。为了解决Windows环境中文件权限的缺失,我做了第一个修正,在Dockerfile中为sentinel-entrypoint.sh 添加可执行权限。Github commit地址 ,修改片段如下

COPY sentinel-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
ENTRYPOINT ["sentinel-entrypoint.sh"]

令人困惑的CRLF

然而很快他又反馈,虽然上述问题已经修正,但是sentinel容器依然启动失败,在执行docker logs rediscluster_sentinel_1 命令时,得到如下异常。

standard_init_linux.go:175: exec user process caused "no such file or directory"

因为手头只有Mac和Linux环境,无法复现问题。直到晚上回家后找了一台Windows机器,观察了一下才有了头绪。因为sentinel-entrypoint.sh中最后一行的结束符成了\r\n 导致Docker镜像中的Linux Shell脚本文件无法执行。

#!/bin/sh

sed -i "s/\$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf
sed -i "s/\$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf
sed -i "s/\$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf

exec docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel

Linux/Mac的结束符是\n,所以Shell解释器会将脚本文件中的\r作为命令行的一部分来执行。这就是出现 “no such file or directory” 的原因。

定位问题之后,修正也很简单。我的做法是利用 dos2unix sentinel-entrypoint.sh命令将其转换成为unix格式文件。 这样问题就解决了。

问题的根源是Git for Windows默认会将文本文件中的换行符强制转为DOS格式。这可以通过修改缺省设置 git config --global core.autocrlf false 再重新git clone项目来解决。但如果使用文本编辑器修改过文件,还是需要利用dos2unix转换修正换行符。

总结

用Google搜了一下,这两个问题在Windows上构建Docker镜像时中非常普遍,即使很多官方镜像也无法直接在Windows上编译成功。Windows的真爱粉在使用Docker时可以使用文中方法解决这些问题。

感谢大家在2016年的支持,2017新年快乐!

想了解更多容器服务内容,请访问 https://www.aliyun.com/product/containerservice

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
3月前
|
Linux 虚拟化 Docker
Windows10安装Docker Desktop(大妈看了都会)
Windows10安装Docker Desktop(大妈看了都会)
730 2
|
20天前
|
搜索推荐 应用服务中间件 nginx
docker与containerd镜像获取及导出导入的区别与注意事项(报错信息:ctr: content digest sha256........ac47: not found)
docker与containerd镜像获取及导出导入的区别与注意事项(报错信息:ctr: content digest sha256........ac47: not found)
|
4天前
|
JavaScript 前端开发 Docker
拿下奇怪的前端报错(二):nvm不可用报错`GLIBC_2.27‘‘GLIBCXX_3.4.20‘not Found?+ 使用docker构建多个前端项目实践
本文介绍了在多版本Node.js环境中使用nvm进行版本管理和遇到的问题,以及通过Docker化构建流程来解决兼容性问题的方法。文中详细描述了构建Docker镜像、启动临时容器复制构建产物的具体步骤,有效解决了不同项目对Node.js版本的不同需求。
|
1月前
|
Docker 容器
14 response from daemon: open \\.\pipe\docker_engine_linux: The system cannot find the file speci
14 response from daemon: open \\.\pipe\docker_engine_linux: The system cannot find the file speci
24 1
|
2月前
|
PHP 虚拟化 Docker
Docker——windows10安装Docker Desktop
Docker——windows10安装Docker Desktop
279 1
|
2月前
|
存储 Linux Windows
【应用服务 App Service】App Service For Windows 如何挂载Storage Account File Share 示例
【应用服务 App Service】App Service For Windows 如何挂载Storage Account File Share 示例
|
2月前
|
存储 Linux Docker
【Azure 应用服务】应用服务中发布Docker Container,如何添加卷(如Azure File Share)以便永久存储文件
【Azure 应用服务】应用服务中发布Docker Container,如何添加卷(如Azure File Share)以便永久存储文件
|
2月前
|
开发框架 JavaScript .NET
【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found
【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found
|
2月前
|
Linux Python Windows
【Azure 环境】Windows中安装Python azure-eventhub-checkpointstoreblob-aio模块时出错 ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:
【Azure 环境】Windows中安装Python azure-eventhub-checkpointstoreblob-aio模块时出错 ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory:
|
2月前
|
Linux 虚拟化 Docker
深入了解Windows安装Docker
【8月更文挑战第22天】深入了解Windows安装Docker
131 0