Docker容器以代码0退出原因及Botkit开发Messenger Bot Docker部署问题
Hey there! Let's walk through your Botkit Messenger Bot setup with Docker, and figure out why your container's exiting with code 0.
Your Current File Structure
First, let's clarify your project layout for reference:
bot/ ├── README.md ├── docker/ │ ├── botkit/ │ │ └── Dockerfile │ └── node/ │ └── Dockerfile ├── docker-compose.yml ├── node_modules/ └── package.json
Fixing Your Docker Configuration
Since your docker-compose.yml is incomplete, here's a practical, adjusted setup tailored to Botkit projects that will work across your machines:
Updated docker-compose.yml
version: "2" services: # This service holds your app code as a reusable volume app: image: busybox volumes: - .:/app - /app/node_modules # Isolates container node_modules from your local folder node: build: ./docker/node volumes_from: - app working_dir: /app # Run your Botkit app's entry point (matches the script in package.json) command: npm start # Expose the port Botkit uses (default is 3000 for most setups) ports: - "3000:3000" # Add Messenger Bot credentials as environment variables environment: - PAGE_ACCESS_TOKEN=your_page_token_here - VERIFY_TOKEN=your_verify_token_here
Tips for Your Dockerfiles
./docker/node/Dockerfile
Botkit runs on Node.js, so use an LTS Node version (18 or 20 work best) and optimize for Docker cache:
FROM node:20-alpine # Install system dependencies (like git for npm packages) if needed RUN apk add --no-cache git WORKDIR /app # Copy package.json first to cache npm install layers COPY package.json . RUN npm install # Your full code is mounted via docker-compose volumes, so no need to copy it here
Note: The botkit/Dockerfile is likely unnecessary—installing Botkit via npm in your Node image is sufficient for most setups.
Why Does My Docker Container Exit with Code 0?
Exit code 0 actually means your container ran successfully... and then finished its process. Here's the most common reasons this happens with Botkit apps:
- Your command isn't a long-lived process: Botkit needs to run a persistent web server or listener for Messenger events. If your
commandis something likenpm run build(which finishes once done) instead ofnpm start(which runs the server), the container will exit immediately after the task completes. - Missing required environment variables: If Botkit can't find credentials like
PAGE_ACCESS_TOKEN, it might fail silently and exit cleanly. Always check logs to confirm. - Volume mounting issues: If your code isn't properly mounted into the container, the entrypoint script (e.g.,
bot.js) might not exist, causing the container to exit right away.
How to Fix It
- Validate your package.json start script: Make sure it points to your Botkit entry file, like:
"scripts": { "start": "node bot.js" } - Check container logs: Run
docker-compose logs nodeto see exactly what's happening before the container exits. This will reveal any errors or Botkit logs that explain the exit. - Confirm environment variables: Double-check that all required tokens are set in
docker-compose.yml, or use a.envfile withenv_file: .envin the node service for cleaner credential management.
内容的提问来源于stack exchange,提问作者Gane D. Geoffrey

