Changed Dockerfile to accept Arguments from docker.compose.yaml for easy appliance with different modbus devices
Opened this issue · 0 comments
Hi,
I dived a little deeper now into the concept of your software but from a Docker point of view the Dockerfile is very inconvenient because it always tries to load a "modbus2mqtt.csv" file and one is not able to pass different parameters / arguments to it.
For me a usual usecase would be to roll out the software along with other packages (like mosquitto, node-red, etc.) in a docker-compose package, which makes it very easy to get a solution which you can copy to many projects.
I have changed your Dockerfile a little so that it can use arguments from a docker-compose.yaml file.
# changed Dockerfile
FROM python:alpine
WORKDIR /app
COPY modbus2mqtt.py ./
COPY modbus2mqtt modbus2mqtt/
RUN mkdir -p /app/conf/
# upgrade pip to avoid warnings during the docker build
RUN pip install --root-user-action=ignore --upgrade pip
RUN pip install --root-user-action=ignore --no-cache-dir pyserial pymodbus
RUN pip install --root-user-action=ignore --no-cache-dir paho-mqtt==1.6.1
#OLD ENTRYPOINT FROM REPO
#ENTRYPOINT [ "python", "-u", "./modbus2mqtt.py"]
#CMD ["--config", "/app/conf/ebyte_test.csv", "--mqtt-host", "192.168.10.5", "--tcp", "192.168.10.99"]
# ARGS available at build time and defined in docker-compose.yaml
ARG CONF # =<DEFAULT VALUE>
ARG MODBUSHOST # =<DEFAULT VALUE>
ARG MQTTHOST # =<DEFAULT VALUE>
# CMD command list is not using a shell. ARG Variables are not resolved for that reason.
# We are using the docker CMD directive along with a string that we have build up before.
ENV CMDLINE="python -u modbus2mqtt.py --config /app/conf/$CONF --mqtt-host $MQTTHOST --tcp $MODBUSHOST --avoid-fc6 --verbosity 4"
CMD $CMDLINE
In order to use this with a docker-compose.yaml see my example below:
version: '3.8'
services:
modbus2mqtt_ebyte:
container_name: modbus_ebyte
build:
context: ./build_modbus2mqtt # Cloned github repo with own Dockerfile. CHANGED Dockerfile ENTRYPOINT to accept environment variables (see below)
args:
CONF: "ebyte_me31_aaax2240.csv"
MQTTHOST: "192.168.10.5"
MODBUSHOST: "192.168.10.99"
privileged: true
restart: unless-stopped
environment:
- TZ=Europe/Berlin
volumes:
- "/opt/mqtt_control/modbus2mqtt:/app/conf" # local folder with configuration files is mounted to the /app/conf of the image
I have a folder on my system /opt/mqtt_control/modbus2mqtt
where all the configuration files live that I am planning to create for all my modbus devices. In order to spawn a lot of different modbus devices without hassle I just have to copy the above part into a new service and only change the service and container name, the MODBUSHOST
and the CONF
Arguments. That's it.
Maybe some users find this useful. You could also consider to change the Dockerfile in your repo because if you provide default values for the ARGS the functionality with normal Docker would not change but you could change Arguments while running the Docker run Command along with ENV variables and are able to use Docker Compose easily.
regards
Chris