I cannnot use network_mode: host in docker in windows 10. I try difference images, it cannot too.
I post my docker-compose.yaml below.
docker-compose.yaml
homeassistant2:
container_name: hass2
image: homeassistant/home-assistant:0.112.2
network_mode: host
But when set the port mapping,everthing is fine
ports:
- '8123:8123'
# network_mode: host
My environmet is windows 10
I get some information from command docker inspect
I post some of it about network settings below.
"NetworkSettings": {
"Bridge": "",
"SandboxID": "f084a1e7e4966f0bff678e4120c1bcbc7afdd1c55d5164b1dc4a606470c43bcc",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "656b532cdc48c9e34e616ec0a38ef944c5e52e2dfca506ca99e53b4b16206000",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
If I need provide something else to analyze the problem, please tell me.
When running Docker under Windows or MacOS, you're actually running Docker in a Linux virtual machine. When you set network_mode: host, you're connecting the container to the network environment of the virtual machine. This is seldom going to be useful.
network_mode: host is really only going to make sense when running Docker natively on Linux.
You're better off sticking with port publishing.
I would like to get the docker container name from within a container. I need this information because I am running a script from the Dockerfile as an ENTRYPOINT that needs to know the node number. I am using the --scale with docker-compose to create a cluster of my image.
I am able to get the hostname of the container which is the container ID. But is there a way to get the container name from inside the container?
Docker remote apis should work for you.
Depending on the version of your docker used appropriate version of remote api. Check this for more info.
I made use of container-inspect api to retrieve the container name.
Follow these steps:
Figure out on which port docker daemon is listening. In my case it was 2375 which is the default port.
$ netstat -anp | grep dockerd
tcp 0 0 :::2375 :::* LISTEN 7/dockerd
tcp 0 0 ::ffff:172.18.0.110:2375 ::ffff:172.18.0.1:29445 ESTABLISHED 7/dockerd
unix 2 [ ACC ] STREAM LISTENING 1242188041 7/dockerd /var/run/docker/metrics.sock
unix 2 [ ACC ] STREAM LISTENING 1242187193 7/dockerd /var/run/docker.sock
unix 2 [ ACC ] STREAM LISTENING 1242186375 7/dockerd /var/run/docker/libnetwork/79ba9bb412445c757dd4cc2fc30c1cbc7efde8482bdc4c1cf15c4ce0e4190752.sock
unix 3 [ ] STREAM CONNECTED 1242187211 7/dockerd
unix 3 [ ] STREAM CONNECTED 1242183625 7/dockerd
unix 3 [ ] STREAM CONNECTED 1242184185 7/dockerd
Run a container and retrieve the container name.
$ docker run -it alpine sh
/ # apk update
/ # apk add curl
/ # apk add jq
/ # curl -s 192.168.0.37:2375/containers/$HOSTNAME/json | jq -r ".Name"
/zen_wescoff
NOTE: Here 192.168.0.37 is the ip of my machine. Also apart from container name you can retrieve lot of information using these docker remote apis.
Sample full output of container-inspect api
/ # curl -s 192.168.0.37:2375/containers/$HOSTNAME/json?pretty=true
{
"Id": "3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9",
"Created": "2019-06-20T09:09:51.258807808Z",
"Path": "sh",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3824,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-06-20T09:09:53.341188978Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:4d90542f0623c71f1f9c11be3da23167174ac9d93731cf91912922e916bab02c",
"ResolvConfPath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/hostname",
"HostsPath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/hosts",
"LogPath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9-json.log",
"Name": "/jovial_moser",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [0, 0],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"DiskQuota": 0,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": ["/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware"],
"ReadonlyPaths": ["/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger"]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39-init/diff:/var/lib/docker/overlay2/edb63c15d70f0a29c60f3725ed952dd76439bef4da8915e66ffe6ed1ecf02107/diff",
"MergedDir": "/var/lib/docker/overlay2/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39/merged",
"UpperDir": "/var/lib/docker/overlay2/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39/diff",
"WorkDir": "/var/lib/docker/overlay2/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3409974f45be",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
"Cmd": ["sh"],
"Image": "alpine",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7f41a5043e99bb110e215ac8e13a8a2c37838b25c6a9fa985584ff458c1e52cc",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/7f41a5043e99",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7aa9f269772edff8a9b7f895d51d6dd472a5820ff56a643134465ab8bff1914e",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "55987078c24843c830172de5c352b00c96cb3f177a349437dd7bee9bd1814e86",
"EndpointID": "7aa9f269772edff8a9b7f895d51d6dd472a5820ff56a643134465ab8bff1914e",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
So in-short you need to know ip for your machine and the port on which docker daemon is listening to retrieve any sort of information from within the container.
Hope this helps, let me know.
Mount the docker socket to your container:
myservice:
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
Then use a lib or directly query the docker api:
curl --unix-socket /var/run/docker.sock http://localhost/containers/json
This will return an array of all the running containers which includes the field:
{ Names: ['/proj_myservice_1', '/proj_myservice_2'] }
If you've scaled your containers up and are looking to communicate with the sibling containers inside a container, docker automatically detects this if you make requests to: http://myservice{:port} from inside your container. Docker will auto-discover the container and route you to a random container.
I have this config (using the go-dockerclient https://github.com/fsouza/go-dockerclient) for a container but it only opens the tcp port when running the container:
...
StartConfig: docker.HostConfig{
PortBindings: map[docker.Port][]docker.PortBinding{
"53/tcp": {{HostIP: "0.0.0.0", HostPort: "1053"}},
"53/udp": {{HostIP: "0.0.0.0", HostPort: "1053"}},
},
I tried in every possible way without luck. When I run the container using the cmd it works as expected:
docker run -d \
-p 0.0.0.0:1053:53/udp \
-p 0.0.0.0:1053:53/tcp ...
Any ideas what am I doing wrong here?
Container inspect output:
"NetworkSettings": {
"Bridge": "",
"SandboxID": "4bdea4ccda50aaf8a3117098fdd2073943af43e8d991ddd7a0be425c5599dfbd",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"53/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1053"
}
]
},
"SandboxKey": "/var/run/docker/netns/4bdea4ccda50",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "1796f8bda73d13d354bcd8bafaf6c1f4ff1491582c3985f6b653ccc6b2226e01",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "fb85e54ede4e18ae48e4c889d169d2d4cd8b2087a8c9691e094a431cb6a7eb43",
"EndpointID": "1796f8bda73d13d354bcd8bafaf6c1f4ff1491582c3985f6b653ccc6b2226e01",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03"
}
}
}
I have tried the following example which worked as expected for me, the example as shown below:
The docker image link: https://github.com/sameersbn/docker-bind
Maybe the issue is related with the image that you have?
package main
import (
docker "github.com/fsouza/go-dockerclient"
)
func main() {
endpoint := "unix:///var/run/docker.sock"
client, err := docker.NewClient(endpoint)
if err != nil {
panic(err)
}
container, err := client.CreateContainer(docker.CreateContainerOptions{
Name: "my-container",
Config: &docker.Config{Image: "sameersbn/bind:9.11.3-20190113"},
HostConfig: &docker.HostConfig{
PortBindings: map[docker.Port][]docker.PortBinding{
"53/tcp": {{HostIP: "0.0.0.0", HostPort: "1053"}},
"53/udp": {{HostIP: "0.0.0.0", HostPort: "1053"}},
},
},
},
)
client.StartContainer(container.ID, nil)
}
Docker inspect results:
"Ports": {
"10000/tcp": null,
"53/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1053"
}
],
"53/udp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1053"
}
]
},
Make sure that you have this in your Dockerfile as by default EXPOSE assumes TCP as described in here:
EXPOSE 53/udp 53/tcp
Here is my docker-compose file:
version: '2'
services:
app:
image: mykiwi/ttrss
database:
image: postgres:10-alpine
environment:
- POSTGRES_USER=ttrss
- POSTGRES_PASSWORD=ttrss
volumes:
- database:/var/lib/postgresql/data
volumes:
database: ~
But when I inspect my main container, there is no ip address:
$ docker inspect 40f78d12c178
...
"NetworkSettings": {
"Bridge": "",
"SandboxID": "70479dc6e987e744e376f6e4222e8d26174b724b686da52924a7e539ef4d4f0c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/70479dc6e987",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"none": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "2c6c546b04398c4b4c7b9e52fdb16b89aacfea33b9038b6aa90b4b44fbd99ac7",
"EndpointID": "ac4da8b1b38118732a6e9e99a0ba56b271e6810afd78975c5f329185cf047bf7",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
I run it through rancher, is it related ?
Rancher uses CNI drivers for networking and so the network is "none" as far as Docker is concerned and no IP addresses or port info appears in Docker inspect. You can get it from the metadata service.
I am rather new to Kubernetes and I am trying to start a kubernetes cluster and deploy some pods .
I have a image I want to build in order to perfrom a deployment of it so I am trying to build it's dockerfile but one of the steps in the dockerfile tries to access golobal resource :
curl --silent --location https://rpm.nodesource.com/setup_7.x
I keep getting this error message :
Connecting to rpm.nodesource.com (rpm.nodesource.com)|13.32.153.164|:443... failed: Connection timed out.
Connecting to rpm.nodesource.com (rpm.nodesource.com)|2600:9000:2116:2800:16:cdcc:51c0:93a1|:443... failed: Network is unreachable
I can see that container was able to resolve the dns name ( based on the IP it prints out in the message ) but is unable to access it.
my /etc/resolv.conf is :
search default.svc.cluster.local svc.cluster.local
nameserver 10.82.67.3
nameserver 10.233.0.3
nameserver 8.8.8.8
options ndots:2 timeout:2 attempts:2
my docker version is 17.05.0-ce
my OS is redhat 7.4
this is the Network part of the docker inpect command of the container :
"NetworkSettings": {
"Bridge": "",
"SandboxID": "a4a4c93d0ea19b48b84ef8556678f458405b32238542ede30a78229f82b55a12",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/a4a4c93d0ea1",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "66d74579ee50bc8289dfc58235188e262ebf707f2f0ae0ccb640524e6e9d3f22",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "655da32f8ecab7a4bba100594a1e242c60b27304e9937ae5192857942b661603",
"EndpointID": "66d74579ee50bc8289dfc58235188e262ebf707f2f0ae0ccb640524e6e9d3f22",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
any help would be appriciated