Access host node from within docker container [duplicate] - docker

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I'm setting up Gitlab instance. And setting up email notifications...
I have the email server postfix running on the host..
In the config gitlab.rb I can set the following email server settings
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "gitlab.simplycreate.online"
gitlab_rails['smtp_port'] = 25
As I can see the smtp_address needs a FQDN and not IP. The problem is Gitlab requires the container hostname set same as the main hosts hostname so I cant use it here!?
Is there a way to point it to the host address?

If you run your container with --net=host you can access to your host by localhost.
from version 20.10 you can run your container with --add-host=host.docker.internal:host-gateway and access to your host by host.docker.internal

Related

How Docker container that has an IP of 172.17.0.2 can access external ip like 172.17.xxxx.xxx [duplicate]

This question already has answers here:
Docker Bridge Conflicts with Host Network
(2 answers)
Closed 3 months ago.
I have a Docker container that has a bridge network driver with IP 172.17.0.2.
Inside the container, I'm running a Python application that needs to communicate with an external service that has an IP address like 172.17.xxx.xx
When the Python application inside the container tries to connect to 172.17.xxx.xx, it throws an error, because default IP route in the container
172.17.xxx.xx is part of a private IP range as set out in RFC 1918. It should never be used for something that needs to be accessed from outside the private network.

I want to connect a Docker Superset container to an existing external MySQL database

I am trying to add an existing MySQL database as a source database to a docker container running Apache Superset. The MySQL database that I am trying to add is not running in a docker container. It's an existing MySQL database running on a Windows machine.
I've added mysqlclient==1.4.6 to requirements.txt. The error message seems to indicate that the driver is installed.
I've used mysql://user:password#127.0.0.1:3306/database_name and mysql://user:password#localhost:3306/database_name
The error I get is:
"ERROR: Connection failed, please check your connection settings."
I am using image: apache / 'incubator-superset' v. 0.36.0
Are there any settings or config that needs to be changed to be able to communicate to an external database from within a running docker container?
So I figured it out. For Windows, run ipconfig (maybe ifconfig linux, mac) in terminal/powershell and check what ip address docker ethernet port is using (listed as WSL), let's say ip is: 172.x(x).x(x).x(x). Then configure connection string with ip address on docker ethernet port as follows: 'mysql://user:password#172.x(x).x(x).x(x):3306/database_name'.
Follow-up question if anybody knows: How can I connect my docker container running apache/superset to another server/ip address on my local network running a MySQL server? In other words I want to connect the apache/superset app that is running on my computer in a docker container, to another computer on my local network that is running a MySQL server. The MySQL sever is not in a docker container.
maybe the steps of this blog can help.
If your mysql is in other docker it it is not 127.0.0.1 and in addition if you don't want the requirements to be updated every time that you git pull a new docker, it is better to use the requirements-local.txt
You should be able to do that but your MySQL has to have external IP that you can access from your Supserset Machine. First do a telnet to see if you can listen from port 3306 to that machine and if you can Supserset should work with very similar URI that you have.

How can a container connect to a service on the docker host? [duplicate]

This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(40 answers)
Closed 3 years ago.
There were two related questions but they didn't quite answer the question. But if mods think this is a duplicate, please let me know.
I have a docker-compose.yml file that deploys phpmyadmin. There is a mysql server hosted locally on the host proper (NOT as a container).
I have a config file for phpmyadmin to connect to my database. I can't seem to find a domain name for docker host so I've been taking the subnet that the containers deploy on and using the .1 of the subnet. For example, initially containers deployed to 172.16.0.0/24 and so I declared in phpmyadmin's configuration to connect to 172.16.0.1
This question was born out of the fact that every time I re-deployed, i.e. issued docker-compose down && docker-compose up -d the network address kept changing. My work around is to explicitly declare the default ipam network subnet, which is a fine workaround and actually preferred because I can then pin mysql user logins to the ip address range.
But given that docker knows to resolve the container service "phpmyadmin" to the container's IP address, I figured there MUST be something for the host so I wouldn't have to re-declare the IP address each time.
So, is there a "hostname" that a container can use to talk to the host or am I stuck using IP addresses?
Edit: I'm using docker on Linux and would very much prefer to not run the container in host mode.
The simplest solution is to set network_mode: "host" in your compose file for the container. See https://docs.docker.com/compose/compose-file/#network_mode.
Alternatively you can use host.docker.internal as the url name from within the container, if using Docker for Windows or Docker for Mac.

On Docker for Windows, how do you push to a registry being port forwarded to localhost on the host machine?

I'm just going to put this here, because it was very difficult to find information on this topic and I ended up solving it myself.
Setup
Bastion host in aws with a public ip address
Registry (image registry:2) on a private subnet behind bastion host
Successful ssh port forwarding through bastion, connecting localhost:5000 to registry:5000
curl localhost:5000/v2/_catalog provides a list of installed registries.
So far so good.
docker tag {my image} localhost:5000/{my image}
docker push localhost:5000/{my image}
Result
The push refers to repository [localhost:5000/{my image}]
Get http://localhost:5000/v2/: dial tcp 127.0.0.1:5000: connect: connection refused
How do we connect to a registry port forwarded to localhost?
I have found some obscure posts suggesting that you need to make a custom privileged container and do your ssh bastion port forwarding inside the container. This is essentially working around a problem introduced by the fact that the docker daemon is actually running inside a virtual machine!
https://docs.docker.com/docker-for-windows/networking/
You can find a hint here:
I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE 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.
So given the above, I reasoned that even though this advice is for containers, the docker daemon itself is probably acting on docker cli commands from within a similar context.
Therefore, first you need to add host.docker.internal:5000 as an insecure registry in your docker daemon setup. On Docker for Windows, this can be found in Settings > Daemon > Insecure registries. Unfortunately this doesn't count as localhost, so this has to be done (Docker allows localhost insecure registries by default). Then simply:
docker tag {my image} host.docker.internal:5000/{my image}
docker push host.docker.internal:5000/{my image}
Success!
Hopefully this helps some other very confused developers.

Can I connect to remote DB server from inside docker container?

My app is running against a mssql server 2012 or above,
I tried to set up 2 containers - 1 for my app and one to be a DB server.
But I couldn't use the DB container due to mssql server version windows image is not supported by my app.
So I'm want to connect to a remote DB server that I have which is a different server than the Docker host.
How do I get the container to ping the remote DB server?
From the container-
C:\Installation>ping my0134.company.net
Ping request could not find host my0134.company.net. Please check the name and try again.
** NOTE - I am using Docker on windows
Maybe you could try adding <IP of my0134.company.net> my0134.company.net to the etc/hosts file. This way the url can be resolved to a IP address. You can also just use
docker run --add-host 'my0134.company.net':<IP of my0134.company.net> <image>
to spin up your container.
If IPV4 forwarding is enabled then container can connect to DB Server.There is no issue with that.

Resources