Docker Laravel Dev Environment

Docker Compose

Create a docker-compose yaml file to define the services (web, app, db) and the mysql volume. As stated in the Docker Compose file reference, The service definition contains configuration which will be applied to each container started for that service, much like passing command-line parameters to docker run.

docker-compose.yml
version: '2'

services:
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    volumes:
      - ./:/var/www
    ports:
      - "8080:80"
    links:
      - app
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    volumes:
      - ./:/var/www
    links:
      - db
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=db"
  db:
    image: mysql:5.6
    volumes:
      - mysql:/var/lib/mysql
    environment:
      - "MYSQL_DATABASE=homestead"
      - "MYSQL_USER=homestead"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_ROOT_PASSWORD=secret"
    ports:
      - "33061:3306"
volumes:
  mysql:

Laravel .env

Update the Laravel project .env file as needed. In particular, the database port and app URL. In our docker-compose file, the database service (db) container port 3306 is mapped to 33061 on the host computer. The web server is mapped to port 8080 on the host computer.

.env
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost:8080

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33061
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

Build/Start Containers

Run docker-compose from the project root. The first time this is run, images are downloaded and containers are built from the definitions in the app and web dockerfiles we created earlier.

docker-compose up -d

-d (detached mode) starts the containers in the background and leaves them running. For more info, visit the docker-compose up docs.

Shutdown containers and services defined in the docker-compose file with the down command.

docker-compose down

For more info, visit the docker-compose down docs.

Remote Debugging with Xdebug and VS Code

VS Code has extensions available for debugging runtimes, add an adapter for Xdebug such as vscode-php-debug to allow PHP debugging. For more info, visit the VS Code Debugging page.

After installing vscode-php-debug, the VS Code project launch configuration file, launch.json, needs to be updated to include the properties to map the files on the server to the local machine. Add the serverSourceRoot and localSourceRoot settings to the home/laravel/mysite/.vscode/launch.json file. For example,

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9001,
            "serverSourceRoot": "/var/www",
            "localSourceRoot": "${workspaceRoot}",
            "log": true
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9001
        }
    ]
}
Source Code

Resources

Published by

Jim Frenette

Web Developer – views here are my own except those taken from people more clever than me.

Loading Disqus Comments ...
Loading Facebook Comments ...