如何将 Linux iso 转换为 Docker 镜像/容器

如何将 Linux iso 转换为 Docker 镜像/容器

我正在尝试将 Linux 发行版 (RasPBX) 转换为 Docker 容器。到目前为止,我能够部署容器,但其运行没有预期的结果。以下是 Docker 文件和 Dockerignore 文件:

FROM debian:buster-slim

# Copy the whole system except what is specified in .dockerignore
COPY / /

RUN ./usr/sbin/asterisk

RUN chown -R asterisk:asterisk /var/run/asterisk && \
    chown -R asterisk:asterisk /var/lib/asterisk && \
    chown -R asterisk:asterisk /var/log/asterisk && \
    chown -R asterisk:asterisk /var/spool/asterisk && \
    chown -R asterisk:asterisk /usr/lib/asterisk
    
RUN chown -R mysql:mysql /var/lib/mysql/*

RUN mkdir /var/run/mysqld && \
    touch /var/run/mysqld/mysqld.sock && \
    touch /var/run/mysqld/mysqld.pid && \
    chown mysql:mysql /var/run/mysqld/ && \
    chmod -R 755 /var/run/mysqld/ && \
    chown -R mysql:mysql /var/run/mysqld/mysqld.sock && \
    chown -R mysql:mysql /var/run/mysqld/mysqld.pid && \
    chmod -R 644 /var/run/mysqld/mysqld.sock && \
    chown -R mysql:mysql /var/lib/mysql /var/run/mysqld

RUN sed -i 's/\(^upload_max_filesize = \).*/\120M/' /etc/php/7.0/apache2/php.ini \
    && cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf_orig \
    && sed -i 's/^\(User\|Group\).*/\1 asterisk/' /etc/apache2/apache2.conf \
    && sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf

RUN a2enmod rewrite
RUN service apache2 restart

RUN chmod +x /run/*

CMD /run/startup.sh

EXPOSE 80 3306 5060 5061 5160 5161 4569 10000-20000/udp code here

docker 的配置如下:

# Remove folders mentioned here:
# https://wiki.archlinux.org/index.php/Rsync#As_a_backup_utility
/dev 
/proc
/sys
/tftpboot
/tmp
/run
/mnt
/media
/lost+found

# Allow files and directories
!/run/startup.sh

# Remove useless heavy files like /var/lib/scrapyd/reports.old
**/*.old
**/*.log
**/*.bak

# Remove docker
/var/lib/lxcfs
/var/lib/docker
/etc/docker
/root/.docker
/etc/init/docker.conf

# Remove the current program
/.dockerignore
/Dockerfile

和startup.sh:

#!/bin/bash -x

#https://docs.docker.com/engine/admin/multi-service_container/

/etc/init.d/mysql start
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start mysql: $status"
  exit $status
fi

fwconsole start
if [ $status -ne 0 ]; then
  echo "Failed to start fwconsole: $status"
  exit $status
fi


#restore backup if exists
if [ -f /backup/new.tgz ]; then
  echo "Restoring backup from /backup/new.tgz"
  php /var/www/html/admin/modules/backup/bin/restore.php --items=all --restore=/backup/new.tgz
  echo "Done"
fi
#restart freepbx to load everything fine after restoring backup
fwconsole stop
if [ $status -ne 0 ]; then
  echo "Failed to stop fwconsole: $status"
  exit $status
fi
fwconsole start
if [ $status -ne 0 ]; then
  echo "Failed to start fwconsole: $status"
  exit $status
fi


/etc/init.d/apache2 start
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start apache2: $status"
  exit $status
fi

/run/backup.sh &
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start backup.sh: $status"
  exit $status
fi

/run/delete-old-recordings.sh &
status=$?
if [ $status -ne 0 ]; then
  echo "Failed to start delete-old-recordings: $status"
  exit $status
fi

while /bin/true; do
  ps aux |grep mysqld |grep -q -v grep
  MYSQL_STATUS=$?
  ps aux |grep asterisk |grep -q -v grep
  ASTERISK_STATUS=$?
  ps aux |grep '/run/backup.sh' |grep -q -v grep
  BACKUPSCRIPT_STATUS=$?

  echo "Checking running processes..."
  if [ $MYSQL_STATUS -ne 0 -o $ASTERISK_STATUS -ne 0 -o $BACKUPSCRIPT_STATUS -ne 0 ]; then
    echo "One of the processes has already exited."
    exit -1
  fi
  echo "OK"
  sleep 60
done

在容器部署期间(docker run --name=pbx --net=macvlan_network --ip=192.168.00.55 -it --privileged --restart except-stopped pbx) 表明:

+ /etc/init.d/mysql start
[ ok ] Starting MariaDB database server: mysqld . . . . . . . . . . . . . . . . . . . . . . . . . . . . ..
+ status=0
+ '[' 0 -ne 0 ']'
+ fwconsole chown
Taking too long? Customize the chown command, See http://wiki.freepbx.org/display/FOP/FreePBX+Chown+Conf
Setting Permissions...
Setting base permissions...Done
Setting specific permissions...
 8486 [============================]
Finished setting permissions
+ fwconsole start
Running FreePBX startup...
Taking too long? Customize the chown command, See http://wiki.freepbx.org/display/FOP/FreePBX+Chown+Conf
Setting Permissions...
Setting base permissions...Done
Setting specific permissions...
 8486 [============================]
Finished setting permissions
Starting Asterisk...
[============================] 3 secs
Asterisk Started
Running Asterisk post from Core module
Starting Core FastAGI Server...
[>---------------------------] 3 secs
Started Core FastAGI Server. PID is 886
+ '[' 0 -ne 0 ']'
+ '[' -f /backup/new.tgz ']'
+ fwconsole stop
Running FreePBX shutdown...

Running Asterisk pre from Core module
Stopping Core FastAGI Server
Stopped FastAGI Server
Shutting down Asterisk Gracefully. Will forcefully kill after 30 seconds.
Press C to Cancel
Press N to shut down NOW
[============================] < 1 sec
+ '[' 0 -ne 0 ']'
+ fwconsole start
Running FreePBX startup...
Taking too long? Customize the chown command, See http://wiki.freepbx.org/display/FOP/FreePBX+Chown+Conf
Setting Permissions...
Setting base permissions...Done
Setting specific permissions...
 8501 [============================]
Finished setting permissions
Starting Asterisk...
[============================] 2 secs
Asterisk Started
Running Asterisk post from Core module
Starting Core FastAGI Server...
[>---------------------------] 2 secs
Started Core FastAGI Server. PID is 1276
+ '[' 0 -ne 0 ']'
+ /etc/init.d/apache2 start
[....] Starting Apache httpd web server: apache2AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 192.168.00.55. Set the 'ServerName' directive globally to suppress this message
. ok 
+ status=0
+ '[' 0 -ne 0 ']'
+ status=0
+ /run/backup.sh
+ '[' 0 -ne 0 ']'
/run/startup.sh: line 47: /run/backup.sh: No such file or directory
+ status=0
+ '[' 0 -ne 0 ']'
+ /bin/true
+ /run/delete-old-recordings.sh
/run/startup.sh: line 54: /run/delete-old-recordings.sh: No such file or directory
+ ps aux
+ grep mysqld
+ grep -q -v grep
+ MYSQL_STATUS=0
+ ps aux
+ grep asterisk
+ grep -q -v grep
+ ASTERISK_STATUS=0
+ ps aux
+ grep /run/backup.sh
+ grep -q -v grep
+ BACKUPSCRIPT_STATUS=1
+ echo 'Checking running processes...'
Checking running processes...
+ '[' 0 -ne 0 -o 0 -ne 0 -o 1 -ne 0 ']'
+ echo 'One of the processes has already exited.'
One of the processes has already exited.
+ exit -1

进入容器后:

# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.0   1940   372 pts/0    Ss+  20:48   0:00 /bin/sh -c /run/startup.sh
root         7  0.0  0.0   3196  2128 pts/0    S+   20:48   0:00 /bin/bash -x /run/startup.sh
root        35  0.0  0.0   1940  1196 pts/0    S+   20:48   0:00 /bin/sh /usr/bin/mysqld_safe
root       127  0.0  0.0   3328  2608 pts/1    Ss   20:48   0:00 /bin/bash
mysql      163  1.3  1.8 724776 71276 pts/0    Sl+  20:48   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/arm-linux
root       164  0.0  0.0   4188   816 pts/0    S+   20:48   0:00 logger -t mysqld -p daemon error
root       369  6.5  0.7  81056 30724 pts/0    S+   20:48   0:01 php /usr/sbin/fwconsole start
root       471  0.0  0.0   1940    84 pts/0    S+   20:48   0:00 /bin/sh /usr/sbin/safe_asterisk -U asterisk -G asterisk
asterisk   473 11.0  1.3 205468 51880 pts/0    Sl+  20:48   0:01 /usr/sbin/asterisk -f -U asterisk -G asterisk -vvvg -c
asterisk   581 20.2  1.0 156188 40372 ?        Ssl  20:48   0:02 PM2 v2.10.7: God Daemon (/home/asterisk/.pm2)
asterisk   636  103  0.5 107888 21780 ?        Rsl  20:48   0:01 node /var/www/html/admin/modules/pm2/node/node_modules/pm2/lib/ProcessContainerFork.js
root       646  0.0  0.0   1940   364 pts/0    S+   20:48   0:00 sh -c runuser 'asterisk' -s '/bin/bash' -c 'cd /var/www/html/admin/modules/pm2/node && m
root       647  1.0  0.0   4832  2156 pts/0    S+   20:48   0:00 runuser asterisk -s /bin/bash -c cd /var/www/html/admin/modules/pm2/node && mkdir -p /ho
asterisk   648 91.0  0.5 105448 20100 ?        Rsl  20:48   0:00 node /var/www/html/admin/modules/pm2/node/node_modules/pm2/bin/pm2 jlist
root       656  0.0  0.0   5176  2120 pts/1    R+   20:48   0:00 ps aux

但是,我仍然无法访问 Web 控制台。请问有人能帮我解决这个问题吗?

相关内容