Build a Laravel / Docker environment with VSCode devcontainer

Overview

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.

Repository

https://github.com/naoyayamamoto/laravel-docker-sample Since it is prepared as a template repository, it can also be used as a template.

Tools used

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.

how to use

Open repository with VS Code

$ 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

Open VS Code using devcontaner

  1. If VS Code contains 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.
  2. Run Remote-Containers: Reopen in Container to start preparing the environment.
  3. It takes time to prepare Docker for the first time, but it starts up at high speed after the second time.

Access confirmation

Go to http: // localhost: 8000 and you should see the Laravel initial page.

laravel-8-top.jpg

Commentary

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)

mysql:8 (db)

Customized for VS Code php: 7-fpm (app)

bonus

Dockerfile for production environment

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.

When using with kubernetes

For reference, it is a setting when using it with kubernetes. I'm assuming you want to run nginx and php as 1POD.

Reference deployment.yml
---
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

Build a Laravel / Docker environment with VSCode devcontainer
Build a Node.js environment with Docker
Build PlantUML environment with VSCode + Docker
Build docker + laravel environment with laradock
Build a Wordpress development environment with Docker
Build a WordPress development environment quickly with Docker
Build a development environment for Docker, java, vscode
Build docker environment with WSL
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
Easily build a Vue.js environment with Docker + Vue CLI
[Note] Build a Python3 environment with Docker in EC2
Create a Vue3 environment with Docker!
Build Couchbase local environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Create Laravel environment with Docker (docker-compose)
Build a Node-RED environment with Docker to move and understand
Environment construction with Docker (Ubuntu20.04) + Laravel + nginx
Create a MySQL environment with Docker from 0-> 1
Build a WAS execution environment from Docker
Laravel + MySQL + phpMyadmin environment construction with Docker
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build TensorFlow operation check environment with Docker
How to build Rails 6 environment with Docker
Build a simple Docker + Django development environment
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
Build a SPA for Laravel 6.2 / Vue.js / Nginx / Mysql / Redis with Docker
Build a development environment for Django + MySQL + nginx with Docker Compose
I tried to build a laravel operating environment while remembering Docker
Build a development environment for Docker + Rails6 + Postgresql
One file of Docker x Laravel threat! Build a local development environment with the minimum configuration
[Memo] Create a CentOS 8 environment easily with Docker
I tried to build a Firebase application development environment with Docker in 2020
Build a simple Docker Compose + Django development environment
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build a Laravel environment on an AWS instance
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
Build a container for Docker x Laravel phpMyAdmin
[Win10] Build a JSF development environment with NetBeans
Rails development environment created with VSCode and devcontainer
Prepare a scraping environment with Docker and Java
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Build mecab (NEologd dictionary) environment with Docker (ubuntu)
[Rails] How to build an environment with Docker
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
[First team development ②] Build an environment with Docker
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Create a Spring Boot development environment with docker
Build a Java development environment with VS Code
Build a development environment to create Ruby on Jets + React apps with Docker
A simple CRUD app made with Nuxt / Laravel (Docker)
How to build docker environment with Gradle for intelliJ
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
Build Docker + Laravel PHP + Vue.js development environment in 5 minutes
[Note] Create a java environment from scratch with docker
Build Java development environment with WSL2 Docker VS Code
Build Rails (API) x MySQL x Nuxt.js environment with Docker
[Docker] Build a Wordpress environment locally (Win, Mac) quickly