Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy a Laravel Web Application on Alpine Linux using Docker?

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/

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 Only

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.

Running Apache/NGINX and PHP with FCGI

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.

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

like image 584
Ing. Luca Stucchi Avatar asked Mar 20 '19 15:03

Ing. Luca Stucchi


People also ask

Does Alpine Linux have docker?

To install Docker on Alpine Linux, run apk add --update docker openrc. The Docker package is available in the Community repository.


Video Answer


1 Answers

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

This is a short list of the operations that I made in the Dockerfile

  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

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/

like image 108
Ing. Luca Stucchi Avatar answered Oct 09 '22 08:10

Ing. Luca Stucchi