Prerequisites
- A system with Docker support (Linux, macOS, or Windows).
- Basic knowledge of Laravel and Docker.
Step 1: Install Docker
For Linux
- Update your system packages:
sudo apt-get update
- Install Docker:
sudo apt-get install -y docker.io
- Start and enable Docker:
sudo systemctl start docker
sudo systemctl enable docker
For macOS and Windows
Download and install Docker Desktop from Docker's official site.
Step 2: Set Up the Laravel Project
Clone the Project Repository
git clone <your-repo-url>
cd <project-name>
Install Dependencies
composer install
Prepare the Environment
- Create a
.env
file:
cp .env.example .env
- Generate the application key:
php artisan key:generate
Update the Dockerfile
and docker-compose.yml
Example Dockerfile
FROM php:8.2-apache
WORKDIR /var/www/html
COPY . .
RUN docker-php-ext-install pdo pdo_mysql
Example docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "8080:80"
volumes:
- .:/var/www/html
environment:
- APACHE_LOG_DIR=/var/log/apache2
depends_on:
- db
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
Build and Start Docker Containers
docker-compose build
docker-compose up -d
Set Permissions
chmod -R 775 bootstrap/cache storage
chown -R www-data:www-data bootstrap/cache storage
chmod 775 database
chown -R www-data:www-data database
chmod 664 database/database.sqlite
chown www-data:www-data database/database.sqlite
Migrate the Database
php artisan migrate
Step 3: Access the Application
Once the containers are running, access your Laravel application at:
http://localhost:8080
Step 4: Reference
Find the complete project code on GitHub:
GitHub Repository Link
Step 5: Most Used Docker Commands
Basic Commands
-
docker --version
: Check Docker version. -
docker ps
: List running containers. -
docker ps -a
: List all containers. -
docker images
: List all Docker images. -
docker pull <image>
: Pull an image from Docker Hub. -
docker build -t <name> .
: Build an image from a Dockerfile. -
docker run -d -p <host-port>:<container-port> <image>
: Run a container. -
docker exec -it <container-id> bash
: Access a container's shell. -
docker stop <container-id>
: Stop a running container. -
docker rm <container-id>
: Remove a stopped container.
Advanced Commands
-
docker logs <container-id>
: View container logs. -
docker-compose up
: Start services defined indocker-compose.yml
. -
docker-compose down
: Stop and remove services. -
docker volume ls
: List volumes. -
docker network ls
: List networks. -
docker inspect <container-id>
: Inspect container details. -
docker system prune
: Clean up unused containers, images, and volumes. -
docker cp <src> <container>:<dest>
: Copy files to a container. -
docker stats
: View real-time resource usage. -
docker tag <image-id> <new-name>
: Tag an image.
Refer to the Docker Documentation for more commands and usage examples.
By following these steps, you’ll have a Laravel application running in Docker in no time!
Top comments (0)