I had some problems with docker recently, due to containers misteriously not
being able to communicate with ports exposed on docker host through other
containers and the default IP address range of docker bridge docker0
clashing
with my job’s internal network one. I also wanted to replicate docker logs to
systemd’s journald daemon, so that they could be locally queried and persisted
with my other system logs. To achieve that, I came up with the following
configuration for the docker daemon, that must be placed at
/etc/docker/daemon.json
:
{
"bip" : "1.1.1.1/24",
"fixed-cidr": "1.1.1.1/25",
"mtu": 1500,
"log-driver": "journald" ,
"log-opts": {
"tag":"id={{.ID}},name={{.Name}},image={{.ImageName}}"
}
}
With that configuration, the docker host ip will be 1.1.1.1
. All containers
you create will have their IPs from .2
and go until .254
, and will also
expose their ports to that docker host ip.
With a little help from jq
, you can discover which IP and ports are exposed
on a container. Here is the command you should use:
docker inspect <container-id> | jq '.[] | {(.Name) : [(.Config.ExposedPorts | keys),(.NetworkSettings.Networks | to_entries | map({(.key): .value.IPAddress}))]}'