I have ASP.NET core 2.0 webAPI and using Mysql database.So I am configuring the "ConnectionString" in application.json
file. When I am running it locally, I am able to read the connection string and application is working perfect. But it is not when I try to deploy to docker using Docker toolbox. I have written three files :-
- Dockerfile
- DockerCompose
- Proxy.Conf
I am using docker compose file because I have to run on ngnix server. I am able to build to image and as well as to run it. Now I am trying to overwrite the connection string from docker-compose file but Its is not getting overwrite.
DockerFile
FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
COPY $source .
EXPOSE 5000
ENTRYPOINT ["dotnet", "xx.dll"]
docker-compose.yaml
version: '2'
services:
app:
container_name: cp-api
build: .
environment:
- "ConnectionStrings:Test=server=192.168.99.101; port=3306; user=root; password=X123; database=TestDb; TreatTinyAsBoolean=true;"
rev-proxy:
container_name: rev-proxy
image: nginx
ports:
- "9090:8080"
volumes:
- ./proxy.conf:/etc/nginx/conf.d/default.conf`
Proxy.conf
server {
listen 8080;
location / {
proxy_pass http://cp-api:5000;
}
}
appsetting.json:-
{
"ConnectionStrings": {
"EcallDbNextGen": "server =192.168.99.100; port =3306; user =root; password =xxx; database =Testdb; TreatTinyAsBoolean=true;"
},
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
Startup.cs
public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) {
Configuration = configuration;
loggerFactory.AddConsole(Configuration.GetSection("Logging")); //log levels set in your configuration
loggerFactory.AddDebug(); //does all log levels
//removed rest of code
}
public IConfiguration Configuration {
get;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
var connection = Configuration.GetConnectionString("EcallDbNextGen");
services.AddDbContext < ecallnextgendbContext > (options => options.UseMySql(connection));
}