i am trying to connect ElasticSearch with my .NET Core project. I can send request and create data if i run my project manually. But if i try with the docker build it throws 500 error. I cant create data. It doesnt create elasticSearch index too. When i manually create index and send request, it doesnt create data either.
Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app
EXPOSE 80
COPY ./*.csproj /app
RUN dotnet restore ./*.csproj
COPY . .
RUN dotnet publish ./*.csproj -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENV ASPNETCORE_URLS http://*:7000
ENV ASPNETCORE_ENVIRONMENT docker
EXPOSE 7000
ENTRYPOINT ["dotnet","ElasticDocker.dll"]
DockerCompose file
version: "3.8"
services:
elasticdocker:
container_name: orhanelasticdeneme
restart: always
build:
context: .
dockerfile: ./Dockerfile
ports:
- "7000:7000"
depends_on:
- elasticsearch
networks:
- es-network
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.0.0
container_name: elasticsearch
restart: always
environment:
- cluster.name=docker-cluster
- xpack.security.enabled=false
- discovery.type=single-node
ulimits:
memlock:
soft: -1
hard: -1
ports:
- "9200:9200"
networks:
- es-network
kibana:
container_name: kibana
image: docker.elastic.co/kibana/kibana:8.0.0
restart: always
environment:
SERVER_NAME: kibana.example.org
ELASTICSEARCH_URL: http://elasticsearch:9200
ports:
- "5601:5601"
depends_on:
- elasticsearch
networks:
- es-network
networks:
es-network:
driver: bridge
I can connect Kibana too btw.
Startup.cs
services.AddSingleton<IElasticClient>(
p =>
{
var elastic = new ElasticClient(new ConnectionSettings(new Uri(Configuration["elasticsearchserver:Host"]))
.BasicAuthentication(Configuration["elasticsearchserver:Username"], Configuration["elasticsearchserver:Password"]));
var userIndex = elastic.Indices.Exists(Configuration["elasticsearchserver:Orhan"].ToString());
if (!userIndex.Exists)
{
elastic.Indices.Create(Configuration["elasticsearchserver:Orhan"].ToString(), ci => ci.Index(Configuration["elasticsearchserver:Orhan"].ToString()).UserMapping().Settings(s => s.NumberOfShards(3).NumberOfReplicas(1)));
}
return elastic;
});
AppConfig.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"elasticsearchserver": {
"Host": "http://localhost:9200",
"Username": "guest",
"Password": "guest",
"Orhan": "orhan"
},
"AllowedHosts": "*"
}
Controller.cs
private readonly IElasticClient _elasticClient;
public UserController(IElasticClient elasticClient)
{
_elasticClient = elasticClient;
}
[HttpPost]
public async Task<IActionResult> Create(User user)
{
var response = await _elasticClient.CreateAsync(user, x => x.Index("orhan").Id(user.Id));
return Ok(response);
}
[HttpGet]
public async Task<IActionResult> GetAll()
{
var response = await _elasticClient.SearchAsync<User>(x =>
x.Index("orhan")
.From(0)
.Size(2000)
);
return Ok(response.Documents.ToList());
}
[HttpGet("/a")]
public async Task<IActionResult> Get()
{
return Ok("sadgasfdgasd");
}
This is the error in cmd.
CodePudding user response:
If you want to connect elasticsearch with docker, change your host to container name.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"elasticsearchserver": {
"Host": "http://elasticsearch:9200",
"Username": "guest",
"Password": "guest",
"Orhan": "orhan"
},
"AllowedHosts": "*"
}