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:
Software: PHP 7.2, Nginx, MySQL, Composer, NPM or Yarn, etc.;
Use domestic mirroring;
Docker-Compose
Here we are using the "DaoCloud" accelerated image -
7.2-fpm-alpineThis version uses both the PHP 7.2
version and the
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 <yemeishu@126.com>
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 \
&& apk add --no-cache \
curl \
git \
imagemagick \
mysql-client \
postgresql-libs \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install \
curl \
iconv \
mbstring \
pdo \
pdo_mysql \
pdo_pgsql \
pdo_sqlite \
pcntl \
tokenizer \
xml \
zip \
&& curl -s https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer \
&& apk del -f .build-deps
# 修改 composer 为国内镜像
RUN composer config -g repo.packagist composer https://packagist.laravel-china.org
# install prestissimo
RUN composer global require "hirak/prestissimo"
# install laravel envoy
RUN composer global require "laravel/envoy"
#install laravel installer
RUN composer global require "laravel/installer"
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk update && apk add -u nodejs libpng-dev python
ENV PATH /root/.yarn/bin:$PATH
RUN apk update \
&& apk add curl bash binutils tar \
&& rm -rf /var/cache/apk/* \
&& /bin/bash \
&& touch ~/.bashrc \
&& curl -o- -L https://yarnpkg.com/install.sh | bash \
&& yarn config set registry 'https://registry.npm.taobao.org' \
&& 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
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
. <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 <yemeishu@126.com>
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:
Test it againCreate Laravel project
composer create-project laravel/laravel demo
folder and the composer.lock file. git clone
In the same folder as the
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
app: build: context: ./ dockerfile: app.dockerfile working_dir: /var/www volumes: - ../:/var/www
buildin
docker-compose up
The whole speed is quite fast
Next enter the container
docker exec -it de075c525528 bash
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
USEcnpmyarn
or
Install plugin:
Generate Laravel key secret
:
cp .env.example .env php artisan key:generate Application key [base64:4A7VK6MEX7FakPLDSLji97kz/nyWUAWhW4wYn3gefsY=] set successfully.
Run Let’s take a look at the effect:
.env:
DB_CONNECTION=mysql DB_HOST=database DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
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
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. .
Related recommendations:
Parsing of URL access patterns in TP5
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!