问题集合
自定义景象
- 运行镜像自动退出
主要原因是docker一个容器运行一个实例,所以docker需要运行一个应用程序,比如我们要安装某个程序,此时可以写一个shell脚本,然后通过entrypoint或者cmd运行,有个小技巧在shell命令里程序运行后使用tail -f或者tail -F进行日志文件跟踪(防止没有程序运行导致docker容器关闭)。
cat>startup.sh<<EOF
#!/bin/bash
if [ -d "/opt/dmdbms/bin" ]; then
if [ ! -d "/opt/dmdbms/data/DAMENG" ]; then
cd /opt/dmdbms/bin
./dminit PATH=/opt/dmdbms/data DB_NAME=DAMENG INSTANCE_NAME=DMSERVER PORT_NUM=5236
echo "Init DM success!"
fi
cd /opt/dmdbms/bin
echo "Start DmAPService..."
./DmAPService start
if [ ! -f "/opt/dmdbms/conf/dm.ini" ]; then
echo "/opt/dmdbms/conf/dm.ini does not exist, use default dm.ini"
./dmserver /opt/dmdbms/data/DAMENG/dm.ini -noconsole > /opt/dmdbms/log/DmServiceDMSERVER.log 2>&1 &
else
./dmserver /opt/dmdbms/conf/dm.ini -noconsole > /opt/dmdbms/log/DmServiceDMSERVER.log 2>&1 &
fi
echo "Start DMSERVER success!"
else
echo "/opt/dmdbms/bin 目录不存在!"
echo "请确认 /opt/dmdbms 目录是否安装dm7!"
fi
echo 'start container [ok]'
# 此处可以使用tail -F log 跟踪日志文件
tail -f /dev/null
EOF
此处是dm8镜像的startup.sh末尾(可以自行模仿)
echo "Start DMSERVER success!"
wait_dm_running
wait_dm_ready
if [ ! -f "/opt/dmdbms/log/dm_DMSERVER.log" ]; then
current_year_month=`date +%Y%m`
DM_LOG=dm_DMSERVER_${current_year_month}.log
ln -s /opt/dmdbms/log/${DM_LOG} /opt/dmdbms/log/dm_DMSERVER.log
echo "Finished soft link DM current ${DM_LOG} to dm_DMSERVER.log"
fi
echo "5 0 1 * * root /opt/switchDmLog.sh" >> /etc/crontab
#systemctl restart crond.service
/etc/init.d/cron start
#echo "Start Cron Service"
tail -F /opt/dmdbms/log/dm_DMSERVER.log
tail -f /dev/null
- 设置时区
# 通过环境变量设置时区
TZ=Asia/Shanghai
# 通过环境变量设置时区
environment:
- TZ=Asia/Shanghai # 设置容器时区为CST
# 或者挂载磁盘
-v /etc/timezone:/etc/timezone:ro
-v /etc/localtime:/etc/localtime:ro
- docker中启动服务
# 问题1 docker mount 出现 'mount: permission denied' 需要添加 --privileged
https://blog.csdn.net/weixin_43793181/article/details/118758796
# 问题2 提示 Failed to get D-Bus connection: Operation not permitted
/usr/sbin/init
# 问题3 提示 New main PID 2499 does not belong to service, and PID file is not owned by root. Refusing.
-v /sys/fs/cgroup:/sys/fs/cgroup
# 后台运行容器
docker run -d --privileged centos:7 /usr/sbin/init
# 进入容器安装
docker exec -it container_id /bin/bash