一、创建容器时运行包含自启动路径的命令

docker run -p 80:80 -i -t -d --restart=always ubuntu:18.04 /bin/bash -c "/etc/rc.local; /bin/bash"

然后:

 二、所有想在开机执行的命令都可以写在里面

自启动
vim /etc/rc.local

exit上面新增
service apache2 start
service mysql start

最后大概这个样子

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
service apache2 start
service mysql start
exit 0

记得要赋予文件可执行权限

chmod 777 /etc/rc.local

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐