Windows 10下Docker运行Elasticsearch失败:端口无法访问求助
docker-compose up Alright, let's figure out why you can't hit those 9200/9300 ports after running docker-compose up on your Windows 10 machine. Here's what to check and fix step by step:
1. First: Is your myapp service actually running Elasticsearch?
Looking at your docker-compose.yml, you're building a custom myapp service from a local Dockerfile—but you mentioned you're trying to start Elasticsearch. If that Dockerfile doesn't use an Elasticsearch base image (like FROM elasticsearch:7.x or FROM docker.elastic.co/elasticsearch/elasticsearch:8.x), then your container isn't running Elasticsearch at all. No wonder the ports are unresponsive!
- Open up
./docker/Dockerfileand check its first line. If it's not pulling an Elasticsearch image, you'll need to adjust the Dockerfile to build on top of one, or swap out the custom build for the official Elasticsearch image directly in your compose file.
2. Verify your container is actually running
Even if your Dockerfile is correct, containers can fail silently. Let's confirm status:
- Run
docker psin your terminal. Ifmyappshows up asExitedorRestarting, it crashed on startup. - Grab the container logs to find the root cause:
docker logs myapp. Common issues here include insufficient memory (Elasticsearch needs a decent amount) or misconfigured Elasticsearch settings. - Double-check Windows Defender Firewall: Sometimes it blocks traffic to Docker's mapped ports. Try temporarily disabling it to test, or add a rule allowing inbound/outbound traffic on 9200 and 9300 for the
docker-proxyprocess.
3. Fix your docker-compose config (for quick Elasticsearch setup)
If your goal is just to get Elasticsearch running, skip the custom build for now and use the official image. Here's an adjusted version of your config that should work:
version: "2" services: elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch:7.17.10 # Pick a compatible version container_name: elasticsearch hostname: elasticsearch volumes: - ../data:/usr/share/elasticsearch/data # Specify the exact mount path for persistence networks: - app-test ports: - "9200:9200" - "9300:9300" environment: - discovery.type=single-node # Required for single-node test setup - ES_JAVA_OPTS=-Xms512m -Xmx512m # Tune memory to avoid crashes on Windows - ES_CONNECT_RETRY=120 - network.host=0.0.0.0 # Ensure Elasticsearch binds to all interfaces networks: app-test: driver: bridge
4. Test access the right way
On Windows, localhost:9200 can sometimes have network quirks with Docker Desktop. Try accessing via the container's direct IP:
- Get the container IP with:
docker inspect myapp | findstr "IPAddress" - Visit
http://<container-ip>:9200in your browser or usecurl <container-ip>:9200in PowerShell.
内容的提问来源于stack exchange,提问作者user1883212

