# Dockerfile for Tani PLC Engine
# Date: 2021-06-10

# 1. Build the image
#   - copy the PLC Engine installer in the same dirctory as this Dockerfile
#   - edit Dockerfile, change the line "ENV plcengine_version ..." to point to the correct verison of the Installer file (OPC Server or PLC Engine).
#   - run "docker build -t plcengine ."
#     params:
#     -t plcengine --> gives a name to the new image
#   NOTE: the last step requires internet access, as it downloads a docker base image.
# 2. Run the generated image
#   - run "docker run --name plcengine1 -p 2468:2468/tcp -p 2468:2468/udp -p 4840:4840/tcp -p 4444:4444/tcp -v plcengine-data:/etc/Tani -d --rm plcengine"
#   or
#   - run "docker run --name plcengine1 -v plcengine-data:/etc/Tani -d --rm plcengine"  when iptables reports an error.
#     params:
#     --name plcengine1           --> the name of the new instance
#     -p ...                      --> exposes the specified ports: configuration (2468), OPC UA (4840), OPCpipe (4444)
#     -v plcengine-data:/etc/Tani --> creates or resuses a volume named plcengine-data do store the PLC Engine configuration and license data
#     -d                          --> run in detached mode
#     --rm                        --> remove the container after exiting
#     plcengine                   --> the image to use
# 3. Stop the running container
#   - run "docker stop plcengine1"
#     params:
#     plcengine1                  --> the instance to stop

# Base image to use: opensuse
#FROM opensuse/leap

# Base image to use: ubuntu
FROM ubuntu

# PLC Engine installer to use
# TODO: edit this line for correct version of installer
ENV plcengine_version PlcEngineInstaller-2.3-i64

# install Tani PLC Engine, delete installer afterwards
ADD ${plcengine_version} /
RUN mkdir -p /opt \
 && chmod +x /${plcengine_version} \
 && TERM=linux MOJOSETUP_UI=stdio script -q -e -c "/${plcengine_version} -destination=/opt/plcengine --i-agree-to-all-licenses --noprompt --noreadme --nooptions" /dev/null \
 && rm /${plcengine_version}

# public ports
EXPOSE 2468/tcp 2468/udp 4840/tcp 4444/tcp
# configuration directory
VOLUME /etc/Tani

# add start script
RUN echo "#!/bin/sh" > /opt/plcengine/docker_start.sh \
 && echo "/etc/init.d/configserver start" >> /opt/plcengine/docker_start.sh \
 && echo "tail -f /opt/plcengine/docker_start.sh" >> /opt/plcengine/docker_start.sh \
 && chmod +x /opt/plcengine/docker_start.sh

# start command
CMD /opt/plcengine/docker_start.sh
