My dotnet core app running in a docker container on my needs to connect to some external service via their IP one of which is an sql database running separately on a remote server hosted on google cloud. The app runs without issue when not running with docker, however with docker it fails with
An error occurred using the connection to database 'PartnersDb' on server '30.xx.xx.xx,39876'.
fail: Microsoft.EntityFrameworkCore.Update[10000]
An exception occurred in the database while saving changes for context type 'Partners.Api.Infrastructure.Persistence.MoneyTransferDbContext'.
System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled conn
ections were in use and max pool size was reached.
My Docker compose file looks like this
version: "3.5"
networks:
my-network:
name: my_network
services:
partners:
image: partners.api:latest
container_name: partners
build:
context: ./
restart: always
ports:
- "8081:80"
environment:
- ASPNETCORE_ENVIRONMENT=Docker
- ConnectionStrings:DefaultConnection=Server=30.xx.xx.xx,39876;Database=PartnersDb;User Id=don;Password=Passwor123$$
networks:
- my-network
volumes:
- /Users/mine/Desktop/logs:/logs
I have
bin bashed into the running container and I'm able to run pings to
the remote sql database server
I've also being able to telnet to
the remote sql database server on the database port
However, problem arises when i do docker-compose up then I get the error above.
Docker version 19.03.5, build 633a0ea is running on MacOS Mojave 10.14.6
I really do not know what to do at this stage.
There are two separate networks. Inside docker is a separate network. Outside docker, on the host machine, it's a different network. If you are accessing it by localhost or IP address it won't work as you are expecting it.
docker network ls
will show you a similar output like below:
NETWORK ID NAME DRIVER SCOPE
58a4dd9893e9 133_default bridge local
424817227b42 bridge bridge local
739297b8107e host host local
b9c4fb3ed4ba none null local
You need to add the host for Java service locally. Try running like a below command:
For Service:
docker run --add-host remoteservice:<ip address of java service> <your image>
Hopefully, this will fix it.
More here: https://docs.docker.com/engine/reference/run/#managing-etchosts
For PartnersDb Database:
If PartnersDb is a SQL database you'll have to configure SQL Server to listen to specific ports. Through SQL Server Configuration Manager > SQL Server Network configuration > TCP/IP Properties.
More here: https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/configure-a-server-to-listen-on-a-specific-tcp-port?view=sql-server-ver15
There are similar settings for MySQL as well.
After which you'll have to run add-host switch
docker run --add-host PartnersDb:<ip address of PartnersDb database> <your image>
You can update the hosts file as well with these settings. I'd prefer it through the command line instead.
Related
My app from container wants to access Mysql from host machine, but is not able to connect. I googled a lot and tried many solutions but could not figure the error, could you please help me on this.
It is a windows image
IIS Website works
Website pages that use DB Connection does not work
Mysql DB is install in local machine (same pc where docker desktop is installed in)
Connection string in app uses 'host.docker.internal' with port 3306.
Tried docker uninstall, reinstall, image prune, container prune, WSL stop and start, host file commenting for below lines:
192.168.1.8 host.docker.internal
192.168.1.8 gateway.docker.internal
Below is the ipconfig from container
Nslookup and Ping commands:
network LS:
Docker Compose:
version: "3.9"
services:
web:
container_name: dinesh_server_container
image: dinesh_server:1
build: .
ports:
- "8000:80"
- "8001:81"
volumes:
- .\rowowcf:c:\rowowcf
- .\rowowcf_supportfiles:c:\rowowcf_supportfiles
- .\rowocollectionsite:c:\rowocollectionsite
environment:
TZ: Asia/Calcutta
Build image uses: FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8
Host OS: Win 10 Pro 10.0.19043 Build 19043
HyperV is enabled too.
Tried the below too:
extra_hosts:
- "host.docker.internal:host-gateway"
Since it is on windows OS - host network mode is not supported (per my research)
EDIT:
MYSQL Bind Address is 0.0.0.0:
Maybe the problem is not directly related to docker but to mysql.
Please, try making your local mysql database listen in all the network interfaces of your host: by default it only listens in 127.0.0.1 and for this reason perhaps docker is unable to connect to it.
I am not sure about how to do it in Windows but typically you need to provide the value 0.0.0.0 for the bind-address configuration option in mysqld.cnf:
bind-address = 0.0.0.0
Please, consider review as well this article or this related serverfault question.
From a totally different point of view, I found this and this other related open issues in the Docker for Windows repository that in a certain way resembles your problem: especially the first one provides some workaround for the problem, like shutting down the wsl backend and restarting the docker daemon. I doesn't look like a solution to me, but perhaps it could be of help.
I have a .net application running in a docker container via docker compose. I'm using a Windows machine with Docker Desktop running Linux containers.
The application connects to a Cosmos instance. The account key is set to the emulator key by default.
Here is the section from docker-compose.yml
customerapi:
container_name: mycustomerapi
image: acr.io/customer-api:master
ports:
- "20102:80"
environment:
- ASPNETCORE_ENVIRONMENT=Development
- CosmosOptions__Endpoint=${endpoint}
- CosmosOptions__DisableSsl=true
If I override the account key and endpoint, I can get the application to connect using a real instance hosted in Azure, but I can't get it to connect to the emulator running on the host machine.
I've tried setting ${endpoint} to the following values with no luck;
https://host.docker.internal:8081/ Fails after about 5 mins with the error System.Net.Http.HttpRequestException: Connection refused (127.0.0.1:8081).
https://192.168.10.110:8081/ This is my local IP address. It fails much faster (around 10 seconds) with the same error as above.
I've also tried using network_mode: host with both endpoints.
https://host.docker.internal:8081/ Fails with the same error as above.
https://192.168.10.110:8081/ Fails after about 10 seconds with the error System.Net.Http.HttpRequestException: No route to host (192.168.10.110:8081)
I needed to run Cosmos with AllowNetworkAccess
This answer shows how to start the emualtor with the /AllowNetworkAccess argument.
Azure Cosmos DB Emulator on a Local Area Network
Once that was running I was able to use https://host.docker.internal:8081/ and the container sprung to life!
I little confused with Linux docker and cosmos db emulator. I have an emulator installed on my local machine. On my Windows 10 I have a Linux docker container with Web API ASP.NET core application. When I try to get access from container to cosmos db I get an exception -> HttpRequestException: Connection refused.
In C# code I get needed options like AuthKey and Uri to database from environment variables. Looks like I have an issue with network between container and localhost but I can not understand how I can connect these.
Below provided docker-compose.yml and docker-compose.override.yml files.
event.webapi:
container_name: event.webapi
image: '${DOCKER_REGISTRY-}eventwebapi'
environment:
**- AzureCollectionName=Events
- AzureDatabaseName=EventsDatabase**
build:
context: .
dockerfile: src/Services/Event/Event.WebApi/Dockerfile
``` docker-compose.override.yml
event.webapi:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_HTTPS_PORT=44378
**- AzureEndpointUri=https://127.0.0.1:8081
-AzurePrimaryKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==**
ports:
- "53753:80"
- "44378:443"
volumes:
- ${APPDATA}/Microsoft/UserSecrets:/root/.microsoft/usersecrets:ro
- ${APPDATA}/ASP.NET/Https:/root/.aspnet/https:ro
You can't access vm host from docker container inside by setting https://127.0.0.1:8081 directly.
Please refer to this document, and try to set host.docker.internal:8081 to access vm host.
The host has a changing IP address (or none if you have no network
access). From 18.03 onwards our recommendation is to connect to the
special DNS name host.docker.internal, which resolves to the internal
IP address used by the host. This is for development purpose and will
not work in a production environment outside of Docker Desktop for
Windows.
Cosmos DB emulator needs installed SSL certificate,according to this link. For .net runtime, you could access the certificate directly from the Windows Certificate Store.
However, you run the .net code in linux docker image.So my idea is exporting SSL certificate following these steps.
Save it in the specific path on the host and mounting host directories.Please refer to this guide.
I'd like to be able to connect to localstack using the host rather than the service name. I have added the localstack image to my docker-compose file and set network_mode: "host". I can connect to http://localhost:8080 from my other containers. But, I can not connect to: http://localhost:8080 from my host machine. How can I connect to a container using localhost rather than the service name? Not sure if I have misunderstood what network_mode: "host" does.
version: "3"
services:
localstack:
image: localstack/localstack:latest
network_mode: "host"
ports:
- "4567-4584:4567-4584"
- "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
environment:
- AWS_REGION=us-east-1
- SERVICES=sqs
Problem is I'm using CircleCI to run some component tests, but it seems that in CircleCI you can only reference other services on localhost and not via the service name. This means there are some difference between my local environment and test environment configs. I tried running docker-compose in CircleCI but it seems to freak out locally when doing that. So I wanted to see if I can reference localhost between services in docker-compose.
This happens because Docker for Mac runs inside a virtual machine using the xhyve hypervisor not natively on macOS.
When you run the container with net=host you are actually using the network of the VM and not the one from your local machine.
This is a known limitation of Docker for mac given the nature of how it works.
The only way to access a container is by using port mapping, so if you remove the network_mode:"host" from your docker-compose file it should work as you are already mapping ports.
The below code iam using in docker-compose:
integration_test:
image: service:1.0.0
volumes:
- .:/service
links:
- oracle_container
# used volumes_from as workaround to wait until the following containers to start
volumes_from:
- oracle_container
container_name: integration_test
tty: true
environment:
USER: go
command: ["mvn clean install -DskipTests"]
oracle_container:
image: inmage_name:1.0.0
container_name: oracle_container
ports:
- "49161:1521"
I want to make the both containers talk application-->oracle
Both containers are running in same machine and i used the below jdbc string to connect the oracle via application,
jdbc:oracle:thin:#localhost:49161/xe
But iam not able to connect the oracle and its throwing SQLRecoverable Exception.
As per my understanding, this comes under Docker Networking and I have used links to connect two containers. but this issue is with the connection string and more specifically ip of the oracle container.
Can someone help on this issue?
You need to use
jdbc:oracle:thin:#oracle_container:1521/xe
In docker-compose each container can reach other on their service name of the container name. You should not used the host ports instead the container port only