2

I try to build a custom image for the EMQ MQTT server. But the script update_config.sh is not executed by during docker copmose up.

Dockerfile:

FROM emqttd-docker-v2.3.5

# change configuration file
ADD update_config.sh /opt/emqttd/etc/update_config.sh
ADD ./certs/MyEMQ1.key /opt/emqttd/etc/certs/MyEMQ1.key
ADD ./certs/MyEMQ1.pem /opt/emqttd/etc/certs/MyEMQ1.pem
ADD ./certs/MyRootCA.pem /opt/emqttd/etc/certs/MyRootCA.pem


WORKDIR /opt/emqttd/etc/

#update the emqtt config file
RUN /bin/ash -c /opt/emqttd/etc/update_config.sh

update_config.sh

#!/bin/ash

cd /opt/emqttd/etc
cp ./emq.conf ./emq.conf.bak
sed -i 's|.*listener.ssl.external.keyfile.*|listener.ssl.external.keyfile = etc/certs/MyEMQ1.key|g' ./emq.conf
sed -i 's|.*listener.ssl.external.certfile.*|listener.ssl.external.certfile = etc/certs/MyEMQ1.pem|g' ./emq.conf
sed -i 's|.*listener.ssl.external.cacertfile.*|listener.ssl.external.cacertfile = etc/certs/MyRootCA.pem|g' ./emq.conf
sed -i 's|.*listener.ssl.external.verify.*|listener.ssl.external.verify = verify_peer|g' ./emq.conf

I use docker-compose to build the image. The update_config.sh script is copied to the image but not executed.

What I tried so far:

  • Used COPY instead of ADD to copy the file
  • Tried the RUN /bin/ash -c /opt/emqttd/etc/update_config.sh in the following flavors:
    • RUN /bin/ash -c /opt/emqttd/etc/update_config.sh
    • RUN /opt/emqttd/etc/update_config.sh
    • RUN ./update_config.sh
  • Tried to add RUN chmod +x /opt/emqttd/etc/update_config.sh before the line RUN /bin/ash -c /opt/emqttd/etc/update_config.sh which results in the error chmod: /opt/emqttd/etc/update_config.sh: Operation not permitted during build

Can anyone help me? Thanks.

Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
Martin
  • 818
  • 9
  • 20
  • 2
    The last line `RUN /bin/ash -c /opt/emqttd/etc/update_config.sh` its `/bin/bash` not `/bin/ash` – Rafaf Tahsin Mar 12 '18 at 08:28
  • Thanks, but currently only ash is installed. If possible I don not want to install bash. – Martin Mar 12 '18 at 08:50
  • 1
    @MartinWeber I can see that all the files are static, why can't you add `emq.conf` of your own with customised config ? – vedarthk Mar 12 '18 at 10:12
  • Thanks. This is really another valid solution. I was just thinking, that it would be easier to update the single settings when I update the version of the base container than to diff the old an new config file. But according to the current feedback, your solution sounds best to me. – Martin Mar 12 '18 at 11:53

2 Answers2

2

Just add ENTRYPOINT ["/bin/bash", "update_config.sh" ] this as your last line. And also update_config.sh file to start your application and make your container in infinite loop.

Example update_config.sh:

    #!/bin/ash

    cd /opt/emqttd/etc
    cp ./emq.conf ./emq.conf.bak
    sed -i 's|.*listener.ssl.external.keyfile.*|listener.ssl.external.keyfile = etc/certs/MyEMQ1.key|g' ./emq.conf
    sed -i 's|.*listener.ssl.external.certfile.*|listener.ssl.external.certfile = etc/certs/MyEMQ1.pem|g' ./emq.conf
    sed -i 's|.*listener.ssl.external.cacertfile.*|listener.ssl.external.cacertfile = etc/certs/MyRootCA.pem|g' ./emq.conf
    sed -i 's|.*listener.ssl.external.verify.*|listener.ssl.external.verify = verify_peer|g' ./emq.conf
    sh start_your_app.sh
    touch 1.txt;tail -f 1.txt #This will make your container in running infinite so that even after all the steps of this script has been executed your container will continue running. until you kill tail -f 1.txt command.

Hope this will help. Thank you!

chintan thakar
  • 1,440
  • 1
  • 15
  • 25
  • 2
    This approach moves script execution from build step to container running step. Could you explain, please, why this approach may help? Thanks! – Bukharov Sergey Mar 12 '18 at 09:37
  • This approach is suitable because first it will optimized layers by writing multiple `RUN` command in Dockerfile. second if you want to pass environment variable at the time of launching container to this script it will be easy. so that your configuration will be dynamic as per environments. – chintan thakar Mar 12 '18 at 14:50
  • As @BukharovSergey said, this doesn't really solve anything if you don't want the script as an entrypoint. – Specimen Sep 24 '22 at 11:50
0

ash - is one of the smallest shells. This command interpreter has 24 built-in commands and 10 different command-line options.

ash hasn't all commands which you need. You should use /bin/bash

Senior Pomidor
  • 1,823
  • 1
  • 14
  • 23