From cd941b8d89e0f3ee91b48afa76c7e2af56c49a3c Mon Sep 17 00:00:00 2001 From: pifou25 Date: Sun, 1 Dec 2024 14:07:00 +0100 Subject: [PATCH] docker build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ajout du fichier ".dockerignore" : comme pour .git les fichiers listés ici ne sont pas copiés par l'instruction COPY . du Dockerfile copier "install.sh" dans /root car on l'utilise pendant la séquence d'initialisation "init.sh" Ajout du "healthcheck": permet de valider la bonne initialisation du container, on peut ainsi le tester dans le workflow Ne pas re-télécharger le source jeedom durant l'initialisation (init.sh) puisqu'on l'a déjà fait durant le build (Dockerfile). Fix la fonction "trap" à l'arrêt du conteneur --- .dockerignore | 3 ++ .github/workflows/docker-test.yml | 14 ++++++--- Dockerfile | 49 ++++++++++++++++++++++-------- install/OS_specific/Docker/init.sh | 43 ++++++++++++++++++++------ install/install.sh | 16 +++++----- 5 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..5f97f08f37 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +/vendor +/docs diff --git a/.github/workflows/docker-test.yml b/.github/workflows/docker-test.yml index ac272915bc..bc5831b87c 100644 --- a/.github/workflows/docker-test.yml +++ b/.github/workflows/docker-test.yml @@ -30,20 +30,26 @@ jobs: run: | docker build -t jeedom:bullseye --build-arg DEBIAN=bullseye --build-arg DATABASE=1 . - name: run jeedom:bullseye and check - # build current image for bullseye and check it + # run current image for bullseye and check it run: | docker run -p 80:80 -d --rm --name jeedom_bullseye jeedom:bullseye - sleep 45 + until [ "`docker inspect -f {{.State.Health.Status}} jeedom_bullseye`" == "healthy" ]; do + echo "waiting container to be healthy..." + sleep 5; + done; docker exec jeedom_bullseye php sick.php - name: Build jeedom:bookworm # build current image for bookworm run: | docker build -t jeedom:bookworm --build-arg DEBIAN=bookworm --build-arg DATABASE=1 . - name: run jeedom:bookworm and check - # build current image for bookworm and check it + # run current image for bookworm and check it run: | docker run -p 81:80 -d --rm --name jeedom_bookworm jeedom:bookworm - sleep 45 + until [ "`docker inspect -f {{.State.Health.Status}} jeedom_bookworm`" == "healthy" ]; do + echo "waiting container to be healthy..." + sleep 5; + done; docker exec jeedom_bookworm php sick.php - name: Clean docker image diff --git a/Dockerfile b/Dockerfile index 37a75bffed..5d8260ab2d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,30 +35,53 @@ VOLUME /var/lib/mysql #speed up build using docker cache RUN apt update -y -RUN apt -o Dpkg::Options::="--force-confdef" -y install software-properties-common \ +RUN apt -o Dpkg::Options::="--force-confdef" -y -q install software-properties-common \ ntp ca-certificates unzip curl sudo cron locate tar telnet wget logrotate dos2unix ntpdate htop \ iotop vim iftop smbclient git python3 python3-pip libexpat1 ssl-cert \ apt-transport-https xvfb cutycapt xauth at mariadb-client espeak net-tools nmap ffmpeg usbutils \ gettext libcurl3-gnutls chromium librsync-dev ssl-cert iputils-ping \ apache2 apache2-utils libexpat1 ssl-cert \ php libapache2-mod-php php-json php-mysql php-curl php-gd php-imap php-xml php-opcache php-soap php-xmlrpc \ - php-common php-dev php-zip php-ssh2 php-mbstring php-ldap php-yaml php-snmp && apt -y remove brltty + php-common php-dev php-zip php-ssh2 php-mbstring php-ldap php-yaml php-snmp \ + npm && \ + # install npm, and node with fixed version + npm install n -g && n 20.18.1 && \ + #clean + apt -y remove brltty -COPY install/install.sh /tmp/ -RUN sh /tmp/install.sh -s 1 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 2 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 3 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 4 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 5 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +COPY --chown=root:root --chmod=550 install/install.sh /root/ +# install step by step : step_1_upgrade ... useless, using the LATEST debian +# RUN sh /root/install.sh -s 1 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_2_mainpackage ... useless, already installed before +# RUN sh /root/install.sh -s 2 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_3_database ... only if $DATABASE +RUN sh /root/install.sh -s 3 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_4_apache +RUN sh /root/install.sh -s 4 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_5_php : install php with extensions +RUN sh /root/install.sh -s 5 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step 6 : copy jeedom source files from local COPY . ${WEBSERVER_HOME} -RUN sh /tmp/install.sh -s 7 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 8 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 9 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 10 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker -RUN sh /tmp/install.sh -s 11 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_7_jeedom_customization_mariadb +RUN sh /root/install.sh -s 7 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_8_jeedom_customization +RUN sh /root/install.sh -s 8 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_9_jeedom_configuration +RUN sh /root/install.sh -s 9 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_10_jeedom_installation : install composer +COPY --from=composer/composer:latest-bin /composer /usr/bin/composer +RUN composer install --no-ansi --no-dev --no-interaction --no-plugins --no-progress --no-scripts --optimize-autoloader +# RUN sh /root/install.sh -s 10 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker +# step_11_jeedom_post +RUN sh /root/install.sh -s 11 -v ${VERSION} -w ${WEBSERVER_HOME} -d ${DATABASE} -i docker + RUN apt-get clean && rm -rf /var/lib/apt/lists/* RUN echo >${WEBSERVER_HOME}/initialisation +# check if apache is running +HEALTHCHECK --interval=1m --timeout=3s --retries=5 --start-period=10s --start-interval=5s \ + CMD curl -f http://localhost/ || exit 1 + WORKDIR ${WEBSERVER_HOME} EXPOSE 80 EXPOSE 443 diff --git a/install/OS_specific/Docker/init.sh b/install/OS_specific/Docker/init.sh index 1bb9c50f77..aa24029749 100644 --- a/install/OS_specific/Docker/init.sh +++ b/install/OS_specific/Docker/init.sh @@ -25,15 +25,22 @@ docker_stop() { echo "${JAUNE}Stopping Jeedom container${NORMAL}" echo "${VERT}Stopping Apache gracefully${NORMAL}" service apache2 stop - if [[ 1 -eq ${ISMARIABDBINSTALLED} ]]; then + # check if mariadb is installed + dpkg --list mariadb-server > /dev/null 2>&1 + status=$? + ISMARIADBSERVER=$(( 1 - ${status} )) + if [[ 1 -eq ${ISMARIADBSERVER} ]]; then echo "${VERT}Stopping Database gracefully${NORMAL}" service mariadb stop + else + echo "${VERT}no mariadb server${NORMAL}" fi + echo "${VERT}Stopping ATD gracefully${NORMAL}" service atd stop + echo "${VERT}Stoping CRON${NORMAL}" + service cron stop echo "${ROUGE}Requesting stop on init.sh${NORMAL}" - echo "${VERT}Killing CRON${NORMAL}" - killall cron exit 0 } @@ -81,10 +88,15 @@ if [ -f ${WEBSERVER_HOME}/core/config/common.config.php ]; then else echo 'Start jeedom installation' JEEDOM_INSTALL=0 - rm -rf /root/install.sh - wget https://raw.githubusercontent.com/jeedom/core/${VERSION}/install/install.sh -O /root/install.sh - chmod +x /root/install.sh - /root/install.sh -s 6 -v ${VERSION} -w ${WEBSERVER_HOME} + + # do not re-install jeedom + if [ ! -f ${WEBSERVER_HOME}/core/config/common.config.sample.php ]; then + echo 'download again Jeedom' + /root/install.sh -s 6 -v ${VERSION} -w ${WEBSERVER_HOME} + # jeedom installation : install composer + /root/install.sh -s 10 -v ${VERSION} -w ${WEBSERVER_HOME} -i docker + fi + if [ $(which mysqld | wc -l) -ne 0 ]; then chown -R mysql:mysql /var/lib/mysql mysql_install_db --user=mysql --basedir=/usr/ --ldata=/var/lib/mysql/ @@ -101,8 +113,14 @@ else sed -i "s/#USERNAME#/jeedom/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#PORT#/3306/g" ${WEBSERVER_HOME}/core/config/common.config.php sed -i "s/#HOST#/localhost/g" ${WEBSERVER_HOME}/core/config/common.config.php - /root/install.sh -s 10 -v ${VERSION} -w ${WEBSERVER_HOME} - /root/install.sh -s 11 -v ${VERSION} -w ${WEBSERVER_HOME} + # init database + php ${WEBSERVER_HOME}/install/install.php mode=force + if [ $? -ne 0 ]; then + echo "${RED}Cannot install Jeedom - Cancelling${NORMAL}" + exit 1 + fi + # jeedom post-install + /root/install.sh -s 11 -v ${VERSION} -w ${WEBSERVER_HOME} -i docker fi fi @@ -148,4 +166,9 @@ service apache2 start echo "Add trap docker_stop" trap "docker_stop $$ ;" 15 -cron -f \ No newline at end of file +# launch cron daemon in background +cron -f & +# hold the cron daemon PID +child=$! +# wait for cron stopping +wait "$child" \ No newline at end of file diff --git a/install/install.sh b/install/install.sh index bb55dafea1..fd14673508 100644 --- a/install/install.sh +++ b/install/install.sh @@ -145,7 +145,7 @@ step_5_php() { step_6_jeedom_download() { echo "---------------------------------------------------------------------" echo "${YELLOW}Starting step 6 - download Jeedom${NORMAL}" - wget https://codeload.github.com/jeedom/core/zip/refs/heads/${VERSION} -O /tmp/jeedom.zip + wget https://codeload.github.com/jeedom/core/zip/refs/heads/${VERSION} -O /tmp/jeedom.zip --no-verbose if [ $? -ne 0 ]; then echo "${YELLOW}Cannot download Jeedom from Github. Use deployment version if exist.${NORMAL}" @@ -184,7 +184,7 @@ step_7_jeedom_customization_mariadb() { echo 'Restart=always' >> /lib/systemd/system/mariadb.service.d/override.conf echo 'RestartSec=10' >> /lib/systemd/system/mariadb.service.d/override.conf - # do not start oany new service during docker build sequence + # do not start any new service during docker build sequence if [ "${INSTALLATION_TYPE}" != "docker" ];then systemctl daemon-reload @@ -331,9 +331,6 @@ step_10_jeedom_installation() { export COMPOSER_ALLOW_SUPERUSER=1 cd ${WEBSERVER_HOME} composer install --no-ansi --no-dev --no-interaction --no-plugins --no-progress --no-scripts --optimize-autoloader - mkdir -p /tmp/jeedom - chmod 777 -R /tmp/jeedom - chown www-data:www-data -R /tmp/jeedom if [ "${INSTALLATION_TYPE}" != "docker" ];then php ${WEBSERVER_HOME}/install/install.php mode=force @@ -349,6 +346,9 @@ step_10_jeedom_installation() { step_11_jeedom_post() { echo "---------------------------------------------------------------------" echo "${YELLOW}Starting step 11 - Jeedom post-install${NORMAL}" + mkdir -p /tmp/jeedom + chmod 777 -R /tmp/jeedom + chown www-data:www-data -R /tmp/jeedom if [ $(crontab -l | grep jeedom | wc -l) -ne 0 ];then (echo crontab -l | grep -v "jeedom") | crontab - @@ -380,8 +380,10 @@ step_11_jeedom_post() { echo 'tmpfs /tmp/jeedom tmpfs defaults,size=256M 0 0' >> /etc/fstab fi fi - chmod +x ${WEBSERVER_HOME}/resources/install_nodejs.sh - ${WEBSERVER_HOME}/resources/install_nodejs.sh + if [ "${INSTALLATION_TYPE}" != "docker" ];then + chmod +x ${WEBSERVER_HOME}/resources/install_nodejs.sh + ${WEBSERVER_HOME}/resources/install_nodejs.sh + fi echo "${GREEN}Step 11 - Jeedom post-install done${NORMAL}" }