Build an environment to work with Laravel using Docker

Sebastián Vidal Aedo
3 min readMay 15, 2023

--

Laravel is widely recognized as the best PHP framework and in this post I will show you how you can set up a development environment for Laravel projects using Docker Compose and PostgreSQL. The main idea is to use Docker to provide us with a database, a web server and all the necessary dependencies, thus reducing compatibility and local versioning issues. With this approach, you will be able to quickly set up a consistent development environment ready to start developing your Laravel project without worrying about the usual complications.

Create the directory and install Laravel

As in any project, the first thing we need to do is to create a base directory. In this case, we will call it “laravel_project”. Once inside the project directory, we will use Composer to download and install Laravel. Composer is an essential tool in the Laravel ecosystem, as it allows us to manage dependencies easily and efficiently. Next, I will show you the necessary steps to perform this installation.

mkdir laravel_project
cd laravel_project/
composer create-project --prefer-dist laravel/laravel .

Docker

Using Docker, and more specifically Docker Compose, we will configure and set up our development environment, which will include the web server and database needed for our Laravel project. Below, I will show you a configuration file:

version: '3.8'
services:
app:
image: php:8.0-apache
container_name: laravel-app
volumes:
- ./laravel-app:/var/www/html
ports:
- 8000:8000
networks:
- laravel
depends_on:
- db

db:
image: postgres:13
container_name: postgres-db
restart: always
environment:
POSTGRES_DB: laravel
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
volumes:
- db_data:/var/lib/postgresql/data
networks:
- laravel

volumes:
db_data:

networks:
laravel:

With this we raise 2 containers that see each other (they are in the same container-level network called laravel). One for the application (laravel-app) which links the current directory to a directory on a server in the internal path of the container in /var/www/html and which exposes its content on port 8080. The other container that is built is the database (postgres-db), which includes the setting of its access variables such as user, pass and database name.

Last steps before we start programming

Before we dive into programming, there are a couple of additional tasks we need to complete. First, we need to generate the Laravel key by running the following command.

php artisan key:generate

In addition, we must complete the .env file with the necessary variables to access the database.

APP_NAME=Laravel
APP_ENV=local
APP_KEY=the_key_generated
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel
DB_USERNAME=postgres
DB_PASSWORD=postgres

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

Once this is done, we just need to create and launch the containers

docker compose up -d

and then start our project

php artisan serve

If we access 127.0.0.1:8080, we will be ready to start programming.

--

--

No responses yet