karaf broker url inside docker container - windows - docker

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.

Related

How to Make Docker Use Specific IP Address for Browser Access from the Host

I'm using docker for building both UI and some backend microservices, and using Spring Zuul as the Proxy to pass Restful API calls from UI to the downstream microservices. My UI project needs to specify an IP address in the JS file before the build, and the Zuul project also needs to specify the IP addresses for the downstream microservices. So that after starting the containers, I can access my application using my docker machine IP http://192.168.10.1/myapp and the restful API calls in the browser network tab will be http://192.168.10.1/mymicroservices/getProduct, etc.
I can set all the IPs to my docker machine IP and build them without issues. However for my colleagues located in other countries, their docker machine IP will be different. How can I make docker use a specific IP, for example, 192.168.10.50, which I can set in the UI project and Zuul Proxy project, so that the docker IP will be the same for everyone, regardless of what their actual docker machine IP is?
What I've tried:
I've tried port forwarding in VirtualBox. It works for the UI, however the restful API calls failed.
I also tried the solution mentioned in this post:
Assign static IP to Docker container
However I can't access the services from the browser using the container IP address.
Do you have any better ideas? Thank you!
first of to clarify couple things,
If you are doin docker run ..... then you just starting container in your docker which is installed on the host machine. And there now way docker can change ip of your host machine. Thus if your other services are running somewhere else they will have to know something about docker host machine, ip or dns name.
so basically docker does runs on 127.0.0.1 if you are trying it on docker host machine, or on host machine IP if from outside of it. So docker don't need IP of host to start.
The other thing is if you are doing docker-composer up/start. Which means all services are in that docker compose file. In this case docker composer creates docker network for all containers in it. in this case you definitely can use fixed IPs for containers, though most often you don't need to because docker takes care of name resolution in that network.
if you are doing k8s way - then it is third way (production way), and it os another story.
if that is neither of above then please provide more info on how are you doing stuff.
EDIT - to:
if you are using docker composer and need to expose any of your containers to host machine you can do it through port mapping:
web:
image: some image here
ports:
- 8181:8080
left is the host machine port, right is container port
and then in browser on the host you can do request to localhost:8181
here is doc
https://docs.docker.com/compose/compose-file/#ports

How to connect to localhost inside the docker container?

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.

How to access service in localhost exposed on a given port from a nginx container

I currently have a nginx container which points to other dockers, that run a java app, through ip and port, working as expected. For simplicity porpouse, I'm running these java apps outside a docker container, thus having them exposed on localhost:xxxx
When trying to communicate to localhost:xxxx from nginx container, it obviously pointer to the container itself and not my java apps.
Is there a workaround to this problem?
Localhost is scoped to the container itself. Therefore to be able to connect to it, you would have to be inside the container.
To fix this, you need to get your application to talking/listen to 0.0.0.0 instead.

Docker container cannot connect to Host Machine

i tried to deploy some containers to a new CentOS7 VM (newest docker version) with docker-compose:
ASP.NET Application
Mongo
Nginx Reverse Proxy
Lets Encrypt Sidecar for Nginx
The connection between the containers works fine. But my ASP.NET Application has to make a request to itself using the public domain name. It fails and when I make a curl request inside the container it fails with No route to Host. Note: I am not using localhost or so.
I found another post: Docker container cannot connect to host machine: No route to host
It seemed to be firewall problem in this case. So I also tried to add firewall rules, but it did not help.
Assuming the container X, where your ASP net container is running, does need to contact your app using the domain. Assuming, you either use domain.dev in development or domain.tld, it you can pass "host" arguments to the container on start ( or in docker compose ways ) to bind that domain to either localhost, so --host domain.tld:127.0.0.1 or the actual IP of the container ( the private one ).
The latter one needs you to actually use docker-networks and give every container a static IP in the docker network, so you know the ASP net container IP prior starting it. You can of course do some docker-socket mount + docker inspect magic in the container, but that is far to overblown for development purposes.
References:
- docker host entries: https://docs.docker.com/engine/reference/run/#managing-etchosts
- docker networks https://docs.docker.com/engine/reference/run/#network-settings
And in docker compose:
- docker host entries: https://docs.docker.com/compose/compose-file/#extra_hosts
- networks: https://docs.docker.com/compose/compose-file/#networks
Hint: i would strongly encourage you to use docker-compose.
Alternativ ways:
you can also use the service-name as the domain in your container, since this is automatically resolving to the container IP, so assuming your ASP net container is named "app" in the docker-compose file ( the service ), you can access it using app in the container.

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