Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development question: How to make MQTT work inside the docker container #117

Open
nexysm opened this issue Sep 29, 2023 · 4 comments
Open

Comments

@nexysm
Copy link

nexysm commented Sep 29, 2023

Hi all guys,

I am struggling to get MQTT messages when running the development environment from the VScode. COuld you please tell me how to proceed with this? I tried several things, one among them using socat and trying to relay the traffic there but without success. I am at the end hardware engineer :).

Thank you

@papauorg
Copy link
Contributor

papauorg commented Oct 2, 2023

Hi @nexysm ,

not sure I understand correctly. Do you want to receive/send messages to/from the mosquitto message broker from your host machine? If so you would need to forward the mqtt port from inside the container network to your host. It would be done the same way as it is already done for the craftbeerpi web ui and the mqtt explorer in ./devcontainer/devcontainer.json:

"forwardPorts": [
	"craftbeerpi4-development:8000",
	"mqtt-explorer:4000",
        "mqtt:1883" // <-- Add this to forward 1883 port from the mqtt container
],

After that (and restarting the dev environment) you should be able to access the message broker from your host machine.

@nexysm
Copy link
Author

nexysm commented Oct 2, 2023

@papauorg Thanks for the answer, I almost lost my hope.

I will try this immediately. Meanwhile I tried to understand the whole concept, still doing that, but since I lack the basic understanding of the Docker thing, I would like to ask an additional question. Why is this what you suggested going to work? I ask this because the following reason:
On the host machine, there is already the MQTT broker running on that port. Is that a problem and how could I use that broker instead?

@papauorg
Copy link
Contributor

papauorg commented Oct 2, 2023

I'm sure it will become clearer as you get familiar with the overall concept. It is more difficult here, as there are multiple concepts at work. There is docker, docker-compose and devcontainers here.

When using the dev-environment you are using several docker containers. Those containers are located in a virtual network and are not accessible from your host machine. If you want to access them from your host, which is in another network, you need to forward the ports of your containers to your host (-network).

If you already have a message broker on your host, that is using the same port, then forwarding the default port will not work as it is already taken. At least if only docker and docker-compose would be involved. I'm not entirely sure, but it may be possible that vscode automatically reassigns the host port to a free one if the same port is already taken. You can see that in the "Ports" tab in vscode after you start the dev environment:

Ports tab

@nexysm
Copy link
Author

nexysm commented Oct 4, 2023

@papauorg thanks!

It worked.

It also works when using a broker on the host machine with adding the extra hosts in the docker-compose.dev.yml:

    build:
      context: ../
      dockerfile: .devcontainer/Dockerfile
    command: /bin/sh -c "while sleep 1000; do :; done"
    user: vscode
    depends_on:
      - mqtt
    volumes:
      # Update this to wherever you want VS Code to mount the folder of your project
      - ../:/workspace:cached
    extra_hosts:
      - "host.docker.internal:host-gateway"

In which case the configuration should be like this:

mqtt: true
mqtt_host: host.docker.internal
mqtt_port: 1883
mqtt_username: ""
mqtt_password: ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants