# Docker for your Laravel app

With Docker, your app runs in **containers**: isolated mini-environments with exactly the right version of PHP, database and extras. If it works for you, it will also work for your colleague and on the server. You don’t need to install PHP, MySQL and Redis on your computer yourself.

A few words you’ll often come across:

- **Image:** the build configuration (for example, `php:8.3-fpm` or `mysql:8.4`).
- **Container:** a running copy of an image.
- **Volume:** storage that persists even after the container has stopped. This is where your database is stored, for example.
- **Compose:** a single file (`compose.yaml`) that describes all your containers together.

## Step 1: Install Docker

- **Windows and macOS:** install **Docker Desktop** from docker.com. On Windows, enable **WSL 2** during installation.
- **Linux:** Install **Docker Engine** and the Compose plugin via your distribution’s package manager (follow the official installation guide for your distribution).

Then check in the terminal:

```bash
docker --version
docker compose version
```

## Step 2 (the easiest route): Laravel Sail

Laravel has its own Docker setup: **Sail**. From your project directory:

```bash
composer require laravel/sail --dev
php artisan sail:install      # kies bijvoorbeeld mysql en redis
./vendor/bin/sail up -d       # start alles op de achtergrond
```

Your app is now running on `http://localhost`.. You can execute commands via Sail:

```bash
./vendor/bin/sail artisan migrate
./vendor/bin/sail composer install
./vendor/bin/sail npm run dev
./vendor/bin/sail down        # stoppen
```

Tip: create an alias called `sail` so you don’t have to keep typing `./vendor/bin/`.

## Step 2 (alternative): your own compose.yaml

If you want to decide for yourself what plays, a simple setup would look like this:

```yaml
services:
  app:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - .:/var/www/html
    env_file: .env
    depends_on:
      db:
        condition: service_healthy
  db:
    image: mysql:8.4
    environment:
      MYSQL_DATABASE: app
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - dbdata:/var/lib/mysql
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
      interval: 10s
      retries: 5
volumes:
  dbdata:
```

Please note: in `.env`, set `DB_HOST=db` (the name of the service), not `127.0.0.1`. Containers locate each other by name.

## The commands you really need

| Command | What it does |
|---|---|
| `docker compose up -d` | Start everything (`-d` = in the background) |
| `docker compose ps` | Which containers are running? |
| `docker compose logs -f app` | Live logs from the app (`-f` = follow) |
| `docker compose exec app php artisan migrate` | Running a command inside the container |
| `docker compose exec -it app bash` | Open a shell in the container |
| `docker compose down` | Stop everything (data is retained) |
| `docker compose build --no-cache` | Rebuild after a change to the Dockerfile |

## Be careful with these commands

- `docker compose down -v` also deletes your **volumes**, including your database.
- `docker system prune -a --volumes` clears everything, including data.

Only use them if you are certain you no longer need the data. Do not let your AI assistant run them without asking first. The **docker-destructive-guardrails** skill can help with this.

## Common problems

- **"port is already allocated":
- **“Connection refused” to the database:** use `DB_HOST=db` and wait until the database is healthy (health check).
- **Files have the wrong owner (Linux):** Run commands via Sail or as your own user (`-u`).
- **Slow on Windows:** Place your project in the WSL file system (`\\wsl$\...`), not on `C:\`.

## Need help?

Would you like to have your Laravel app professionally set up in Docker, including a production-ready configuration? This falls under **Custom software and web applications**.

## Skills

- [Docker Project Foundations] (/nl/skills/docker-project-foundations)
- [Docker Compose Patterns] (/nl/skills/docker-compose-patterns)
- [Docker Build Strategies] (/nl/skills/docker-build-strategies)
- [Multi Stage Dockerfile] (/nl/skills/multi-stage-dockerfile)
- [Docker Destructive Command Guardrails] (/nl/skills/docker-destructive-guardrails)

## Related services

- [Maatwerk software en webapplicaties] (/nl/services#service-03)

