Table of Contents
php-fpm
.
, mainly to load the website configuration file into
For testing, you can delete the
project,
file:
Execute the build command under:
You can also learn more by building your own docker development environment. In the future, we will continue to improve and minimize it to meet development needs.
Home Backend Development PHP Tutorial How to build a Docker development environment for Laravel yourself

How to build a Docker development environment for Laravel yourself

Jul 23, 2018 am 10:13 AM
docker laravel php

The content of this article is about setting up a Docker development environment for Laravel. It has certain reference value. Friends in need can refer to it.

I haven’t written anything for a long time. Today I will talk about how to build a Docker environment for Laravel to run.

The most famous one on the market is "laradock" https://github.com/laradock/laradock

Docker PHP development environment.
Usage reference: http:/ /laradock.io

Since it is "self-built", we can refer to this to minimize the needs for Laravel operation.

The following are the basic conditions I listed:

  1. Software: PHP 7.2, Nginx, MySQL, Composer, NPM or Yarn, etc.;

  2. Use domestic mirroring;

  3. Docker-Compose

  4. To achieve scalability, just like "laradock", use the Docker-Compose orchestration method to assemble several core images together .

php-fpm

Here we are using the "DaoCloud" accelerated image -

7.2-fpm-alpine

.

This version uses both the PHP 7.2 version and the

alpine

minimized system. Based on this, you can install additional tools required for the environment: such as, composer, nodejs, python, yarn, etc. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">FROM daocloud.io/php:7.2-fpm-alpine MAINTAINER coding01 &lt;yemeishu@126.com&gt; RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories RUN apk add --no-cache --virtual .build-deps \         $PHPIZE_DEPS \         curl-dev \         imagemagick-dev \         libtool \         libxml2-dev \         postgresql-dev \         sqlite-dev \     &amp;&amp; apk add --no-cache \         curl \         git \         imagemagick \         mysql-client \         postgresql-libs \     &amp;&amp; pecl install imagick \     &amp;&amp; docker-php-ext-enable imagick \     &amp;&amp; docker-php-ext-install \         curl \         iconv \         mbstring \         pdo \         pdo_mysql \         pdo_pgsql \         pdo_sqlite \         pcntl \         tokenizer \         xml \         zip \     &amp;&amp; curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer \     &amp;&amp; apk del -f .build-deps # 修改 composer 为国内镜像 RUN composer config -g repo.packagist composer https://packagist.laravel-china.org # install prestissimo RUN composer global require &quot;hirak/prestissimo&quot; # install laravel envoy RUN composer global require &quot;laravel/envoy&quot; #install laravel installer RUN composer global require &quot;laravel/installer&quot; RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories RUN apk update &amp;&amp; apk add -u nodejs libpng-dev python ENV PATH /root/.yarn/bin:$PATH RUN apk update \   &amp;&amp; apk add curl bash binutils tar \   &amp;&amp; rm -rf /var/cache/apk/* \   &amp;&amp; /bin/bash \   &amp;&amp; touch ~/.bashrc \   &amp;&amp; curl -o- -L https://yarnpkg.com/install.sh | bash \   &amp;&amp; yarn config set registry 'https://registry.npm.taobao.org' \   &amp;&amp; npm install -g cnpm --registry=https://registry.npm.taobao.org WORKDIR /var/www</pre><div class="contentsignin">Copy after login</div></div>In which to install the alpine system plug-in, we use the

mirrors.aliyun.com

Alibaba Cloud mirror. php:7.2-fpm-alpine For specific usage, please refer to: https://dashboard.daocloud.io/packages/019c8dce-ec80-4468-bddc-254fc62ef5c7

nginxWe use

nginx

, mainly to load the website configuration file into

nginx

. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">FROM daocloud.io/nginx:1.13-alpine MAINTAINER coding01 &lt;yemeishu@126.com&gt; ADD vhost.conf /etc/nginx/conf.d/default.conf WORKDIR /var/www</pre><div class="contentsignin">Copy after login</div></div>The rest is to connect these images. Finally, take a look at docker-compose.yml File content:

version: '2'
services:

  # The Application
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ../:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"
      - "REDIS_HOST=redis"
      - "REDIS_PORT=6379"

  # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
      - app
    ports:
      - 8080:80

  # The Database
  database:
    image: daocloud.io/mysql:5.7.4
    volumes:
      - dbdata:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=homestead"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
        - "3306:3306"

  redis:
    image: daocloud.io/library/redis:4.0.10-alpine
    command: redis-server --appendonly yes

volumes:
  dbdata:
Copy after login

Test it againCreate Laravel project

composer create-project laravel/laravel demo
Copy after login

Note:

For testing, you can delete the

vendor

folder and the composer.lock file. git cloneIn the same folder as the

demo

project,

git clone

our self-built "laraveldocker": <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">git clone https://github.com/fanly/laraveldocker.git</pre><div class="contentsignin">Copy after login</div></div>Modify docker-compose.ymlExecute our project with the path of the

docker-compose.yml

file:

app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ../:/var/www
Copy after login

buildin

laraveldocker

Execute the build command under:

docker-compose up
Copy after login

The whole speed is quite fast

Next enter the container

docker exec -it de075c525528 bash
Copy after login

Let’s see the effect of installing the plug-in:

Using https:/ /packagist.laravel-china.org
Domestic mirror.

Note: This mirror is a public welfare project jointly launched by the Laravel China community, Youpaiyun and Youfanyuanyang, aiming to provide stable and high-speed Composer domestic mirroring services for the majority of PHP users. It is recommended to use

Reference: http://laravel-china.org/topics/4484/composer-mirror-use-help

USE

yarn

or
cnpm

Install plugin:

Generate Laravel key secret
:

cp .env.example .env
php artisan key:generate

Application key [base64:4A7VK6MEX7FakPLDSLji97kz/nyWUAWhW4wYn3gefsY=] set successfully.
Copy after login

Run Let’s take a look at the effect:

Let’s take a look at the database connection next, modify

.env:

DB_CONNECTION=mysql
DB_HOST=database
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Copy after login

We use php artisan make: auth to generate layout, registration and login views and routes for all authentication interfaces. At the same time, it will also generate

HomeController

to handle the application’s login request. Use php artisan migrate to load data.

Let’s take a look at the data table:

At this point, it shows that we are connected MySQL
The database is OK.

SummaryIn the learning process, it is best to use the Dockerfile made by others. Although it can be used directly, it would be best if it can be self-sufficient. .

You can also learn more by building your own docker development environment. In the future, we will continue to improve and minimize it to meet development needs.

Related recommendations:

Parsing of URL access patterns in TP5


Nginx Configuration detailed code

The above is the detailed content of How to build a Docker development environment for Laravel yourself. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

What is the difference between php framework laravel and yii What is the difference between php framework laravel and yii Apr 30, 2025 pm 02:24 PM

The main differences between Laravel and Yii are design concepts, functional characteristics and usage scenarios. 1.Laravel focuses on the simplicity and pleasure of development, and provides rich functions such as EloquentORM and Artisan tools, suitable for rapid development and beginners. 2.Yii emphasizes performance and efficiency, is suitable for high-load applications, and provides efficient ActiveRecord and cache systems, but has a steep learning curve.

H5: Key Improvements in HTML5 H5: Key Improvements in HTML5 Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

Docker vs. Kubernetes: Key Differences and Synergies Docker vs. Kubernetes: Key Differences and Synergies May 01, 2025 am 12:09 AM

Docker and Kubernetes are leaders in containerization and orchestration. Docker focuses on container lifecycle management and is suitable for small projects; Kubernetes is good at container orchestration and is suitable for large-scale production environments. The combination of the two can improve development and deployment efficiency.

Recommended Laravel's best expansion packs: 2024 essential tools Recommended Laravel's best expansion packs: 2024 essential tools Apr 30, 2025 pm 02:18 PM

The essential Laravel extension packages for 2024 include: 1. LaravelDebugbar, used to monitor and debug code; 2. LaravelTelescope, providing detailed application monitoring; 3. LaravelHorizon, managing Redis queue tasks. These expansion packs can improve development efficiency and application performance.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Laravel logs and error monitoring: Sentry and Bugsnag integration Laravel logs and error monitoring: Sentry and Bugsnag integration Apr 30, 2025 pm 02:39 PM

Integrating Sentry and Bugsnag in Laravel can improve application stability and performance. 1. Add SentrySDK in composer.json. 2. Add Sentry service provider in config/app.php. 3. Configure SentryDSN in the .env file. 4. Add Sentry error report in App\Exceptions\Handler.php. 5. Use Sentry to catch and report exceptions and add additional context information. 6. Add Bugsnag error report in App\Exceptions\Handler.php. 7. Use Bugsnag monitoring

See all articles