如何使用Docker在Alpine Linux上部署Laravel Web应用程序? [英] How to deploy a Laravel Web Application on Alpine Linux using Docker?

查看:94
本文介绍了如何使用Docker在Alpine Linux上部署Laravel Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功使用PHP中的基础映像在ECS上成功部署了Laravel Web应用程序,尤其是

I am successfully deploying a Laravel Web Application on ECS using a base image from PHP, in particular 7.3-apache-stretch from https://hub.docker.com/_/php/

请充分了解有关Docker中 Alpine Linux 映像的讨论(以最终的图片尺寸),我想对其进行一次测试,以了解其效果.不幸的是,尽管使用CLI版本非常容易(使用7.3-cli-alpine3.9),但没有启用apache的版本.我需要的是Dockerfile作为开发基础.

Being well aware of the discussion about Alpine Linux Images in Docker (granting significative reductions in the final image dimension), I wanted to give it a run, to see how it performed. Unfortunately, while with the CLI version it was very easy (using 7.3-cli-alpine3.9), there is no apache-enabled version. What I would need is a Dockerfile to use as a base for my developments.

仅Apache

正在浏览SO,我发现如何做我在Docker的Alpine上运行Apache 2吗?引起了我的注意 https://github.com/nimmis/docker-alpine-apache ,启用apache,但PHP完全缺失,因此我必须将其集成.

Browsing SO, I found How do I run Apache 2 on Alpine in Docker? that brought my attention to https://github.com/nimmis/docker-alpine-apache, that enables apache, but PHP is completely missing, so I'd have to integrate this.

通过FCGI运行Apache/NGINX和PHP

另一个问题 PHP和Apache/httpd的高山变体Docker中的使我们更加接近,但是暗示了使用两个容器,这不是我想要的.

This other question Alpine variants of PHP and Apache/httpd in Docker brings us closer, but implies theuse of two containers, that is not what I want to have.

Dockerfile应该如何让我现成部署Laravel Web应用程序?

How should the Dockerfile be to let me deploy a Laravel Web Application off the shelf ?

推荐答案

经过两天的尝试,我终于到达了可以在支持php的apache容器上部署Laravel应用程序的地步.由于发现的问题数不胜数,因此以下是最后的Dockerfile以及各节的说明:

After two days of attempts, I finally arrived to a point in which I am able to deploy my Laravel Application on a php-enabled apache container. Since the number of issues found was countless, here is the final Dockerfile, and an explanation of the sections:

# PHP Images can be found at https://hub.docker.com/_/php/
FROM php:7.3-alpine3.9

# The application will be copied in /home/application and the original document root will be replaced in the apache configuration 
COPY . /home/application/ 

# Custom Document Root
ENV APACHE_DOCUMENT_ROOT /home/application/public

# Concatenated RUN commands
RUN apk add --update apache2 php7-apache2 php7-mbstring php7-session php7-json php7-pdo php7-openssl php7-tokenizer php7-pdo php7-pdo_mysql php7-xml php7-simplexml\
    && chmod -R 777 /home/application/storage \
    && chown -R www-data:www-data /home/application \
    && mkdir -p /run/apache2 \
    && sed -i '/LoadModule rewrite_module/s/^#//g' /etc/apache2/httpd.conf \
    && sed -i '/LoadModule session_module/s/^#//g' /etc/apache2/httpd.conf \
    && sed -ri -e 's!/var/www/localhost/htdocs!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/httpd.conf \
    && sed -i 's/AllowOverride\ None/AllowOverride\ All/g' /etc/apache2/httpd.conf \
    && docker-php-ext-install pdo_mysql \
    && rm  -rf /tmp/* /var/cache/apk/*

# Launch the httpd in foreground
CMD rm -rf /run/apache2/* || true && /usr/sbin/httpd -DFOREGROUND

这是我在Dockerfile

  1. 首先,我将所有内容都建立在基于高山分布的PHP映像上.
  2. 我将所有Laravel源代码复制到/home/application
  3. 我将文档根目录设置为我的public Laravel文件夹
  4. 通过apk请求安装操作系统软件包(所有这些都是我的Laravel应用程序所必需的).可用软件包的完整列表可在 http://dl- cdn.alpinelinux.org/alpine/edge/community/x86_64/
  5. 扩展storage文件夹上的权限
  6. 更改整个/home/application/文件夹的所有者
  7. 启用所有需要的模块(可能需要不同的模块,具体取决于应用程序)
  8. 更改httpd.conf文件中的文档根目录
  9. 启用AllowOverride All指令
  10. 启用pdo_mysql扩展名(否则命令将无法访问mysql)
  11. 清理包装系统的缓存
  12. 运行httpd
  1. First of all, I base everything on a PHP image, based on alpine distribution.
  2. I copy all of my Laravel source code on /home/application
  3. I set the document root to my public Laravel folder
  4. Request the installation of the operative system packages via apk (all of them were required for my Laravel application). A full list of the available packages can be found on http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/
  5. Extend permissions on the storage folder
  6. Change the owner of the whole /home/application/ folder
  7. Enable all the needed modules (different modules could be required, depending of the application)
  8. Change the document root in the httpd.conf file
  9. Enable the AllowOverride All instruction
  10. Enable the pdo_mysql extension (otherwise commands will not be able to access mysql)
  11. Clean the cache of the packaging system
  12. Run httpd

使用此Dockerfile,现在可以运行所有Laravel Web应用程序,只需在/home/application/

Using this Dockerfile, it's now possible to run all of the Laravel Web Applications, it will just be a matter of copying the application source code in /home/application/

这篇关于如何使用Docker在Alpine Linux上部署Laravel Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆