Can docker manage the host's hosts file? - docker

I use Docker under Windows and develop a small web app for example.com.
In my dev environment, I use the ip address to connect to my container group (localhost:8000), which works just fine.
But before I finally deploy to production, I want to confirm the prod-images are also working fine. But the prod image only listens on example.com:80. Can I tell docker to add 127.0.0.1 example.com to my hosts file (c:\Windows\System32\Drivers\etc\hosts) while the container is running?.

Related

How do I connect to other computers via Host Name on Ubuntu?

I have a docker container that is running on Windows currently and it is accessing database resources via the host name (e.g Desktop1, Desktop2, etc...). The docker container is using a bridge network that was created new for the purpose of the system.
What I notice on Windows is that I can ping or connect to those resources simply via the host name and I do not need to remember the IP address of the computer.
I also notice that this can also be done even if I don't have a DNS server running locally (I think?).
However, when I run the container on an Ubuntu host, I keep getting connection errors and timeouts.
I have tried to edit the /etc/hosts and /etc/hostname to include the proper host name of the PC and the fixed wired IP I am using.
I have also tried a test database on the same Ubuntu system but I cannot connect to it via its host name. At best, I am able to connect via something like Desktop1.local but it only solves 1 issue. The other responses I receive from the other systems on the network return only the hostname (e.g. http://Desktop2/api/..., ws://Desktop3/api/..., etc...).
I was wondering if there is a configuration I am missing to have the same functionality as Windows? Do I need to change my code to handle this kind of situations or do I need to do something else like on the OS level?
My command for creating the docker container is along these lines:
docker create -p 172.16.0.1:50000:80/tcp --env MongoDatabaseSettings__ConnectionString="mongodb://desktop1:27017/?uuidRepresentation=standard" --env ConnectionStrings__MySQLConnection="server=desktop2;database=DB;user=user;password=password" --name container1 registry.gitlab.com/group/image:latest
Contents of my /etc/hosts
127.0.0.1 localhost
172.16.0.1 desktop1
If it's me, maybe will try to build the reverse proxy server.
Step. 1
choose your server. (recommend Nginx)
Step. 2
Forward traffic
For example, if your ip of docker service is 192.168.1.2:8080, then you can make 127.0.0.1:80 to forward to it. (or any port you want)
Then you just need to access 127.0.0.1:80, the server will forward the traffic to service of docker.
I dont know is that you actually want to do.
oh, btw, if you still want to access via host name, just edit host file with root user. (make 127.0.0.1:80 a custom domain.
I dont know the reason of that why you can not setting the host file, but set 127.0.0.1 in host file is always working for me.

How to consume static file hosted on Tomcat 9 Server in Docker Image

Version of Tomcat Server: tomcat-9.0.10
Basically I have a docx file hosted in the ROOT folder of my tomcat server. If i try to get the file on my browser it works using "http://localhost:1212/Manufacturing.docx". When I try to use the file in a local build of my webapp it works. But when i try to use "http://localhost:1212/Manufacturing.docx" in my app hosted on the docker container, it fails to get the file (it seems like it cannot establish a connection). Any idea why I can server the file outside of the container but cannot inside the container?
When you try to access localhost from a container, it resolves to the localhost within the container and not the host.
Possible solutions:
If you are using Docker 18.03+ and are on Windows / Mac, accessing host.docker.internal will resolve to the 127.0.0.1 of the host machine.
Run your container using docker run -d --network=host your-image:tag. This way your container will share the networking namespace with your host and localhost inside the container will be the same as localhost on the host.
Also see: From inside of a Docker container, how do I connect to the localhost of the machine? and What is linux equivalent of "host.docker.internal"

DNS issue when using Docker

From a Spring Boot application running on a docker container, I am trying to connect to Rabbit MQ, Storm and other services which are also running on a docker container. It is working fine when using IP address like x.x.x.x but the same is not working when using the DNS name for that IP. In the command prompt I am able to ping and get a successful response for the same DNS name. Requesting for your help and support in this issue.
You said that you can resolve DNS on the command line. If you mean the command line of the host machine (outside of of the docker container), then the issue is probably with the container's own DNS settings.
A container has it's own network settings. Take a look at the /etc/resolv.conf file being used by the Spring Boot container - this will show you that container's DNS settings.

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

WSL2 lan redirection issue when Docker installed

My current setup is my laptop and desktop, they're connected in the same network. I do webdev so i code in my laptop and see the results in my desktop's browser. After installing docker i can no longer access my laptop's web servers that are being runned inside WSL2 directly, ONLY if they're running inside a Docker container.
This issue wasn't present in WSL1, which is:
If you install docker desktop on your windows machine and enable the new WSL2 integration it will mess your windows 'hosts' file (found at %SYSTEMROOT%\System32\drivers\etc\hosts)
Docker automatically adds the following:
# Added by Docker Desktop
192.168.1.77 host.docker.internal
192.168.1.77 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
Note that 192.168.1.77 is my laptop static IP which i set in my router
My guess is that these inputs are redirecting my desktop's requests to docker, completely breaking directly access.
This is not ideal because i have to build a container for coding a simple react app which leads to alot of useless complexity
ps: I have tried the following entries in hosts file, no success:
192.168.1.77 localhost
also:
172.22.3.92 ubuntu.wsl # managed by wsl2-host (service that creates a hostname for WSL2 ip)
192.168.1.77 ubuntu.wsl
The only workaround is to disable docker and clean hosts file, which isn't really what it suposed to be.
Add this to ~/.wslconfig (win) or /etc/wsl.conf (wsl)
[network]
generateHosts = false

Resources