Docker network interfaces names inside docker container - docker

i am using docker to run application that requires multi network interfaces,
i create new networks using docker network create, and i see that new networks created on my host.
When i do docker run, i use --network to connect the docker container with the chosen network. My problem is that on the host the network interface is called “cvd-ebr” (with is what i want) but inside the docker container is called “eth0”, with cause me problem with the application.
Any way to setup the network interfaces names inside the container ?
The network type is Bridge.
thanks
I try to use docker run with --network name=my-net,iface=my-iface but i get error invalid key iface. I also try to use com.docker.network.bridge.name: my-iface when i create the network.
what i want is the network interface on the host and inside the container will have the same name, without using --network host.

Related

How docker process communication between different containers on default bridge on the same host?

Here is my situation:
First,I run a MySQL container(IP:172.17.0.2) on centOS;
Then I run a Nacos contanier with specified datasource(MySQL above) on the same host, but i didn't use the ip of the MySQL container, instead I used the ip of the bridge Gateway(172.17.0.1)(two containers both link to the default bridge).
What surprised me was that Nacos works well, it can query config data from MySQL container normally.
How did this happen? I have read some documention but didn't get the answer.It really confused me.
On modern Docker installations, try to avoid using the default bridge network. docker network create a network (it doesn't need any special options, but it does need to be created) and then launch your containers on --net that network. If you're using Compose, it creates a ("user bridge") network named default for you.
On your CentOS host, if you run ifconfig, you should see a docker0 interface with the 172.17.0.1 address. When you launch a container with the docker run -p option, that container is accessible via the first port number on all host interfaces, including the docker0 interface.
Meanwhile, inside a container (on the default bridge network), it sees that same IP address as the normal IPv4 gateway address (try docker run --rm busybox route -n). So, when you connect to 172.17.0.1:3306, you're connecting out to the host, and then connecting to the published port of the database container.
This isn't a totally standard way to connect between containers, though it will work. You should prefer using Docker named networks, which will let you connect to another container using the container's name without manually doing any IP-address lookups. If you really can't move off of the default bridge network, then the standard approach is to --link to the other container, but this entire path is considered outdated.

Docker For Mac IP address

I am running an Elasticsearch in a Docker container. To access this from an app running inside another container I can specify elasticsearch host as the machine local IP address on a Mac.
I want to bundle the es host config inside the image, to make it just work without external files, but the issue I have is the image will only work with that IP, so a different mac on the network will not be able to use the image as the es host config will not be correct.
Any ideas how I can use a single IP in the config that will work in all cases, regardless if IP changes?
Linking both containers solved the issue (thanks for suggestion héctor)
By creating a custom network and adding both containers to the network, this allows them to communicate.
I then set Elasticsearch host to container name (of es container), as Docker will automatically resolve the container name to its internal IP.
Created network e.g. docker network create mynet
used --network mynet when running both containers & specified --name
Specified elasticsearch host using container name e.g. es-container

Docker install and use mysql

I would like to install or add a mysql instance in my docker container and would like to access that mysql instance in my application in another docker container. So basically I'm creating one docker container that contains my application and I want to access a mysql inside that container
You can connect using networks. Take a look at this file which builds a mysql container and connects it to a network. And then connects another container to the same network. The required container accesses the mysql container using its ip address and port. I had to use IP address due to following reasons.
The Docker embedded DNS server enables name resolution for containers connected to a given network. This means that any connected container can ping another container on the same network by its container name. From within container2, you can ping container3 by name.
This functionality is not available for the default bridge network.
Both container1 and container2 are connected to the bridge network,
but you cannot ping container1 from container2 using the container
name.
Source:Working with networks
While I was trying to do this process, I wrote about it in my blog. Might help you.

Inter-container connection via localhost

I would like to set up my containers so that they connect to each other via localhost.
My setup is a main application container and two other containers that it needs to connect to (ActiveMQ and Wiremock).
I already run ActiveMQ and Wiremock in containers with the relevant ports exposed, and the main application runs through IntelliJ and connects to these. However, when I am not developing the main applications, I would like to run it in a container for simplicity but it cannot connect to the ports exposed by the others.
Setting --net=host doesn't seem to work, nor does creating a network docker network create <NAME> and assigning it in the docker run with --net=<NAME>.
The application already runs in a container in other environments on the host network.
docker creates a default network in which all containers run, and sets a network name for each of your containers, using the container name.
if you have a contained named mq for your ActiveMQ, then you would use something like tcp://mq:61616 (or whatever protocol / port you have configured) from your other containers, to connect to it.
you shouldn't need to set the --net option unless you need to create a specific network for specific containers to use.

Is there a way to add a hostname to an EXISTING docker container?

I have some containers that communicate via their IP from the network docker.
I can use the option -h or --hostname when running a new container but I want to set the hostname for existing container.
Is it possible?
One way is to create network and add different container in this network.
When adding container in the network, you can use the --alias option of docker network. Like this:
Create a network:
docker network create <my-network-name>
Add containers in the network:
docker network connect --alias <hostname-container-1> <my-network-name> <container-1>
docker network connect --alias <hostname-container-2> <my-network-name> <container-2>
docker network connect --alias <hostname-container-3> <my-network-name> <container-3>
Enjoy.
So each container can see other container by the alias (the alias is used as hostname).
Generally, you would need to stop/restart a container, in order to run it again with -h (--hostname) (unless you used --net=host)
If you cannot stop the container, you can try and (in an attached bash session) edit its /etc/hostname.
The hostname is immutable once the container is created (although technically you can modify /etc/hostname).
As suggested in another answer, you cannot change the hostname by stopping or restarting the container. There are not Docker engine client parameters for the start command that affect hostname. That wouldn't make sense anyway as starting a container simply launches the ENTRYPOINT process in a container filesystem that has already been created (i.e. /etc/hostname has already been written).
It is possible to synchronize the container hostname with the host by using the --uts=host parameter when the container is created. This shares the UTS namespace. I would not recommend --net=host unless you also want to share the host network devices (i.e. bypass the Docker bridge).

Resources