Unable to access host MySQL from container under Linux

I am running a simple Blazer test app in a container on a remote Linux server.
The server is also running MySql (not in a container) port 3306, supporting a couple of websites
I can’t get the app to connect to the MySQL, otherwise the app runs fine.
I tried using the remote host address of in the sql connection string (determined using “docker network inspect bridge” - no success.
I get the “connection timeout” error back from MySQL connection request.
I know not to use localhost or because they are local to the container.
I don’t have any --net settings in my command line, just a port map 8000:80

Alternatively, I have the same app loaded in a local, windows desktop docker container.
With this instance, I can connect to the MySQL on the remote server. I use the public IP address of the server in the connection string.

What else do i have to open up or map?
Do I have to make any changes to the MySQL contiguration, allowing other IP addresses through?
I have not tried --net=host. I loose my port mapping capability and things get complicated with Apache.
I know this is not a new topic. I am a relative newbe and am feeling my way through. Want to resolve this before I go any further. Thanks for any help…

My Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
COPY ["BlazorServerDocker.csproj", "."]
RUN dotnet restore "./BlazorServerDocker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "BlazorServerDocker.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "BlazorServerDocker.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BlazorServerDocker.dll"] 


looks as you are running Docker on Windows.
So please try host.docker.internal as Hostname to connect to from within the container.

Best regards

Tried “host.docker.internal” in my connect string.
Still didn’t connect. Returned the error “Unable to connect to any of the specified MySQL hosts”
I am unable to connect to host MySQL when running in a container under Linux (not Windows)

When I run under docker on my local windows machine I can connect to the remote MySQL

I’ve tried the following command (in this case using the mysql:5.7-image) to create a new container and run the mysql-command with parameters to connect to the mentioned host. With this command you might get some more detailed error-message why the connection fails.
docker run --rm -it mysql:5.7 mysql -u <user> -p -h host.docker.internal -P 3306

If I mistype the hostname then the errormessage was
ERROR 2005 (HY000): Unknown MySQL server host 'host.docker.internalx' (2)

A wrong password (or some other sort of incorrect/invalid credentials as you can restrict the hosts a user can connect from within mysql itself) results in
ERROR 1045 (28000): Access denied for user '<user>'@'' (using password: YES)

Thanks for helping me -
I entered your command line as stated -
Prompted for password -
Responded with: “ERROR 2005 (HY000): Unknown MySQL server host ‘host.docker.internal’ (2)”

Just to be clear: you are using docker-ce on a Linux host. The database runs as native service on that said host. You only mentioned Docker Desktop, as you are able to connect from a container running in Docker Desktop to the database on the Linux host?

With docker-ce, containers can reach services on the host using the ip of the docker0 network interface, if the service is bound to (or and no firewall prevents the connection.

Please share the output of this command, to see if the port is bound to

 sudo netstat -tlpn | grep ":3306 `

Please share the exact error message you get, as it might be possible that the mysql server does not accept connections from the ip range of the container, like @matthiasradde already wrote:

results from netstat:
tcp 0 0* LISTEN 12830/mysqld

I don’t believe I installed docker-ce
Just referenced “docker” when installtin
Is there something I can run to determine what I have?

I am running docker desktop on my local windows computer. My app running locally in a container can connect to the MySQL on the Linux server.

If I am being blocked by some sort of permission or credential error I would get the message indicating so. Correct? Right now I am just getting the Mysql 2005 error.

I tried connecting to MySQL at the host (not from a container) using the cli and I was successful. So, I think my credentials are OK

According the netstat output, mysqld from the host, should be reachable from the container using the ip or the host’s network ip on port 3306. That is unless a firewall blocks it - you would need to sort this out by your own.

Yes. But the precise command depends on your Linux distribution.

Please share the output of:

cat /etc/os-release

[root@vps ~]# cat /etc/os-release
NAME=“CentOS Linux”
VERSION=“7 (Core)”
ID_LIKE=“rhel fedora”
PRETTY_NAME=“CentOS Linux 7 (Core)”


[root@vps ~]#

I did check the firewall and didn’t see anything.
I might drop it for a moment just to see if it is having any effect

Thanks for your help

I am not real a RHEL (and clones) person, so I don’t know the commands required to see which package is installed. I would need to google it, so I leave it to you to google it.

Though, the commands docker version and docker info should show the relevant information. For vanilla Docker experience, it is indeed best to use docker from the official docker repositories. The installation is described here: https://docs.docker.com/engine/install/centos/

Solved the problem -
Was the firewall after all. Had to add the container address of my app (in this case to the firewall allow IP addresses list and to the MySQL access hosts list - since it is the soruce of the request that must be allowed through. I access MySQL using the host address of This address is pretty much fixed on a Linux docker instance. All makes total sense once you see it. Too bad it took me so long to see it :slight_smile:

Which was obvious after you mysql server was listening on

Instead, you might want to add the whole ip range When you re-create the containers based on a new image version, the ip will not be retained.

1 Like