Docker启动MongoDB应用失败求助:启动流程疑问与端口27017占用问题
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 :27017Open 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
lsofisn't installed, runsudo apt install lsoffirst.
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:
Validate Your Docker Compose File
- Check the volume path:
/mongo:/data/dbpoints 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/dbinstead. - For better performance in WSL, stick to directories inside the WSL filesystem (avoid mapped Windows folders when possible).
- Check the volume path:
Launch the Services
Open your WSL terminal, navigate to your project directory:cd /mnt/c/proyect-shopStart the containers in detached mode (background):
docker compose up -d # Use `docker-compose up -d` if you have an older Docker Compose versionCheck Container Status
Confirm if your MongoDB container is running:docker compose ps # Or check all containers system-wide: docker psTroubleshoot 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-databaseAdd Your Own App to Docker Compose
If you have a backend/frontend app, add it as another service in yourdocker-compose.yaml. For example, a Node.js API with aDockerfilein 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 nameRun
docker compose up -d --buildto build and start all services together.
Quick Tips for WSL + Docker
- Volume Permissions: If you get permission errors with the
/mongovolume, 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

