Docker构建React应用报错:无法找到Dockerfile文件求助
问题排查:Docker构建React应用时找不到Dockerfile
环境与文件信息
项目文件夹结构
build/ docker-compose.yml package.json src/ componants/ dockerfile pages/ tsconfig.json compose.yaml gulpfile.js pnpm-lock.yaml yarn.lock docker-compose.debug.yml node_modules/ public/
package.json 内容
{"name": "dylansteeletech","version": "0.1.0","private": true,"dependencies": {"@testing-library/jest-dom": "^5.14.1","@testing-library/react": "^13.0.0","@testing-library/user-event": "^13.2.1","@types/jest": "^27.0.1","@types/node": "^16.7.13","@types/react": "^18.0.0","@types/react-dom": "^18.0.0","react": "^18.2.0","react-dom": "^18.2.0","react-markdown": "^8.0.7","react-router-dom": "^6.14.1","react-scripts": "5.0.1","typescript": "^4.4.2","web-vitals": "^2.1.0"},"scripts": {"start": "react-scripts start","build": "react-scripts build","test": "react-scripts test","eject": "react-scripts eject"},"eslintConfig": {"extends": ["react-app","react-app/jest"]},"browserslist": {"production": [">0.2%","not dead","not op_mini all"],"development": ["last 1 chrome version","last 1 firefox version","last 1 safari version"]},"devDependencies": {"gulp": "^4.0.2"}}
构建命令与错误信息
执行命令:
docker build -t dylansteeletech .
报错内容:
$ docker build -t dylansteeletech . [+] Building 0.5s (2/2) FINISHED docker:default => [internal] load build definition from Dockerfile 0.3s => => transferring dockerfile: 2B 0.0s => [internal] load .dockerignore 0.2s => => transferring context: 2B 0.0s ERROR: failed to solve: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount998491540/Dockerfile: no such file or directory View build details: docker-desktop://dashboard/build/default/default/fr6ylromwvv4me4vkxmqmgg01
当前Dockerfile内容
# syntax=docker/dockerfile:1 # Comments are provided throughout this file to help you get started. # If you need more help, visit the Dockerfile reference guide at # https://docs.docker.com/engine/reference/builder/ ARG NODE_VERSION=18.16.1 ARG PNPM_VERSION=8.6.6 ################################################################################ # Use node image for base image for all stages. FROM node:${NODE_VERSION}-alpine as base # Set working directory for all build stages. WORKDIR /usr/src/app # Install pnpm. RUN --mount=type=cache,target=/root/.npm \ npm install -g pnpm@${PNPM_VERSION} ################################################################################ # Create a stage for installing production dependecies. FROM base as deps # Download dependencies as a separate step to take advantage of Docker's caching. # Leverage a cache mount to /root/.local/share/pnpm/store to speed up subsequent builds. # Leverage bind mounts to package.json and pnpm-lock.yaml to avoid having to copy them # into this layer. RUN --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ --mount=type=cache,target=/root/.local/share/pnpm/store \ npm install --prod --frozen-lockfile ################################################################################ # Create a stage for building the application. FROM deps as build # Download additional development dependencies before building, as some projects require # "devDependencies" to be installed to build. If you don't need this, remove this step. RUN --mount=type=bind,source=package.json,target=package.json \ --mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \ --mount=type=cache,target=/root/.local/share/pnpm/store \ npm install --frozen-lockfile # Copy the rest of the source files into the image. COPY . . # Run the build script. RUN npm run build ################################################################################ # Create a new stage to run the application with minimal runtime dependencies # where the necessary files are copied from the build stage. FROM base as final # Use production node environment by default. ENV NODE_ENV production # Run the application as a non-root user. USER node # Copy package.json so that package manager commands can be used. COPY package.json . # Copy the production dependencies from the deps stage and also # the built application from the build stage into the image. COPY --from=deps /usr/src/app/node_modules ./node_modules COPY --from=build /usr/src/app/build ./build # Expose the port that the application listens on. EXPOSE 3000 # Run the application. CMD npm start
问题原因与解决方案
核心原因
Docker默认会在当前目录查找首字母大写的Dockerfile,但你的文件名为全小写的dockerfile,在大小写敏感的文件系统(如Linux或Docker Desktop默认配置)下,两者会被识别为不同文件,导致构建时找不到指定文件。
解决方法
方法1:重命名Dockerfile
将项目根目录下的dockerfile重命名为Dockerfile(首字母大写),之后直接执行原构建命令即可。
方法2:构建时指定文件路径
如果不想修改文件名,执行构建命令时通过-f参数指定Dockerfile的路径:
docker build -t dylansteeletech -f dockerfile .
额外检查点
- 确认执行构建命令时,终端的当前工作目录是项目根目录(即包含
dockerfile的目录) - 检查
.dockerignore文件,确保没有将dockerfile或Dockerfile添加到忽略列表中
内容的提问来源于stack exchange,提问作者Dylan Steele
相关产品推荐
相关产品推荐

