You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建镜像时出现‘Directory nonexistent’错误,如何解决?

问题描述

给定Dockerfile内容如下:

FROM sauloefo/my-ubuntu-22.04:v1

RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.2

RUN echo 'source ~/.asdf/asdf.fish' >> ~/.config/fish/config.fish

执行docker build命令时出现如下错误:

> [3/3] RUN echo 'source ~/.asdf/asdf.fish' >> ~/.config/fish/config.fish:
#6 0.372 /bin/sh: 1: cannot create /root/.config/fish/config.fish: Directory nonexistent

但直接从基础镜像sauloefo/my-ubuntu-22.04:v1创建容器时,/root/.config/fish/config.fish文件是存在的,请问忽略了什么?

原因分析与解决方案

问题核心是Docker的RUN指令默认使用/bin/sh执行命令,而非fish shell。

基础镜像中/root/.config/fish/config.fish的存在,是因为用该镜像创建容器时,默认登录shell是fish,启动时会自动初始化并生成该目录及文件。但在docker build过程中,RUN指令默认调用/bin/sh,不会触发fish的初始化流程,导致/root/.config/fish目录不存在,无法写入文件。

有两种解决方式:

  • 方式一:先创建目标目录,再追加内容
FROM sauloefo/my-ubuntu-22.04:v1

RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.2

RUN mkdir -p ~/.config/fish && echo 'source ~/.asdf/asdf.fish' >> ~/.config/fish/config.fish
  • 方式二:指定用fish执行该RUN命令
FROM sauloefo/my-ubuntu-22.04:v1

RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.2

RUN fish -c "echo 'source ~/.asdf/asdf.fish' >> ~/.config/fish/config.fish"

内容的提问来源于stack exchange,提问作者Saulo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 02:20:30