Docker部署Laravel应用的实现示例
- 本文PHP基础镜像使用:php:7.3-apache
- 本文Laravel版本为:laravel:5.8.*
- 队列和任务调度我们在下一篇写
一、准备我们的Laravel应用
#运行mix命令,打包js、css、img文件,如果没有用到mix这一步可以省略 npminstall npmrunproduction #安装composer.lock中的依赖 composerinstall--ignore-platform-reqs--optimize-autoloader--no-dev
二、准备Apache配置文件docker/000-default.conf
#TheServerNamedirectivesetstherequestscheme,hostnameandportthat #theserverusestoidentifyitself.Thisisusedwhencreating #redirectionURLs.Inthecontextofvirtualhosts,theServerName #specifieswhathostnamemustappearintherequest'sHost:headerto #matchthisvirtualhost.Forthedefaultvirtualhost(thisfile)this #valueisnotdecisiveasitisusedasalastresorthostregardless. #However,youmustsetitforanyfurthervirtualhostexplicitly. #ServerNamewww.example.com ServerAdminliuzhaowei55@gmail.com DocumentRoot/var/www/html/public #Availableloglevels:trace8,...,trace1,debug,info,notice,warn, #error,crit,alert,emerg. #Itisalsopossibletoconfiguretheloglevelforparticular #modules,e.g. #LogLevelinfossl:warn ErrorLog${APACHE_LOG_DIR}/error.log CustomLog${APACHE_LOG_DIR}/access.logcombined #Formostconfigurationfilesfromconf-available/,whichare #enabledordisabledatagloballevel,itispossibleto #includealineforonlyoneparticularvirtualhost.Forexamplethe #followinglineenablestheCGIconfigurationforthishostonly #afterithasbeengloballydisabledwith"a2disconf". #Includeconf-available/serve-cgi-bin.conf
三、准备Dockerfile文件
FROMphp:7.3-apache
LABELmaintainer="liuzhaowei55@gmail.com"
#设置时区
ARGTZ=Asia/Shanghai
ENVTZ${TZ}
RUNln-snf/usr/share/zoneinfo/$TZ/etc/localtime&&echo$TZ>/etc/timezone
#安装软件cron
RUNset-eux\
&&apt-getupdate\
&&apt-getinstall-y--no-install-recommendscron\
&&apt-getautoremove\
&&apt-getautoclean\
&&apt-getclean\
&&rm-rf/var/lib/apt/lists/*/tmp/*/var/tmp/*
#安装拓展
ENVPHP_EXTENSION\
pdo_mysql\
bcmath
ENVPECL_EXTENSION\
redis
RUNdocker-php-ext-install$PHP_EXTENSION\
&&peclinstall$PECL_EXTENSION\
&&docker-php-ext-enable$PECL_EXTENSION\
opcache\
&&a2enmodrewrite
#访问端口
EXPOSE80
ARGAPP_ENV=development
ENVAPP_ENV${APP_ENV}
COPY--chown=www-data:www-data./var/www/html
COPYdocker/000-default.conf/etc/apache2/sites-available/000-default.conf
WORKDIR/var/www/html
四、准备Dockerignore文件:.dockerignore(非必需)
/node_modules /.dockerignore /Dockerfile
五、准备镜像入口文件:docker-entrypoint.sh(非必需)
注意一下,此文件需要执行权限,不过此入口文件不是必须的,只是为了在应用运行前执行一些缓存命令,具体可以点这里去文档了解一下。
#!/usr/bin/envbash phpartisancache:clear phpartisanconfig:cache phpartisanroute:cache phpartisanview:cache apache2-foreground
六、运行镜像
dockerrun-p80:80.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。