I have this Dockerfile:
# Use the official python image
FROM python:3.12
# set the working directory
WORKDIR /app
# copy the current directory contents into the container at /app
COPY . /app
### install libgl
RUN apt update && apt install -y libgl1-mesa-glx && rm -rf /var/lib/apt/lists/*
# install the required packages
RUN pip install -r requirements.txt
# run the server script
CMD ["python", "server.py"]
and i have this docker-compose.yml file:
version: '3'
services:
mqtt2:
image: eclipse-mosquitto:latest
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf:ro
server:
build: .
depends_on:
- mqtt2
environment:
- MQTT_BROKER=mqtt2
links:
- mqtt2
and this is my server.py file:
import paho.mqtt.client as mqtt
import time
import os
MQTT_BROKER = os.getenv('MQTT_BROKER', 'mqtt2')
MQTT_PORT = 1883
#MQTT_PORT = 9001
CAMERA_TOPIC = "camera/image"
CLIENT_TOPIC = "server/message"
def on_connect(client, userdata, flags, rc, properties):
print("connected with result code " + str(rc))
client.subscribe(CAMERA_TOPIC)
def on_message(client, userdata, msg):
print(f"Recieved message on {msg.topic}")
processed_message = "new message for testing"
client.publish(CLIENT_TOPIC, processed_message)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_BROKER, MQTT_PORT, 120)
client.loop_start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
client.loop_stop()
client.disconnect()
when i build the docker file locally, it works, but when i want to deploy it to a server, it gives error. the error is related to this line of python code:
client.connect(MQTT_BROKER, MQTT_PORT, 120)
It says: “””Name or service not known.””” what should i do? what is wrong with my codes?
As i said, i tried to build this dockerfile locally with this command:
docker-compose up --build
it works, but when i want to deploy it to a server, i get this error:
"""Name or service not known."""