1 year ago
#374817
dinki
Want to convert OS script to simple docker container
I'm in the process of cleaning up many years of hackery and I'm hoping to get some help. I am wanting to convert a shell script I wrote that uses netcat to connect to a Mochad (X10 interface) container and then grabs the output and formats it and passes that along to my MQTT server. This has worked great for years but now I'd like to make it into a simple Docker container.
Here's the simple script I want to convert (mochadtomqtt.sh):
#!/bin/sh
nc 192.168.0.25 1099 | awk ' /HouseUnit:/ && /Func:/ { system("mosquitto_pub -h 192.168.0.25 -q 1 -t /X10/"$6" -m "$8) } '
and this is the Dockerfile I've tried:
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y mosquitto-clients && apt-get install -y mosquitto
CMD ["/x10/mochadtomqtt.sh", ""]
and finally, the portion from my docker-compose:
x102mqtt:
container_name: x102mqtt
image: 'x102mqtt:latest'
restart: unless-stopped
depends_on:
- mqtt
volumes:
- /sharedfolders/appdata/x102mqtt:/x10
network_mode: host
I've built the image and deployed the container but I do not get any sort of output. No errors no nothing. I know the script is being run as I've added an echo "hello world" and that did show up repeatedly. Can someone tell me how I might debug this or if they can spot my obvious errors (outside that I have no experience at doing this).
Many thanks!
EDIT to add more information:
This is what the output from nc looks like:
04/04 15:47:17 Rx RF HouseUnit: B3 Func: On
Answering @The Fool:
I copied most of this from other files. Noted on the command options being blank, I can remove that. Not sure about the restarting as this is intended to be a one shot call. What causes the restarts?
EDIT
I changed the script to ensure that nc, mosquitto_pub and awk were indeed available. They are. I put the full path to these executables in the commandline. I also added an echo to see if the command was running or if it had ended. It ended.
Here's the script modified:
#!/bin/sh
which nc
which mosquitto_pub
which awk
/bin/nc 192.168.0.25 1099 | /usr/bin/awk ' /HouseUnit:/ && /Func:/ { system("/usr/bin/mosquitto_pub -h 192.168.0.25 -q 1 -t /X10/"$6" -m "$8) } '
echo "End"
and resulting output:
/bin/nc
/usr/bin/mosquitto_pub
/usr/bin/awk
End
I'm not sure what that strange square character comes from. I also did a ping previously to the IP I'm trying to connect to and it did ping fine within the container.
Not sure why nc is exiting instead of staying connected. I get no errors at all.
docker
dockerfile
netcat
0 Answers
Your Answer