Is it possible to access localhost from Docker? - docker

Hello is it possible to access localhost in Docker and test web app on mobile device? Computer and smartphone are in the same WiFi. I would be pleased for any advice. :)

Yes, it is possible but it's not OS independency so it's good for a dev/local environment only.
Besides, it's quite vary because Docker will try to create a virtual network/adaptor on your docker machine and each different OS has their owned way and limitations.
In Window you can use docker.for.win.localhost to access host network from inside container. For MacOS, there is docker.for.mac.localhost. Docker >= 18.03 will support host.docker.internal but there is no gurantee that it will work on all platforms, so you will have to try it.
There is another safer solution and OS independent, which is using container network in host mode https://docs.docker.com/network/host/. In this mode, your container will use host machine network directly and every port exposed inside container will be exposed outside too. And of course you can access others services running on host machine by just using localhost.
I recommend you to use the docker built-in docker.for.xxx.domain if you are in dev environment and still keep the network isolation, so any security problem inside container won't affect your server. Otherwise, host mode network is a wider compatibility choice.

Related

Freezing my machines IP only for the containers and internal communications

We have a system of numerous microservices, which when setup in local, refers to certain services in my local using their IP(of course resolved via Consul).
It becomes a problem when I connect my laptop from different networks, when the IP of my machine changes. Is there a way in which I can freeze the IP of my machine only for the communications from the containers and the services within my machine?
PS:
Of course, loop-back address won't work, as the loop-back address from container will refer to the container itself and not the host machine.
I can't run with network=host, as there will be many services in different containers running in same port.
I'm using Mac, but looking for generic solution, which would also work in Ubuntu.
Your containers shouldn't ever need to know the ip address of your host. For Docker on MacOS, you can use the hostname host.docker.internal to refer to the host, and this will work regardless of how your host's primary address changes.
While in the past this wasn't possible on Linux, you can now set up the equivalent alias by mapping that hostname to the magic address host-gateway:
docker run --add-host host.docker.internal:host-gateway ...
That will give you consistent behavior for your configurations under both Mac and Linux.

Can docker container call host syscalls?

I'm running a Docker container (alphine) on MacOS 11.6, there's a Typescript app in that container. I need to simulate and record input from Docker on host. Is it possible to setup Docker in a way that would allow my container to control host's input using node.js osx-mouse package, or by writing a Swift wrapper creating CGEvents?
That's almost certainly not possible. In general Docker containers are prohibited from accessing the host display or other host devices. Since Docker Desktop runs a hidden Linux VM, it's especially difficult: the display technologies are totally different and the VM layer makes it look like the container and host are on physically separate systems.
As a general rule, if you need to interact with the host display or any other hardware, it's much easier to run the task outside a container.

Docker container extremely slow when network mode host

I’ve had a working setup with a docker-compose and especially a wildfly image running in network mode = host.
Since the company stopped the internet connection, the startup of the container is extremely slow and end with a timeout.
I found out that when I run the container in network mode = bridge, it is working just normally.
I tried with a docker-hub wildfly empty image to be sure the issue is not on my side and it’s the same problem.
It starts in 5s in bridge, and 33 in host…
I use the command :
docker run --network host jboss/wildfly:18.0.1.Final
to start the container in network host mode.
My docker version is 19.03.15 and it’s running in a VM in bridge mode.
I need the network mode host because we access the containers from outside the VM and they need to communicate with each other.
I can’t use internet anymore on the VM neither the host machine because of the security policy of the company.
So I’m looking for a solution to still use network host without this not-understandable slowness…
I’m not sure if it’s coming from wildfly or the docker itself ?
Thanks by advance,
Loïc.

Connect from container to a service on the host (docker for mac)

I have a somewhat complex situation and am probably out of luck here, but here's hoping. This is part of a large development project, so my options for what changes I can make are somewhat limited.
I have a virtual machine running a k8s cluster. That cluster has an http service that is exposed via ingress, and is available, on my local machine, at develop.com, via an /etc/hosts entry on the host mac.
I have a container, necessarily (see above) separate from the cluster, which needs access to this service. This container uses an env var, SERVICE_HOST to configure its requests.
What is the simplest way to provide a value that can be resolved by the standalone container to my cluster? Ideally, something other than ngrok which is simple, but is complicated by the fact that it's already in use in this setup to allow the cluster to reach the standalone container! I'd much prefer to make this work without premium features...
I'm aware of --net=host concept, but it doesn't work on an OSX host.

Connecting to BACNET Server on Host Machine Using Client Container

I am trying to connect my BACNET client which has been containerized and the BACNET server which is running on the host machine. I am using Docker for Windows on Windows 10 (host machine) with Linux containers.
I have tried the following:
a. Publishing the ports 47808 for the client container with the run command.
b. Running the container with network=host, to access services of localhost.
c. Tried specifying the gateway IP as the server's IP address with run command.
d. Running the container in the same subnet as my server
e. Running the container with the host IP specified and the ports published.
My bacnet server, taken from https://sourceforge.net/projects/bacnet/ always connects to the DockerNAT, 10.0.75.1? Any idea why does this happens? The server application is not a container but an executable file.
Server IP:10.0.75.1 (dockerNAT)
Client container running on host machine.
From a quick google:
For Windows containers this component is not used and containers and
their ports are only accessible via the NATed IP address.
With respect to BACnet, this is going to put you in a world of hurt. You will have to use BACnet BBMD with NAT support in your container to achieve this, and your BACnet Client will have to register as a BACnet Foreign Device. The BACnet Stack at SourceForge does seem to have some NAT support (the code seems to be there but I have never tested it in its original form).
So what you are seeing is 'expected', but your solution is going to require that you become much more familiar with BACnet BBMDs than you ever want to be. Read the BACnet specification carefully. Good luck.

Resources