Example of deploying Laravel application with Docker

Example of deploying Laravel application with Docker
  • The PHP base image used in this article is: php:7.3-apache
  • The Laravel version in this article is: laravel:5.8.*
  • We will write about queues and task scheduling in the next article

1. Prepare our Laravel application

# Run the mix command to package js, css, and img files. If you do not use mix, you can skip npm install.
npm run production
# Install dependencies in composer.lock composer install --ignore-platform-reqs --optimize-autoloader --no-dev

2. Prepare the Apache configuration file docker/000-default.conf

<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 request's 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 [email protected]
 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
 # 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>

3. Prepare the Dockerfile

FROM php:7.3-apache

LABEL maintainer="[email protected]"

# Set time zone ARG TZ=Asia/Shanghai
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Install software 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/*
 
# Install extension 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
 
# Access port 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

4. Prepare Dockerignore file: .dockerignore (optional)

/node_modules
/.dockerignore
/Dockerfile

5. Prepare the image entry file: docker-entrypoint.sh (optional)

Note that this file requires execution permissions, but this entry file is not necessary. It is just to execute some cache commands before the application runs. You can click here to learn more about it.

#!/usr/bin/env bash

php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache

apache2-foreground

6. Run the image

docker run -p 80:80 .

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of how to install the laravel development environment with docker
  • Detailed tutorial on using Docker to build a local Laravel environment
  • How to use Docker compose to orchestrate Laravel applications
  • Detailed explanation of using Docker to build a development environment for Laravel and Vue projects
  • Docker deploys Laravel application to realize queue & task scheduling
  • Complete steps to build a Laravel development environment using Docker

<<:  MySQL 5.7.23 winx64 installation and configuration method graphic tutorial under win10

>>:  How to use skeleton screen in vue project

Recommend

Some understanding of absolute and relative positioning of page elements

From today on, I will regularly organize some smal...

Detailed explanation of the use of vue-resource interceptors

Preface Interceptor In some modern front-end fram...

Summary of common Mysql DDL operations

Library Management Create a library create databa...

How to use Docker Compose to implement nginx load balancing

Implement Nginx load balancing based on Docker ne...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...

Ubuntu 20.04 how to modify the IP address example

illustrate: Today, when continuing the last offic...

javascript to switch by clicking on the picture

Clicking to switch pictures is very common in lif...

CSS imitates Apple's smooth switch button effect

Table of contents 1. Code analysis 2. Source code...

Nginx defines domain name access method

I'm building Nginx recently, but I can't ...

MySQL 4 common master-slave replication architectures

Table of contents One master and multiple slaves ...

Using zabbix to monitor the ogg process (Linux platform)

The ogg process of a database produced some time ...

MySQL 5.7.15 installation and configuration method graphic tutorial (windows)

Because I need to install MySQL, I record the ins...

MySQL database SELECT query expression analysis

A large part of data management is searching, and...