In many cases, we want to customize our Elasticsearch image, for example, we need to install some additional plugins, or if we want an Elasticsearch with a synonym file and custom configuration? Or we need some corresponding configuration, etc. We want to repeat the above steps in each docker deployment, so how should we do it? It can be a little difficult to find a piece with all the details. With this in mind, I've decided to post a more detailed custom configuration for our docker containers.
Require
- Docker is already installed in our system
- log in to docker
- Visual Studio Code or some editor of your choice to create and edit the file
Create Dockerfile
- Create a new file called Dockerfile, without extension, where we will set up all the steps needed to customize the Elasticsearch installation.
- Decide which version we want to use. For example, we will use 8.7.0.
- The first line will be:
From docker.elastic.co/elasticsearch/elasticsearch:8.7.0 as elasticsearch
- Add the synonyms file - synonyms_path should be the path to the folder in the host that contains the synonyms file in the same folder where we have the Dockerfile:
ADD ./{synonyms_path}/ /usr/share/elasticsearch/config/
VOLUME ["/usr/share/elasticsearch/config/"]
- Add configuration - config_path should be the path of the folder in the host that contains the configuration file in the same folder where we have the Dockerfile:
ADD ./{config_path}/ /usr/share/elasticsearch/config/
VOLUME ["/usr/share/elasticsearch/config/"]
- Add plugins - plugins_path should be the path to the folder in the host that contains the plugins in the same folder where we have the Dockerfile:
ADD ./{plugins_path}/ /usr/share/elasticsearch/plugins/
VOLUME ["/usr/share/elasticsearch/plugins/"]
NOTE : All plugins should be built for the same version of Elasticsearch we are using: 8.7.0
- Expose the Elasticsearch port:
EXPOSE 9200
You should have a Dockerfile like this:
Dockerfile
From docker.elastic.co/elasticsearch/elasticsearch:8.7.0 as elasticsearch
ADD ./{synonyms_path}/ /usr/share/elasticsearch/config/
VOLUME ["/usr/share/elasticsearch/config/"]
ADD ./{config_path}/ /usr/share/elasticsearch/config/
VOLUME ["/usr/share/elasticsearch/config/"]
ADD ./{plugins_path}/ /usr/share/elasticsearch/plugins/
VOLUME ["/usr/share/elasticsearch/plugins/"]
EXPOSE 9200
We have two options: create a docker compose file or run the following command to build the image:
docker build . -f Dockerfile -t elasticsearch
Start the container with the following command:
docker run -p 9200:9200 elasticsearch
If we prefer to use docker compose, we need to create a new file called docker-compose.yaml in the same folder as the Dockerfile with the following text:
docker-compse.yaml
version: "3.4"
networks:
internal-network:
driver: bridge
services:
elasticsearch:
image: elasticsearch
container_name: elasticsearch
networks:
- internal-network
build:
context: .
dockerfile: Dockerfile
target: elasticsearch
ports:
- 9200:9200
environment:
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
To start the container, we run the following command in the folder containing the docker-compose file:
docker compose up
We now have Elasticsearch running in a container on our computer!
We can even use the docker save command to save this image, and use docker load to install it on other computers.