Spin off another container on host machine from an existing container - docker

I am currently using Docker Desktop for Mac.
My requirement is to spin off a container from another container.
Situation:
Container A has a service running which upon request looks for a swarm manager and spin off another container B. I have started single node swarm manager on my machine. I can not use host network_mode because docker for MAC exposes light weight linux vm as host and not my actual localhost. I have tried this also : https://forums.docker.com/t/access-host-not-vm-from-inside-container/11747/7
Any possible solution?

The idea is that your container can access your host. So, use the Engine API provided by Docker:
POST /containers/create
You will have to post json that contains the details of the new container.
Engine API v1.24
The daemon listens on unix:///var/run/docker.sock but you can Bind Docker to another host/port or a Unix socket.
You can listen on port 2375 on all network interfaces with -H tcp://0.0.0.0:2375, or on a particular network interface using its IP address: -H tcp://192.168.59.103:2375. It is conventional to use port 2375 for un-encrypted, and port 2376 for encrypted communication with the daemon.

Related

docker-compose: port forward localhost:80 inside a docker container to host port 80

I have a situation where I need to let several jobs inside a single docker container orchestrated by docker-compose 1.16.1 communicate with a legacy system.
The legacy system runs in a vagrant box on the same host and binds to three ports (7880, 58608, and 58709). I understand that the default configuration of docker allows accessing the host as 172.17.0.1, but for obscure technical reasons due to network differences I need the host port available on "localhost".
So, how do I make "localhost port 7880" as seen from inside the docker container port forward to the host port 7880?
I have full control of the docker instance and invocation.
Just add network_mode: host section to your docker-compose file and share localhost with containers and host.

How to ping docker container from VM on the same host?

I have a docker container and a virtual machine(VM) on the same host(OpenSUSE). the docker has the IP like 172.18.0.2 and the host IP is something like 3.204.XX.XX and VM IP is also something like 3.204.xx.xx, I am able to ping the docker from the host and even the VM is pingable from the host and vice-versa but I am unable to ping the docker from the Virtual machine present on the same host. Is there a way to access the docker on the host from the VM present on the same host? please help.
it is not possible directly because docker creates its bridge "bridge0" all the traffic is been routed using nat, where as virtualbox also creates its own bridge/interface , because of which its not able to access. But you can access by exposing port.
above mention requirement is possible with consul service discovery and host n/w config modification

How to access a Process running on docker on a host from a remote host

How to access or connect to a process running on docker on host A from a remote host B
consider a Host A with ip 192.168.0.3 which is running a application on docker on port 3999 .
If i want to access that application from remote machine with IP 192.168.0.4 in same subnet.
To be precise i am running Kafka producer on the server and i am trying to receive using Kafka-console-Consumer.
Use --net=host to run your container and it'll use the host's network stack, then you can connect to the application running inside container like it's running on host directly.
Port mapping, use option -p to map the port inside your container to a port of your host. e.g. docker run -d -p <container port>:<host port> <image>, then you can connect to <host>:<host port> to connect your application inside container
Docker's built-in multi-host network. In early releases the network driver is isolated from docker's core, you have to use 3rd party tools like flannel or weave for multi-host connection, but from release 1.9, it has been merged into docker. You can follow it's guide to set it up.
Hope this is helpful :-)
First you need to bind docker container's port to the Host A:
docker run -d -p 3999:3999 kafka-producer
Then you need to access Host A from Host B using IP:Port
192.168.0.3:3999

port linking from docker container to host

I have the following situation. I have a service that listens to 127.0.0.1 on port 1234 (This cannot be changed for security reasons). On the same machine run a docker container. I need to somehow connect to the service on the host from within the container. Because the service only accepts requests from 127.0.0.1, i need somehow to link the port from the container to the host port but in reverse so when i connect from within the container to 127.0.0.1:1234 the service on the host will receive the data. Is this possible?
Thanks.
With the default bridged network, you won't be able to connect from the container to a service on the host listening on 127.0.0.1. But you can use --net=host when running a container to use the host network stack directly in the container. It removes some of the isolation, but then allows you to talk directly to 127.0.0.1 as the container and talk to services running on the host.
Question
How to bind Dockerized service on localhost:port ?
Answer
Use the -p as this: docker run -p 127.0.0.1:1234:1234 <other options> <image> <command>.

Host-only network for Docker container

When running a Docker container, I'd like to set up the container's network so that the container is only able to communicate with the host on the (TCP) ports that the host is listening to. I don't want the container to have access to the internet, or other containers running on the same host, or to the network that the host is connected to. If I was running a VM with something like VMWare, I would choose the "host-only" networking option which creates a private network between the guuest VM and the host with the properties described above.
I've looked into using Docker's --net=none but I don't know what direction to go with to configure the network to achieve my goals. TAP/TUN seems to be the way to go, but I'd appreciate some direction
You could create --internal network and run a container inside it.
Creating a network:
docker network create -d bridge --internal hostonly
Running a container:
docker run --network hostonly ...
Potential partial answer:
If you can use a unix socket to communicate with your application instead of TCP, then you could use
docker run -net=none -v /host-path/socket.sock:/container-path/socket.sock
to provide direct communication between the container and the host socket, without allowing any networking out of the container.

Resources