Skip to content

Instantly share code, notes, and snippets.

@devinci-it
Last active June 2, 2024 02:32
Show Gist options
  • Select an option

  • Save devinci-it/eaf93ed4982ee6dc4055245cd3d8ff00 to your computer and use it in GitHub Desktop.

Select an option

Save devinci-it/eaf93ed4982ee6dc4055245cd3d8ff00 to your computer and use it in GitHub Desktop.

Quick Setup: Laravel with Docker

Step 1: Create Laravel Project with Docker

To set up a new Laravel project with Docker, you can use the following script. This will create a new Laravel project, set up Docker with a Dockerfile and docker-compose.yaml, and configure the .env file with MySQL settings.

Prerequisites

Make sure you have composer, docker, and docker-compose installed on your system.

Quick Setup Script

You can set up your Laravel project quickly by running this command:

curl -sSL https://gist.github.com/devinci-it/eaf93ed4982ee6dc4055245cd3d8ff00/raw/setup.sh | bash -s

Step 2: Follow the Prompts

Follow the prompts to enter your Laravel application name when prompted.

Step 3: Access Your Application

Once the setup completes, you can access your Laravel application at http://localhost:8000.

Additional Information

  • SSH into the app container:

    docker-compose exec app bash
  • MySQL Database:

    • Database Name: Your application name
    • Username: ${APP_NAME}user
    • Password: ${APP_NAME}password

Notes:

  • Make sure to review and customize the Dockerfile and docker-compose.yaml files as per your project's requirements.
  • Ensure your system meets the prerequisites before running the script.

Troubleshooting

If you encounter any issues during the setup, make sure Docker and its dependencies are properly installed and configured on your system. If you need further assistance, refer to the Docker documentation or Laravel documentation.


#!/bin/bash
# Function to fetch gist content
function fetch_gist_file {
local gist_id="$1"
local file_name="$2"
local url="https://gist.github.com/${gist_id}.git"
curl -sSL "${url}/raw/${file_name}" -o "${file_name}"
}
# Prompt for the application name
read -p "Enter your Laravel application name: " APP_NAME
# Create Laravel project using Composer
composer create-project --prefer-dist laravel/laravel $APP_NAME
# Change directory to the project
cd $APP_NAME
# Fetch Dockerfile and docker-compose.yaml from Gist
DOCKERFILE_GIST_ID="e08a618f6fd92e8aa65cc68fddfdffcd"
COMPOSE_GIST_ID="e08a618f6fd92e8aa65cc68fddfdffcd"
fetch_gist_file "${DOCKERFILE_GIST_ID}" "Dockerfile"
fetch_gist_file "${COMPOSE_GIST_ID}" "docker-compose.yaml"
# Update the .env file with MySQL settings
DB_NAME="${APP_NAME}"
DB_USER="${APP_NAME}user"
DB_PASSWORD="${APP_NAME}password"
sed -i "s/DB_DATABASE=.*/DB_DATABASE=${DB_NAME}/" .env
sed -i "s/DB_USERNAME=.*/DB_USERNAME=${DB_USER}/" .env
sed -i "s/DB_PASSWORD=.*/DB_PASSWORD=${DB_PASSWORD}/" .env
# Verify dependencies and run Docker setup
command -v docker >/dev/null 2>&1 || { echo >&2 "Docker is required but it's not installed. Aborting."; exit 1; }
command -v docker-compose >/dev/null 2>&1 || { echo >&2 "docker-compose is required but it's not installed. Aborting."; exit 1; }
# Build Docker image and run containers
docker-compose up -d --build
# Provide summary
echo ""
echo "Laravel application '${APP_NAME}' is now running with Docker:"
echo "- Access the application: http://localhost:8000"
echo "- SSH into the app container: docker-compose exec app bash"
echo "- MySQL Database: ${DB_NAME}"
echo "- MySQL Username: ${DB_USER}"
echo "- MySQL Password: ${DB_PASSWORD}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment