Solving the “No Alive Nodes Found” Error in Magento2 on Docker
Understanding the Issue
When setting up Magento2 on a Docker environment for the first time, you might encounter the error: Unable to connect ElasticSearch server : No alive nodes found in your cluster. This issue often arises due to improper configuration of the Elasticsearch container or incorrect setup within the Magento installation process.
Analyzing Your Current Setup
Let’s break down the problems in your current docker-compose.yml file and the Magento setup command:
Docker Compose File Analysis
version: '3'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- cluster.initial_master_nodes=es01
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
While this setup seems correct, the issue might lie in the way you’re trying to access Elasticsearch from your Magento container.
Magento Setup Command Analysis
php bin/magento setup:install \
--admin-firstname=JJ \
...
--elasticsearch-host=127.0.0.1 \
--elasticsearch-port=9200 \
--elasticsearch-username=admin \
--elasticsearch-password=xxx
Using 127.0.0.1 as the Elasticsearch host in this context refers to the local machine, not the Docker container running Elasticsearch.
The Solution: Connecting Magento and Elasticsearch Correctly
To resolve this issue, you need to connect your Magento containers with the Elasticsearch container using a service name that can be resolved internally by Docker.
Step 1: Update Your docker-compose.yml File
version: '3'
services:
es01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.6.2
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- cluster.initial_master_nodes=es01
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9200:9200
networks:
- elastic
web:
image: webdevops/php-apache-dev:7.4
container_name: web
restart: always
user: application
environment:
# ...
volumes:
- /Users/jonasborneland/Documents/magento:/app:cached
ports:
- "80:80"
- "443:443"
- "32823:22"
links:
- mysql
- es01 # Add this line
mysql:
image: mariadb:10.4
container_name: mysql
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=magento
Step 2: Update Your Magento Setup Command
php bin/magento setup:install \
--admin-firstname=JJ \
...
--elasticsearch-host=es01 \
--elasticsearch-port=9200 \
--elasticsearch-username=admin \
--elasticsearch-password=xxx
By linking the es01 container to your Magento container (Web service) and using es01 as the Elasticsearch host, Docker will automatically resolve this name to the correct internal IP address of the Elasticsearch container.
Additional Best Practices
- Ensure your Elasticsearch password is secure. You can generate a new random password with this tool.
- Keep your Elasticsearch version up-to-date for better performance and security.
- Consider using environment variables to manage sensitive data like passwords.
Conclusion
The key takeaway here is understanding the internal networking of Docker containers. By connecting services correctly within the same network, you can avoid common connection errors like “no alive nodes found”.
If you still encounter issues, consider checking your Elasticsearch logs or using a tool like cURL to test the connection from your Magento container:
curl -X GET 'http://es01:9200'
For more detailed guides on Magento setup and Docker best practices, check out our guide on Magento2 development with Docker.
Happy coding!