Docker: how to connect from local container to local swarm port - docker

We have swarm running on 6 hosts and about 15 containers. There are one accesspoint open as port 3010.
On every host, which are nodes of swarm, there is local isolated network with 3 docker containers. On each host, one of this containers want to connect to that publish port 3010.
I like to use port on that host, which is currently running that container. I do not know, if this is wise?
How to solve the name of host to use on docker container to connect to the local swarm port. Localhost and 127.0.0.1 are not available. I can connect container on overlay network on swarm, but it is not possible, when starting container, because of local isolated network.

How to solve the name of host to use on docker container to connect to the local swarm port.
It the name of your service name.
E.g. when docker service create --name blue --network dev markuman/color
then you can attache to this service container with figuring out the exactly name id
docker ps| grep blueeb46c52d0568 markuman/color:latest
"/bin/sh -c '/bin/..." 51 seconds ago Up 49 seconds 80/tcp blue.1.o5w76smq3kh5jlomltf6yohj3
and simply do
docker exec -ti blue.1.o5w76smq3kh5jlomltf6yohj3 bash
That's it. From there you can ping or ssh into other serivces which are assigned to the same network.
E.g. when docker service create --name apache ... is running in the same network, just do a ping apache. That's sufficient

Related

Cannot connect interface to Docker container

I am trying to connect and run a device (LiDAR) through Docker container since it needs Ubuntu 16 while my computer is Ubunutu 20.
I got the device to ping inside the docker container, but it is not recognised when I try to use it.
What I did:
Made Dockerfile with requirements (Added EXPOSE to expose all ports)
Built docker image using:
docker build -t testLidar
I then made a container using
docker run -d -P --name test_Lidar (imagename)
Then
docker exec -t test_Lidar (device_ip) works
I am able to ping my LiDAR IP inside the container, but when I do ip a I cannot see the interfaces connected to my machine.
Been stuck on this for 3 days, any suggestions?
Note: I have done the exact same steps but on an Ubuntu 16 machine. The only change was the docker run command had --net host instead of -P tag and my device worked perfectly. I feel like this is the root of my problem.
Use --net host flag with docker run to attach the container to your host's networking stack and make it available in for other hosts in your network.
When you use --net host, you actually attach the container to your host's networking stack. By default, containers are attached to the default network of type bridge and can communicate with each other. You can then reach them only from your host using its ip addresses typically in subnet 172.17.0.0/16.
Using -P actually binds exposed ports from a container with randomly selected free ports on your host. It should be used for exposing network services (eg. web server with port 80), but not for ICMP ping.

Rancher container taking over host IP

I have 2 IP addresses in my rancher host (centos): 1.1.1.1 and 2.2.2.2
1.1.1.1 is the IP address I want to use to access the rancher UI and SSH into the host.
I want to use 2.2.2.2 for accessing containers for an application. I have 2 containers, one nginx and one ssh. I configured the containers to use hostport 80 mapped to 2.2.2.2:80 and 22 to hostport 2.2.2.2:22.
I have also changed the default run command for the rancher container to listen on port 80 and 443 of IP 1.1.1.1
If I go to my browser and access 1.1.1.1 I see rancher as expected, and if I access 2.2.2.2 I see my container app as expected.
However, if I try accessing 1.1.1.1:22 I end up connecting to the container ssh, which should be only listening to 2.2.2.2:22.
Am I missing something here? Is this a configuration issue on the host or the container? Can the container get access to something that it shouldn't even be aware of?
UPDATE
Let me try to clarify the setup:
Rancher is running in a host with 2 IP addresses. When I run rancher, I execute the following command, so it becomes attached to the first IP address:
docker run -d --volumes-from rancher-data --restart=unless-stopped -p 1.1.1.1:80:80 -p 1.1.1.1:443:443 rancher/rancher
docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.1.7 --server https://rancher1.my.tld --token [token] --ca-checksum [checksum] --etcd --controlplane --worker
I have 4 containers configured in the rancher UI, which I want pointing to 2.2.2.2:22 and 2.2.2.2:80, 2.2.2.2:2222 and 2.2.2.2:8080
These are 2 environments for an application. 22 and 80 are nginx and ssh containers for the LIVE environment (sharing a data volume between them) and the same thing for 2222 and 8080, with these being for a the QA environment. I use the ssh container to upload contents to the nginx container through the shared data volume.
I don't see a problem with this configuration, except the fact that when I configure the ssh machine to use port 22, when I try connecting to the host ssh, I get connected to the container ssh.
UPDATE 2
Here is a screenshot from the port mapping settings in the container: https://snag.gy/idTjoV.jpg
Container port 22 mapped to IP 2.2.2.2:222
If I set that to 2.2.2.2:22, SSH to host stops working, and ssh connections are established to the container instead.

How to access container hostname from host machine

I am running docker containers from MaxOS(docker for mac). I have setup a bridge network for these containers. Each of them has its own hostname and they can connect to each other through the hostname. Now I want to connect to these containers from my host os which is Mac by their hostname. How can I do that? I only know that I can bind port number to my host os but I don't know how to access these containers by their hostname.
I know that I can attach the container by docker command like below:
docker exec -it $INSTANCE_NAME /bin/bash
But I want to access it through network.

How to connect a Docker container to a local network

I'm running Docker on a Raspberry Pi 3 using Raspbian (Jessie). I want to access my containers from other PCs on the same network. Can someone explain how can I make containers to show under my router list as an independent machine?
Port forwarding is useless because we are using few ports and if we need to add some new function to it, we must commit the container, delete the container, create a new container under the image created and add the new ports to forward.
Maybe you can try docker ps -a to check the container's states and available ports.
This is the result on my Linux (IP address is 135.251.247.21):
sdn#sdn-KVM:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be8c8289fe20 135.249.45.113:9005/onos:1.7.004 "./bin/onos-service" 3 weeks ago Up 7 hours 0.0.0.0:6633->6633/tcp, 6653/tcp, 0.0.0.0:8101->8101/tcp, 9876/tcp, 0.0.0.0:9191->8181/tcp onos-docker
I can access this container from a remote machine via SSH:
ssh -p 8101 karaf#135.251.247.21.
If you cannot access your container from a remote machine, you can try to access it on your local machine by running docker exec -it xxx bash, where xxx is the container name.

Set hostname of service container to host's hostname

I run a docker swarm with docker swarm mode. Let's say I have 4 nodes, 1 manager, 3 worker. The hostnames are:
manager0
worker0
worker1
worker2
I start the service in global mode, so every node runs the service once.
Let's say the command looks like this:
docker service create --name myservice --mode global --network mynetwork ubuntu wait 3600
mynetwork is an overlay network.
Now I am trying to access the hostname of the docker host in the containers, so I can pass the hostname to an application in the container.
I tried to pass the hostname with the environment variables (--env hostname=$(hostname)), but actually ${hostname} is only executed on the manager and the hostname is set to manager0 for all nodes.
Is there a way to access the hostname or pass the hostname to the containers?
You can use latest naming templates to create service with hostname.
Here is the feature request, that has been implemented in docker version 17.10
https://github.com/moby/moby/issues/30966

Resources