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

Docker启动MongoDB应用失败求助:启动流程疑问与端口27017占用问题

Troubleshooting Docker Compose & MongoDB Port Conflict + Basic Deployment Guide

Hey Jackson! Let's work through your Docker issues one by one—you're already halfway there since you got the getting-started container running, so we just need to fix the port conflict and clarify how to launch your own apps with Docker Compose.

First: Fix the Port 27017 Conflict

The error message spells it out clearly: "端口不可用:暴露TCP端口0.0.0.0:27017 -> 0.0.0.0:0失败:监听tcp 0.0.0.0:27017时绑定失败:通常每个套接字地址(协议/网络地址/端口)只允许使用一次"

This means port 27017 on your host machine is already being used by another program—most likely a local MongoDB instance you have running, or another background service. Here are three straightforward fixes:

Option 1: Kill the Process Using Port 27017

  • On Windows (Command Prompt/PowerShell):
    Run this to find the process ID (PID) using the port:

    netstat -ano | findstr :27017
    

    Open Task Manager, go to the "Details" tab, find that PID, right-click it, and select "End Task".

  • In WSL/Ubuntu:
    Use these commands to locate and stop the process:

    sudo lsof -i :27017  # Finds the process using the port
    sudo kill -9 <PID_FROM_PREVIOUS_COMMAND>
    

    If lsof isn't installed, run sudo apt install lsof first.

Option 2: Change the Host Port in Docker Compose

Instead of mapping host port 27017 to container port 27017, use an unused port like 27018 on your host. Update your docker-compose.yaml like this:

version: '2'
services:
  teslodb:
    image: mongo:5.0.0
    container_name: teslo-database
    ports:
      - 27018:27017  # Host port 27018 maps to container's 27017
    volumes:
      - /mongo:/data/db

Now you can access MongoDB from your host using mongodb://localhost:27018.

Option 3: Remove the Port Mapping (If Host Access Isn't Needed)

If your app will run in another Docker container (not directly from your host), you can delete the ports section entirely. Containers in the same Docker network can communicate with each other using the service name teslodb and port 27017 without exposing the port to your host.


Second: Basic Guide to Launching Your Own App with Docker Compose

Since you already have a MongoDB service defined, here's a step-by-step breakdown to get your full stack running smoothly:

  1. Validate Your Docker Compose File

    • Check the volume path: /mongo:/data/db points to a directory in your WSL Ubuntu instance. If you want to map to a Windows folder, use a WSL path like /mnt/c/your-windows-project-folder/mongo:/data/db instead.
    • For better performance in WSL, stick to directories inside the WSL filesystem (avoid mapped Windows folders when possible).
  2. Launch the Services
    Open your WSL terminal, navigate to your project directory:

    cd /mnt/c/proyect-shop
    

    Start the containers in detached mode (background):

    docker compose up -d  # Use `docker-compose up -d` if you have an older Docker Compose version
    
  3. Check Container Status
    Confirm if your MongoDB container is running:

    docker compose ps
    # Or check all containers system-wide:
    docker ps
    
  4. Troubleshoot with Logs
    If the container fails to start, check the logs for detailed error messages:

    docker compose logs teslodb
    # Or use the container name directly:
    docker logs teslo-database
    
  5. Add Your Own App to Docker Compose
    If you have a backend/frontend app, add it as another service in your docker-compose.yaml. For example, a Node.js API with a Dockerfile in your project root:

    version: '2'
    services:
      teslodb:
        image: mongo:5.0.0
        container_name: teslo-database
        ports:
          - 27018:27017
        volumes:
          - ./mongo:/data/db
      teslo-api:
        build: .  # Builds from the Dockerfile in the current directory
        container_name: teslo-api
        ports:
          - 3000:3000
        depends_on:
          - teslodb  # Ensures MongoDB starts before your API
        environment:
          - MONGO_URI=mongodb://teslodb:27017/teslo-db  # Access MongoDB via service name
    

    Run docker compose up -d --build to build and start all services together.


Quick Tips for WSL + Docker

  • Volume Permissions: If you get permission errors with the /mongo volume, create the directory in WSL and set the correct ownership (MongoDB uses UID 999):
    mkdir -p ./mongo
    sudo chown -R 999:999 ./mongo
    
  • Use WSL Terminal Exclusively: For Docker commands in WSL, stick to the Ubuntu terminal instead of Windows Command Prompt/PowerShell to avoid path and permission mishaps.

If you run into more specific errors (like build failures or connection issues between your app and MongoDB), share the relevant logs and we can dig deeper!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 15:07:33