eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.93k stars 2.37k forks source link

Connecting to mosquitto (docker) #1104

Open alexchern opened 5 years ago

alexchern commented 5 years ago

Hi guys,

A newbie question… This is my docker-compose file:

version: '3'
services:  
  mosquitto:
    container_name: eclipse-mosquitto
    image: eclipse-mosquitto
    restart: always   
    ports:
      - '1883:1883'
      - '9001:9001'      
    volumes:
      - mosquittodata:/data
volumes:
  mosquittodata:

When I run it, everything seems to be Ok, but … I cannot connect to the broker from the host computer:

C:\Program Files (x86)\mosquitto>mosquitto_pub -h 127.0.0.1 -t 'test/topic' -m "Hello"
Error: No connection could be made because the target machine actively refused it.

What is wrong? Any ideas?

Thanks,

Alex

akshay-ans commented 5 years ago

By default docker runs on the bridge network 172.17.0.0/16. Find the container's IP address by docker inspect eclipse-mosquitto and connect to that IP Address instead of 127.0.0.1

alexchern commented 5 years ago

Hi,

Thanks for your help.

docker inspect eclipse-mosquitto

brings me the following:

$ docker inspect eclipse-mosquitto
[
    {
        "Id": "41b2312f185c2672d981ebd3fd9976a80f55057b14990ff83bcbb4431c426042",
        "Created": "2019-01-14T06:59:24.981691303Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "/usr/sbin/mosquitto",
            "-c",
            "/mosquitto/config/mosquitto.conf"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 19831,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2019-01-14T06:59:30.042669041Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:339b653189373e53f071c711b25e1c45ee71bac90d8a17d8193c7b98dbef6e6e",
        "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/41b2312f185c2672d981ebd3fd9976a80f55057b14990ff83bcbb4431c426042/resolv.conf",
        "HostnamePath": "/mnt/sda1/var/lib/docker/containers/41b2312f185c2672d981ebd3fd9976a80f55057b14990ff83bcbb4431c426042/hostname",
        "HostsPath": "/mnt/sda1/var/lib/docker/containers/41b2312f185c2672d981ebd3fd9976a80f55057b14990ff83bcbb4431c426042/hosts",
        "LogPath": "/mnt/sda1/var/lib/docker/containers/41b2312f185c2672d981ebd3fd9976a80f55057b14990ff83bcbb4431c426042/41b2312f185c2672d981ebd3fd9976a80f55057b14990ff83bcbb4431c426042-json.log",
        "Name": "/eclipse-mosquitto",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "stservices_mosquittodata:/data:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "stservices_default",
            "PortBindings": {
                "1883/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "1883"
                    }
                ],
                "9001/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "9001"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "always",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "CapAdd": null,
            "CapDrop": null,
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "shareable",
            "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": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/asound",
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/mnt/sda1/var/lib/docker/overlay2/9eaffa5e8eb9610a2be071970a680f4c7e818471630cb308fe6e2f49df70823a-init/diff:/mnt/sda1/var/lib/docker/overlay2/95e632d2986ac96cccdbb10b042fe5333319126656b630f2d48bc15959bc772a/diff:/mnt/sda1/var/lib/docker/overlay2/efe236c69b50634814922c30e7187abb4217309fc2950da1866df8f8e1c044ed/diff:/mnt/sda1/var/lib/docker/overlay2/57999832918b5477101f4e6604101b9eca83e0829c9ba6f652c7482f2023cc57/diff",
                "MergedDir": "/mnt/sda1/var/lib/docker/overlay2/9eaffa5e8eb9610a2be071970a680f4c7e818471630cb308fe6e2f49df70823a/merged",
                "UpperDir": "/mnt/sda1/var/lib/docker/overlay2/9eaffa5e8eb9610a2be071970a680f4c7e818471630cb308fe6e2f49df70823a/diff",
                "WorkDir": "/mnt/sda1/var/lib/docker/overlay2/9eaffa5e8eb9610a2be071970a680f4c7e818471630cb308fe6e2f49df70823a/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "203f94305af4e1e2a59af84dbfc69874d5ce392faecd9d2eb81e0d6dbfae15f8",
                "Source": "/mnt/sda1/var/lib/docker/volumes/203f94305af4e1e2a59af84dbfc69874d5ce392faecd9d2eb81e0d6dbfae15f8/_data",
                "Destination": "/mosquitto/log",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "stservices_mosquittodata",
                "Source": "/mnt/sda1/var/lib/docker/volumes/stservices_mosquittodata/_data",
                "Destination": "/data",
                "Driver": "local",
                "Mode": "rw",
                "RW": true,
                "Propagation": ""
            },
            {
                "Type": "volume",
                "Name": "d9ac26bb606e53c4bc184d611cc3b7b101926e44ca063105abcd83bb580e241a",
                "Source": "/mnt/sda1/var/lib/docker/volumes/d9ac26bb606e53c4bc184d611cc3b7b101926e44ca063105abcd83bb580e241a/_data",
                "Destination": "/mosquitto/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "41b2312f185c",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "1883/tcp": {},
                "9001/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "VERSION=1.5.5",
                "DOWNLOAD_SHA256=fcdb47e340864c545146681af7253399cc292e41775afd76400fda5b0d23d668",
                "GPG_KEYS=A0D6EEA1DCAE49A635A3B2F0779B22DFB3E717B7",
                "LWS_VERSION=2.4.2"
            ],
            "Cmd": [
                "/usr/sbin/mosquitto",
                "-c",
                "/mosquitto/config/mosquitto.conf"
            ],
            "ArgsEscaped": true,
            "Image": "eclipse-mosquitto",
            "Volumes": {
                "/data": {},
                "/mosquitto/data": {},
                "/mosquitto/log": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "/docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "a8faace1ec438c5c82f84373fb7b8f6bfea57bd6fd32c001e89b4ea8492edc8f",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "stservices",
                "com.docker.compose.service": "mosquitto",
                "com.docker.compose.version": "1.20.1",
                "description": "Eclipse Mosquitto MQTT Broker",
                "maintainer": "Roger Light <roger@atchoo.org>"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "f87a9cedecf9a5bc58c69c7824b88ad6c6cc74d364117b2dab854f93ec1dd034",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "1883/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "1883"
                    }
                ],
                "9001/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "9001"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/f87a9cedecf9",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "stservices_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "mosquitto",
                        "41b2312f185c"
                    ],
                    "NetworkID": "53057b11792b8175d5e777a62474e96a748de0df5a440c203526aa9495a4a972",
                    "EndpointID": "ad0613c114ae486da29221ae146fe0ac0d639e84f04245f0ee7449974698873a",
                    "Gateway": "172.26.0.1",
                    "IPAddress": "172.26.0.5",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:1a:00:05",
                    "DriverOpts": null
                }
            }
        }
    }
]

So, I tried to connect to 172.26.0.5, but it didn't help :-(

Thanks,

Alex

jonahfang commented 5 years ago

I have the same problem.You can try "IPAddress": "172.26.0.5" as host inside another container.

phisch92 commented 4 years ago

I had the same issue. My solution was to add two env variables to the docker-compose file for PUID: 1883 and PGID: 1883.

supmik commented 4 years ago

may be it will help: switch off your "MQTT broker" Service in Windows, and 1883 port will be accessible.

mitul-srashtasoft commented 2 years ago

1883 port is free, but then also it's not working.