小知识:Docker部署Laravel应用的实现示例

本文 PHP 基础镜像使用:php:7.3-apache 本文 Laravel 版本为:laravel:5.8.* 队列和任务调度我们在下一篇

一、 准备我们的 Laravel 应用

?
1
2
3
4
5
# 运行 mix 命令,打包 js、css、img 文件,如果没有用到 mix 这一步可以省略
npm install
npm run production
# 安装 composer.lock 中的依赖
composer install –ignore-platform-reqs –optimize-autoloader –no-dev

二、 准备 Apache 配置文件 docker/000-default.conf

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the requests Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin liuzhaowei55@gmail.com
DocumentRoot /var/www/html/public
# Available loglevels: trace8, …, trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with “a2disconf”.
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>

三、 准备 Dockerfile 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
FROM php:7.3-apache
LABEL maintainer=”liuzhaowei55@gmail.com”
# 设置时区
ARG TZ=Asia/Shanghai
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 安装软件 cron
RUN set -eux \
&& apt-get update \
&& apt-get install -y –no-install-recommends cron \
&& apt-get autoremove \
&& apt-get autoclean \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# 安装拓展
ENV PHP_EXTENSION \
pdo_mysql \
bcmath
ENV PECL_EXTENSION \
redis
RUN docker-php-ext-install $PHP_EXTENSION \
&& pecl install $PECL_EXTENSION \
&& docker-php-ext-enable $PECL_EXTENSION \
opcache \
&& a2enmod rewrite
# 访问端口
EXPOSE 80
ARG APP_ENV=development
ENV APP_ENV ${APP_ENV}
COPY –chown=www-data:www-data . /var/www/html
COPY docker/000-default.conf /etc/apache2/sites-available/000-default.conf
WORKDIR /var/www/html

四、准备 Dockerignore 文件:.dockerignore(非必需)

?
1
2
3
/node_modules
/.dockerignore
/Dockerfile

五、准备镜像入口文件:docker-entrypoint.sh(非必需)

注意一下,此文件需要执行权限,不过此入口文件不是必须的,只是为了在应用运行前执行一些缓存命令,具体可以点这里去文档了解一下。

?
1
2
3
4
5
6
7
8
#!/usr/bin/env bash
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
apache2-foreground

六、运行镜像

?
1
docker run -p 80:80 .

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000020257310

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:使用docker部署django技术栈项目的方法步骤

2023-4-2 13:27:07

建站知识

小知识:Docker搭建Zookeeper&Kafka集群的实现

2023-4-2 13:49:07

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索