This article explains how to build a Laravel environment using VS Code's dev container. You can build an environment at high speed as long as you have the tools to use, and since you are using Docker, you can use it as a unified development environment for your team. If you want to move it for the time being, please use the repository link.
https://github.com/naoyayamamoto/laravel-docker-sample Since it is prepared as a template repository, it can also be used as a template.
Use the above tools.
Installation details for Docker
and Visual Studio Code
are omitted.
Visual Studio Code Remote --Containers
is a VS Code plug-in made by Microsoft. You can install it in VS Code from the link.
$ git clone https://github.com/naoyayamamoto/laravel-docker-sample
$ code laravel-docker-sample
#If you don't have the code command, open the repository with VS Code
Visual Studio Code Remote --Containers
, an icon has been added at the bottom left, so press it to open the dialog, or Show All Commands cmd + shift + P
to open the dialog.Remote-Containers: Reopen in Container
to start preparing the environment.Go to http: // localhost: 8000
and you should see the Laravel initial page.
When you open devcontainer, the Docker environment will be built based on .devcontainer/docker-compose.yml
.
We are using the following containers and will explain each of them.
nginx:alpine (web)
php artisan serve
.devcontainer/docker/nginx/default.conf
and send the process to php-fpmmysql:8 (db)
.devcontainer/docker/mysql/my.cnf
is loaded (Japanese basic settings)MYSQL_ALLOW_EMPTY_PASSWORD:'yes'
MYSQL_DATABASE: laravel
is set, so no root password, initial table laravel
is created.devcontainer/docker/php/Dockerfile
composer
・ node
・ npm
・ yarn
can be used.devcontainer/docker/php/php-fpm.d/zzz-www.conf
to run php-fpm on sockDB_HOST: db
is set, this is prioritized over the setting in .env and connected to the db container.The Dockerfile that builds for production has been added to the repository, so please use it as a source of customization. We use a multi-stage build to compile js and install composer in advance to keep the final image as small as possible. I only pass build tests with Github Actions.
For reference, it is a setting when using it with kubernetes. I'm assuming you want to run nginx and php as 1POD.
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
default.conf: |
access_log /dev/stdout main;
error_log /dev/stderr warn;
server {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_http_version 1.0;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
listen 80;
root /workspace/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
---
kind: Service
apiVersion: v1
metadata:
name: laravel-service
spec:
selector:
app: laravel
ports:
- protocol: TCP
port: 80
targetPort: 80
name: laravel-http
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: laravel
labels:
app: laravel
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: laravel
template:
metadata:
labels:
app: laravel
spec:
containers:
- name: app
image: myimage
volumeMounts:
- mountPath: /var/run/php-fpm
name: php-fpm-socket
- mountPath: /shared
name: public-contents
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "cp -aT /workspace/public /shared"]
- name: web
image: nginx:alpine
ports:
- containerPort: 80
volumeMounts:
- mountPath: /var/run/php-fpm
name: php-fpm-socket
- mountPath: /etc/nginx/conf.d
name: nginx-conf
- mountPath: /workspace/public
name: public-contents
volumes:
- name: php-fpm-socket
emptyDir: {}
- name: public-contents
emptyDir: {}
- name: nginx-conf
configMap:
name: nginx-conf
items:
- key: default.conf
path: default.conf
Recommended Posts