docker_practice/image/multistage-builds/laravel.md

233 lines
5.7 KiB
Go
Raw Permalink Normal View History

# Laravel
2018-12-31 06:27:55 +00:00
2020-10-07 05:31:45 +00:00
> PHP `Laravel` 8.x
2018-12-31 06:27:55 +00:00
##
2018-12-31 06:27:55 +00:00
`Laravel` `Laravel` `Dockerfile` `.dockerignore` `laravel.conf`
`.dockerignore`
```bash
.idea/
.git/
2020-10-07 05:31:45 +00:00
2018-12-31 06:27:55 +00:00
vendor/
2020-10-07 05:31:45 +00:00
2018-12-31 06:27:55 +00:00
node_modules/
2020-10-07 05:31:45 +00:00
2018-12-31 06:27:55 +00:00
public/js/
public/css/
2020-10-07 05:31:45 +00:00
public/mix-manifest.json
2018-12-31 06:27:55 +00:00
yarn-error.log
bootstrap/cache/*
storage/
# .env.*
```
`laravel.conf` nginx
```nginx
server {
listen 80 default_server;
root /app/laravel/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .*\.php(\/.*)*$ {
fastcgi_pass laravel:9000;
include fastcgi.conf;
# fastcgi_connect_timeout 300;
# fastcgi_send_timeout 300;
# fastcgi_read_timeout 300;
}
}
```
##
2018-12-31 06:27:55 +00:00
```docker
2018-12-31 06:27:55 +00:00
FROM node:alpine as frontend
COPY package.json /app/
2020-10-07 05:31:45 +00:00
RUN set -x ; cd /app \
&& npm install --registry=https://registry.npmmirror.com
2018-12-31 06:27:55 +00:00
COPY webpack.mix.js webpack.config.js tailwind.config.js /app/
2020-10-07 05:31:45 +00:00
COPY resources/ /app/resources/
2018-12-31 06:27:55 +00:00
2020-10-07 05:31:45 +00:00
RUN set -x ; cd /app \
&& touch artisan \
&& mkdir -p public \
&& npm run production
2018-12-31 06:27:55 +00:00
```
## Composer
2018-12-31 06:27:55 +00:00
Composer
```docker
2018-12-31 06:27:55 +00:00
FROM composer as composer
COPY database/ /app/database/
COPY composer.json composer.lock /app/
2020-10-07 05:31:45 +00:00
RUN set -x ; cd /app \
2019-08-14 13:50:30 +00:00
&& composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ \
2018-12-31 06:27:55 +00:00
&& composer install \
--ignore-platform-reqs \
--no-interaction \
--no-plugins \
--no-scripts \
--prefer-dist
```
##
2018-12-31 06:27:55 +00:00
```docker
2020-10-07 05:31:45 +00:00
FROM php:7.4-fpm-alpine as laravel
2018-12-31 06:27:55 +00:00
ARG LARAVEL_PATH=/app/laravel
COPY --from=composer /app/vendor/ ${LARAVEL_PATH}/vendor/
COPY . ${LARAVEL_PATH}
COPY --from=frontend /app/public/js/ ${LARAVEL_PATH}/public/js/
COPY --from=frontend /app/public/css/ ${LARAVEL_PATH}/public/css/
2020-10-07 05:31:45 +00:00
COPY --from=frontend /app/public/mix-manifest.json ${LARAVEL_PATH}/public/mix-manifest.json
2018-12-31 06:27:55 +00:00
2020-10-07 05:31:45 +00:00
RUN set -x ; cd ${LARAVEL_PATH} \
2018-12-31 06:27:55 +00:00
&& mkdir -p storage \
&& mkdir -p storage/framework/cache \
&& mkdir -p storage/framework/sessions \
&& mkdir -p storage/framework/testing \
&& mkdir -p storage/framework/views \
&& mkdir -p storage/logs \
2020-10-07 05:31:45 +00:00
&& chmod -R 777 storage \
&& php artisan package:discover
2018-12-31 06:27:55 +00:00
```
## NGINX
2018-12-31 06:27:55 +00:00
```docker
2018-12-31 06:27:55 +00:00
FROM nginx:alpine as nginx
ARG LARAVEL_PATH=/app/laravel
COPY laravel.conf /etc/nginx/conf.d/
COPY --from=laravel ${LARAVEL_PATH}/public ${LARAVEL_PATH}/public
```
## Laravel Nginx
2018-12-31 06:27:55 +00:00
使 `docker build`
```bash
$ docker build -t my/laravel --target=laravel .
$ docker build -t my/nginx --target=nginx .
```
##
2018-12-31 06:27:55 +00:00
Docker
```bash
$ docker network create laravel
```
laravel `--name=laravel` `nginx` `fastcgi_pass laravel:9000;`
```bash
2020-10-07 05:31:45 +00:00
$ docker run -dit --rm --name=laravel --network=laravel my/laravel
2018-12-31 06:27:55 +00:00
```
nginx
```bash
2020-10-07 05:31:45 +00:00
$ docker run -dit --rm --network=laravel -p 8080:80 my/nginx
2018-12-31 06:27:55 +00:00
```
访 `127.0.0.1:8080` Laravel
> Laravel redisMySQL
##
2018-12-31 06:27:55 +00:00
便使 **** `config` `secret` `Swarm mode` `Kubernetes`
2020-10-07 05:31:45 +00:00
https://github.com/khs1994-docker/laravel-demo 项目。
##
2018-12-31 06:27:55 +00:00
`Dockerfile`
```docker
2018-12-31 06:27:55 +00:00
FROM node:alpine as frontend
COPY package.json /app/
2020-10-07 05:31:45 +00:00
RUN set -x ; cd /app \
&& npm install --registry=https://registry.npmmirror.com
2018-12-31 06:27:55 +00:00
COPY webpack.mix.js webpack.config.js tailwind.config.js /app/
2020-10-07 05:31:45 +00:00
COPY resources/ /app/resources/
2018-12-31 06:27:55 +00:00
2020-10-07 05:31:45 +00:00
RUN set -x ; cd /app \
&& touch artisan \
&& mkdir -p public \
2018-12-31 06:27:55 +00:00
&& npm run production
FROM composer as composer
COPY database/ /app/database/
COPY composer.json /app/
2020-10-07 05:31:45 +00:00
RUN set -x ; cd /app \
2019-08-14 13:50:30 +00:00
&& composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ \
2018-12-31 06:27:55 +00:00
&& composer install \
--ignore-platform-reqs \
--no-interaction \
--no-plugins \
--no-scripts \
--prefer-dist
2020-10-07 05:31:45 +00:00
FROM php:7.4-fpm-alpine as laravel
2018-12-31 06:27:55 +00:00
ARG LARAVEL_PATH=/app/laravel
COPY --from=composer /app/vendor/ ${LARAVEL_PATH}/vendor/
COPY . ${LARAVEL_PATH}
COPY --from=frontend /app/public/js/ ${LARAVEL_PATH}/public/js/
COPY --from=frontend /app/public/css/ ${LARAVEL_PATH}/public/css/
2020-10-07 05:31:45 +00:00
COPY --from=frontend /app/public/mix-manifest.json ${LARAVEL_PATH}/public/mix-manifest.json
2018-12-31 06:27:55 +00:00
2020-10-07 05:31:45 +00:00
RUN set -x ; cd ${LARAVEL_PATH} \
2018-12-31 06:27:55 +00:00
&& mkdir -p storage \
&& mkdir -p storage/framework/cache \
&& mkdir -p storage/framework/sessions \
&& mkdir -p storage/framework/testing \
&& mkdir -p storage/framework/views \
&& mkdir -p storage/logs \
2020-10-07 05:31:45 +00:00
&& chmod -R 777 storage \
&& php artisan package:discover
2018-12-31 06:27:55 +00:00
FROM nginx:alpine as nginx
ARG LARAVEL_PATH=/app/laravel
COPY laravel.conf /etc/nginx/conf.d/
COPY --from=laravel ${LARAVEL_PATH}/public ${LARAVEL_PATH}/public
```