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

Docker中用pm2运行Node.js/Swagger应用:Api.yaml打开失败

Docker + PM2 + Express: Fixing Swagger Api.yaml Path Mismatch

I’ve run into almost identical issues when containerizing Node.js apps with PM2 and Swagger—this usually boils down to mismatched working directories or hardcoded absolute paths that don’t translate to the Docker environment. Let’s walk through the fixes step by step:

1. Fix Dockerfile Workdir & Copy Paths

First, make sure your Dockerfile is setting the correct working directory and copying your build files to the right location. Based on your error, your Api.yaml lives at /build/server/common/swagger/Api.yaml locally, so your Dockerfile should mirror that structure inside the container:

# Example Dockerfile snippet (adjust based on your build process)
# After compiling/packaging your app
COPY ./build/server /server
WORKDIR /server

# Optional debug step to confirm file exists
RUN ls -la common/swagger/Api.yaml

This ensures when your app runs, the relative path ./common/swagger/Api.yaml points to the correct file inside the container.

2. Set PM2’s Working Directory

PM2 can default to the root directory if you don’t specify cwd in your config, which breaks relative file paths. Update your pm2.json to explicitly set the working directory to match your Docker WORKDIR:

{
  "apps": [
    {
      "name": "express-server",
      "script": "./index.js", // Replace with your app's entry point
      "cwd": "/server", // Match the WORKDIR set in Dockerfile
      "env": {
        "NODE_ENV": "production"
      }
    }
  ]
}

3. Use Dynamic Paths in Your Code

Stop using hardcoded absolute paths for Api.yaml—instead, use Node.js’s path module to resolve the file relative to your app’s entry point. This works consistently both locally and in Docker.

For example, if loading the Swagger spec with yamljs:

const path = require('path');
const yaml = require('yamljs');

// Resolve path relative to the current file's directory
const swaggerDocument = yaml.load(path.resolve(__dirname, './common/swagger/Api.yaml'));

Or if using swagger-jsdoc:

const path = require('path');
const swaggerJsdoc = require('swagger-jsdoc');

const options = {
  swaggerDefinition: { /* Your Swagger spec configuration */ },
  apis: [path.resolve(__dirname, './common/swagger/Api.yaml')]
};

const swaggerSpec = swaggerJsdoc(options);

4. Verify the Fix

After making these changes:

  • Rebuild your Docker image: docker build -t your-server-image .
  • Run the container: docker run -p 3000:3000 your-server-image
  • Test the Swagger endpoint: curl 0.0.0.0:3000/api/v1/spec

If you still hit issues, exec into the container to double-check paths:

docker exec -it <container-id> bash
# Check current working directory
pwd
# Confirm the file exists at the expected path
ls -la common/swagger/Api.yaml

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:13:21