简体   繁体   中英

MQTT port connection refused when deploying Docker container to Cloud Foundry

I have built a docker image contains a C++ application with MQTT & Mongodb module. When i tried to deploy it to my cloud using Cloud Foundry, this error showed up:

ERR Failed to make TCP connection to port 1883: connection refused

Given that 1883 is MQTT's listen port. I've found on CloudFoundry Docker that the only thing i need to do is include EXPOSE 1883 in my Dockerfile, which i have already done.
Can anyone explain what i have not considered so far? thank you for reading.

Quoting a statement from github commit .

You expose ports using the EXPOSE keyword in the Dockerfile or the --expose flag to docker run . Exposing ports is a way of documenting which ports are used, but does not actually map or open any ports. Exposing ports is optional.

IF you want to access MQTT on port 1883 use -p option in your docker run command.

In your case

docker run -itd -p 1883:1883 mqtt-image-name

Hope this helps.

Update:

Sorry I misunderstood, I gone through official doc.

EXPOSE should have to worked in your case.

The error you got ERR Failed to make TCP connection to port 1883: connection refused means something is wrong with your application or with cloud foundry.

The error might be because your app either does not become available on port 1883 due to a failure, or that it takes longer than the specified healthcheck timeout for it to be up and running, thus failing the healthcheck.

Please check this for more info.

Hope this helps.

i have figured it out! Cloud Foundry opens only port 8080 for container communication. In this site FoundryDocker it stated that if i wanna use other port then i have to specify it via EXPOSE [port]. First i understood it as the port i wanna publish must be pass to EXPOSE argument (in my case is 1883); however, it seemed that the EXPOSE port is the port that application will listen, like ... there is a NAT which redirects my hard-code port 1883 to 8080 for outside communication and vice versa. I hope someone can explain this more clearly, right now all i need to do is EXPOSE 8080 (instead of 1883) doesn't matter which port my application actually listens to.

You are correct here, but that's only part of the story.

I've found on CloudFoundry Docker that the only thing i need to do is include EXPOSE 1883 in my Dockerfile.

When your image is run by Cloud Foundry, it expects your app to listen on a certain port. Normally, CF will tell your app the port on which it should listen via the $PORT env variable. With Docker, you can specify this port by adding EXPOSE to your docker file and indicating the port to use. CF will read this information and use the port you've specified instead of picking one to use.

That should be enough for your application to start, listen on the agreed upon port and for Cloud Foundry to validate the health check to your application. In other words, it should be enough for your application to start successfully. If your application is not passing its health check, then you need to confirm that your application is actually starting and listening on the port. Also, as mentioned in the comments above, make sure it's not listening on localhost or 127.0.0.1 . Those are not accessible outside the app itself, not even to the health check.

The other piece of the puzzle here is mapping external traffic to your application. The port on which your application is listening inside the container is not exposed outside of that container. To route traffic into your app, you need to map a route to your application. Typical routes on CF are HTTP based, so I don't believe they would work for MQTT. You would need to specifically map a TCP route to your application.

Ex: cf map-route my-app example.com --port 5000

This takes an public, external TCP port, which will probably not be 1883 (it depends entirely on what your provider makes available though), and routes traffic to the internal port on which your app is listening. Your clients that wish to connect to the application on CF, need to connect to the mapped public/external port. See diagram here for more details.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM