Laravel Development with Docker and VS Code

Reference

1. PHP7.4-FPM Container(with Composer)

1.1 Dockerfile

FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# change sources list of update to local repository
RUN rm -rf /etc/apt/sources.list.d/buster.list
COPY sources.list /etc/apt/sources.list
RUN apt-get update

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

1.2 sources.list

For example, Aliyun in for Chinese user

deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

2. Docker compose

  • app: PHP7.4-FPM
  • MySQL 5.7
  • nginx service
version: "3.7"
services:
  app:
    build:
      args:
        user: laradev
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: wylaradev
    container_name: wylaradev-app
    restart: unless-stopped
    working_dir: /var/www/
    volumes:
      - ./:/var/www
    networks:
      - wylaradev

  db:
    image: mysql:5.7
    container_name: wylaradev-db
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_USER: ${DB_USERNAME}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./docker-compose/mysql:/docker-entrypoint-initdb.d
    networks:
      - wylaradev

  nginx:
    image: nginx:alpine
    container_name: wylaradev-nginx
    restart: unless-stopped
    ports:
      - 8000:80
    volumes:
      - ./:/var/www
      - ./docker-compose/nginx:/etc/nginx/conf.d/
    networks:
      - wylaradev

networks:
  wylaradev:
    driver: bridge

2.1 nginx configuration (app.conf)

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

3. Command (Composer or Artisan)

  • build app (PHP7.4-FPM)
    docker-compose build app
  • boot up all three containers
    docker-compose up -d
  • check condition of the containers
    docker-compose ps
  • check file in the app container
    docker-compose exec app ls -l
  • check nginx log
    docker-compose logs nginx
  • pause
    docker-compose pause
  • unpause
    docker-compose unpause
  • down
    docker-compose down

3.1 Composer

  • composer install
    docker-compose exec app composer install

3.2 Artisan

  • artisan key:generate
    docker-compose exec app php artisan key:generate

4. Js


5. VS Code

Attach to a running container