We need to setup elasticsearch on Magento 2 when a new container starts. We have post-install scripts that could handle that and not have someone go into the admin and manually do it.


2 Answers 2


Found a way to do it. We added these commands to our post-install script:

magento config:set catalog/search/engine 'elasticsearch5'
magento config:set catalog/search/elasticsearch5_server_hostname 'elasticsearch-hostname.env'

You can also configure other things like ports:

magento config:set catalog/search/elasticsearch5_server_port '9200'
magento config:set catalog/search/elasticsearch5_index_prefix 'magento2'
magento config:set catalog/search/elasticsearch5_enable_auth '0'
magento config:set catalog/search/elasticsearch5_server_timeout '15'

These are the engine options:

mysql - MySQL
elasticsearch - Elasticsearch
elasticsearch5 - Elasticsearch5.0+

If you decide to use variables on the server_hostname, you must use double quotes:

magento config:set catalog/search/elasticsearch5_server_hostname "elasticsearch-$MY_VARIABLE.env"

If you use elasticsearch 7 use these commands (change values according to your needs):

magento config:set catalog/search/engine 'elasticsearch7'
magento config:set catalog/search/elasticsearch7_server_hostname 'localhost'
magento config:set catalog/search/elasticsearch7_server_port '9200'
magento config:set catalog/search/elasticsearch7_index_prefix 'magento2'
magento config:set catalog/search/elasticsearch7_enable_auth '0'
magento config:set catalog/search/elasticsearch7_server_timeout '15'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.