How to connect to localhost inside the docker container? - docker

I have mysql and an application running on the docker. I want the application to connect to mysql localhost inside the docker.

Every container in Docker is a different host with its own IP and hostname, that's why you can't connect to your DB from your app using 127.0.0.1, they are not running on the same host.
You can see the IP assigned to a container with docker inspect <container-id>, but more easily you can refer to a service running on a container by its host, which by default is the name of the container (db in your case). You can also customize the hostname using hostname as you did.
Set db (or hybris_dev depending on how you prefer to configure your container) as the hostname to establish the connection to your DB from your app and it should work.

Related

Connect windows containers to docker host network

Context, I'm currently dockerizing an application in windows containers, the application
will connect to a Sql Server database from outside the container, normally working with linux containers I could use host driver, but since that is not available in windows containers. How could I connect to that database outside my windows container?
So, the answers provided before are all valid. I'd just add that while Host network is not available on Windows, you can still use the same concept - albeit a bit different.
The native network driver on Windows is Network Address Translation. With that driver, the container will get a private IP address and the ports from the container host can be mapped to the ports on the container, by use of the docker run -p 8080:80, for example.
That way, if you want to continue to use the option to call the localhost between the app container and the database container you can. You just need to specify the port: localhost:8080. Note that if the host is not using that port, you can even map it directly, such as: docker run -p 80:80. The caveat here is: The container host cannot be using the port already, and you can't map the same port to another container. So, if you need another instance, you can map to something like: docker run -p 81:80.
I blogged about this here: https://cda.ms/4nB

If docker container ip and external network ip is same, then which one will get respond if will do telnet? any idea

If docker container IP and external network IP is the same, then which one will get respond if it will do telnet?
I know below configuration is the worst configuration, but I want to know the behaviour.
Give you one example
My application is running in localhost which is talking to the database inside the docker container.
Custom IP we provided - (IP : 10.0.0.1, PORT :5432)
Another database running outside the container, let say both container (IP and port) and host (IP and port) are the same.
HOST IP : 10.0.0.1, HOST PORT :5432
Which one will connect by application host/container-database or both the database?
or
If will do the telnet 10.0.0.1 5432? which one will respond and why?
Explain in Diagram
I don't think that's possible, even if you have the same IP (somehow) for the container and the host, you won't be able to map the container port 5432 to the host port 5432, because there's already an application (host dB) running on that port.
Consider a scenario where you are using the host network for the container as well, probably by using the --network host. This way your container IP will be the same as the host IP. The container will be using the 5432 port of the host to run the dB. Now, if you try to start the dB on the host using the same port, you should get an error that port is already being used.

How can I inverse EXPOSE a port to a container?

I am new to Docker and I may not be looking into the right place in the documentation because I couldn't find a way to do what I call "inverse EXPOSE".
So for example, I have one web application that EXPOSE 80. That same application is using a postgresql database. When I am locally developing it works fine because I connect to localhost:5432 but when I containerize the app, it says something like "connection refused". I think the Docker philosophy is to containerize as much as possible and make those containers communicate between each other through a docker network. But I am curious if it actually is possible to say that localhost:5432 in my container actually refers to the port 5432 on the actual machine that hosts my container.
Localhost inside a container is not your docker host, it's a namespaced network inside the container. So if you try to communicate with localhost or 127.0.0.1 inside a container, it's only going to communicate with other apps running inside that container.
Instead, you should use the routeable IP of the host, so that requests can come out of the container and back into the docker host interface to reach applications running outside of a container.
When the app is running in the container you should use the IP:5432 e.g. 192.168.99.100:5432 of the host and not localhost.
When using localhost in the container it refers to localhost (127.0.0.1) of the container and not the one of the host.

karaf broker url inside docker container - windows

So I am working on creating a docker container for a camel project. In the project I am sending data to a messaging queue using apache karaf.
Now when the project is ran normally, i.e without as a container, it works properly. The karaf broker url is - tcp://localhost:61616. This value is coming from a config file inside the camel project.
But when I create the container error is thrown -
Cannot connect to the broker url - tcp://localhost:61616
My guess is 'localhost' inside the docker wouldn't be considered the localhost of my machine (laptop). Inside the container, locahost would be it's own IP address, I believe.
So, what address should I put for the broker url? 127.0.0.1? or my laptop's external IP?
If you camle project is running as docker container and karaf on host then you need to connect to karaf using host ip address. You can change broker url in config like below
tcp://host_ip:61616
As docker container is also like a vm and it has its own network and isolated form host, so localhost for host and container is different.

Can't connect to ASP.Net site in Docker for Windows

I am having difficulty connecting from the host to an ASP.Net website running in a Windows container on Docker. I can connect to a website running in a Linux container without any problem.
I have tried connecting to both localhost and to the IP port assigned to the container but in both cases I just get a timeout error.
I have tried several ASP.Net examples which are already pre-built along with trying to build my own custom image. In every case I get the same timeout error. I have also tried uninstalling and re-installing docker but that didn't change anything.
I am running Windows 10 Pro and Docker Community Edition Version 17.03.1-ce-win12 (12058)
Ultimately I was able to completely reset my container network using a customized older version of the Microsoft Vitualization cleanup scripts. https://github.com/Microsoft/Virtualization-Documentation/tree/live/windows-server-container-tools/CleanupContainerHostNetworking This reset my container network and everything is now working as expected.
SUMMARY:
When the published port/s for a container are defined using the EXPOSE directive in the container's Dockerfile, the -P argument must be used with the docker run command in order to "activate" those exposed port/s.
It is not possible for a Windows container host to access containers that it is running using localhost, 127.0.0.1 or its external host IP address. Access containers running on a given host, A, by using the IP address of A from a second host, B. Alternatively, you can use the IP address of a container directly.
FULL EXPLANATION:
So there are a few nuances with ensuring that the proper firewall rules are created, and your containers are actually accessible on their published port/s.
For instance, I'll assume that your ASP.Net containerized application is defined by a container image, which was defined by a Dockerfile. If so, you probably defined the published port for the image/app using the Dockerfile EXPOSE directive. In this case, when you actually run the container you need to "activate" that published port using the "-P" argument to the docker run command.
For example, if your container image is web_app, and the Dockerfile for that image included the line, EXPOSE 80, then when you go ahead and run that image you need to do something like:
C:\> docker run -P web_app
Once the container is running, it should be available on container port 80. You can then go ahead and view the app via browser. To do that you have two options:
You can access the app from your container host, using the container IP and port
Find the container IP using docker network inspect nat, then looking for the endpoint/IP address that corresponds with your container.
You can also fund the container IP by running docker exec <CONTAINER ID> ipconfig, where <CONTAINER ID> is the ID of your container.
You can get the ID of your container and the exposed port for your container by running docker ps on the container host.
You can access the app from another host machine, using the container host IP and host port
You can find the IP address of your host using ipconfig.
You can identify the host port upon which your app is exposed, by running docker ps from the host. Then, under PORTS you'll see a mapping of the form 0.0.0.0:<HOST PORT>-><CONTAINER PORT>/TCP. In this mapping <HOST PORT>, is the port upon which your app is available on the host.
Once you have the IP address of your container host, and the port upon which your app is available on the host, you can use that information to access your app from a browser on a separate host.
NOTE: Today you cannot access a container in this way from its own host--currently a Windows container host cannot access the containers it is running, despite whether localhost, 127.0.0.1 or the host IP address is used.

Resources