Skip to content

Instantly share code, notes, and snippets.

@WalterWoshid
Last active February 17, 2023 14:51
Show Gist options
  • Save WalterWoshid/31cd1fcb5e158d3ab2b74639338bf052 to your computer and use it in GitHub Desktop.
Save WalterWoshid/31cd1fcb5e158d3ab2b74639338bf052 to your computer and use it in GitHub Desktop.
Magento 2 Docker Scripts (PowerShell)

Magento 2 Docker Scripts (PowerShell)

Twitter: @WalterWoshid

Installation

  • Open a PowerShell Terminal and type: notepad $profile
  • Paste the content of this gist to the bottom of the profile

Usage

Open a new PowerShell window and enter magento. It will list all the available commands

image image

Set-Alias -Name magento -Value Run-magento
Function Run-magento {
# Help Menu for no arguments
if ($args.Count -eq 0) {
Help-menu
}
$lastArgs = $args[1..$args.length]
# If arguments
switch ($args[0]) {
# Help (Magneto)
{"-h", "--help", "help", "list" -eq $_} {
docker-compose exec phpfpm /bin/sh -c "php bin/magento '$lastArgs'"
Break Script
}
# Start container
{"start", "up" -eq $_} {
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up $lastArgs
Break Script
}
# Stop container
{"stop", "down" -eq $_} {
$container = docker ps -q
if ($null -eq $container)
{
Write-Host "No containers running" -ForegroundColor Green
}
else
{
docker kill $container
Write-Host "Containers stopped" -ForegroundColor Green
}
Break Script
}
# Bash
{"bash", "shell", "sh" -eq $_} {
bin/bash.ps1
Break Script
}
# Bash Nginx
{"bash-nginx", "shell-nginx", "sh-nginx" -eq $_} {
docker-compose exec phpfpm bash
Break Script
}
# Bash Root
{"bash-root", "shell-root", "sh-root" -eq $_} {
docker-compose exec app /bin/sh
Break Script
}
# Bash Root Nginx
{"bash-root-nginx", "shell-root-nginx", "sh-root-nginx" -eq $_} {
docker-compose exec -u=root app /bin/sh
Break Script
}
# CLI
{"cli" -eq $_ } {
docker-compose exec phpfpm /bin/sh -c "$lastArgs"
Break Script
}
# CLI Nginx
{"cli-nginx" -eq $_ } {
docker-compose exec app /bin/sh -c "$lastArgs"
Break Script
}
# CLI Root
{"cli-root" -eq $_ } {
docker-compose exec -u root phpfpm /bin/sh -c "$lastArgs"
Break Script
}
# CLI Root Nginx
{"cli-root-nginx" -eq $_ } {
docker-compose exec -u root app /bin/sh -c "$lastArgs"
Break Script
}
# Composer
{"composer" -eq $_ } {
docker-compose exec phpfpm /bin/sh -c "composer $lastArgs"
Break Script
}
# PHP
{"php" -eq $_} {
docker-compose exec phpfpm /bin/sh -c "php $lastArgs"
Break Script
}
# Ownerships
{"fixowns" -eq $_} {
Write-Host "Correcting filesystem ownerships..." -ForegroundColor Green
docker-compose exec -u root -T phpfpm chown -R app:app /var/www/
Write-Host "Filesystem ownerships corrected." -ForegroundColor Green
Break Script
}
# Permissions
{"fixperms" -eq $_} {
$verbose = Write-Host "Verbose output? (y/N): " -ForegroundColor Yellow -NoNewLine; Read-Host
if ($verbose -eq "y") {
$verbose = "-v"
} else {
$verbose = ""
}
Write-Host "Correcting filesystem permissions..." -ForegroundColor Green
docker-compose exec -u root phpfpm /bin/sh -c "chown $verbose -R app:app /var/www"
docker-compose exec -u root phpfpm /bin/sh -c "find var vendor pub/static pub/media app/etc -type f -exec chmod $verbose u+w `{`} `+"
docker-compose exec -u root phpfpm /bin/sh -c "find var vendor pub/static pub/media app/etc -type d -exec chmod $verbose u+w `{`} `+"
docker-compose exec -u root phpfpm /bin/sh -c "chmod $verbose u+x bin/magento"
Write-Host "Filesystem permissions corrected." -ForegroundColor Green
Break Script
}
# Xdebug
{"xdebug" -eq $_ } {
Xdebug-Toggle $lastArgs
Break Script
}
{"xdebug-enable" -eq $_} {
Xdebug-Enable
Break Script
}
{"xdebug-disable" -eq $_} {
Xdebug-Disable
Break Script
}
{"clear-all" -eq $_} {
docker-compose exec phpfpm /bin/sh -c "rm -rf generated/code pub/static/frontend pub/static/adminhtml var/view_preprocessed"
Write-Host "Cleared generated/code pub/static/frontend pub/static/adminhtml var/view_preprocessed" -ForegroundColor Green
Break Script
}
{"clear-generated" -eq $_} {
docker-compose exec phpfpm /bin/sh -c "rm -rf generated/code"
Write-Host "Cleared generated/code" -ForegroundColor Green
Break Script
}
{"clear-static" -eq $_} {
docker-compose exec phpfpm /bin/sh -c "rm -rf pub/static/frontend pub/static/adminhtml"
Write-Host "Cleared pub/static/frontend pub/static/adminhtml" -ForegroundColor Green
Break Script
}
{"clear-var" -eq $_} {
docker-compose exec phpfpm /bin/sh -c "rm -rf var/view_preprocessed"
Write-Host "Cleared var/view_preprocessed" -ForegroundColor Green
Break Script
}
default {
docker-compose exec phpfpm /bin/sh -c "bin/magento $args"
Break Script
}
}
}
Function Xdebug-Toggle {
if ( "$lastArgs" -eq "enable" ) {
Xdebug-Enable
} elseif ( "$lastArgs" -eq "disable" ) {
Xdebug-Disable
} else {
Write-Host "Please specify either 'enable' or 'disable' as an argument" -ForegroundColor Red
}
}
Function Xdebug-Enable {
# Enable xdebug mode
docker-compose exec -u root phpfpm /bin/sh -c "sed -i -e 's/^xdebug.mode = off/xdebug.mode = debug/g' /usr/local/etc/php/php.ini"
# Add xdebug extension to php
docker-compose exec -u root phpfpm /bin/sh -c "sed -i -e 's/^\;zend_extension/zend_extension/g' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini"
# Add "server_name localhost;" to nginx config
docker-compose --% exec -u root app /bin/sh -c "echo \"$(awk '/server_name localhost;/{found=1; next}; /listen 8443 ssl http2;/{print $0 \"\n server_name localhost;\"; next}; {print}' /etc/nginx/conf.d/default.conf)\" > /etc/nginx/conf.d/default.conf"
#docker-compose restart phpfpm
Write-Host "Xdebug debug mode has been enabled." -ForegroundColor Green
}
Function Xdebug-Disable {
# Disable xdebug mode
docker-compose exec -u root phpfpm /bin/sh -c "sed -i -e 's/^xdebug.mode = debug/xdebug.mode = off/g' /usr/local/etc/php/php.ini"
# Remove xdebug extension from php
docker-compose exec -u root phpfpm /bin/sh -c "sed -i -e 's/^zend_extension/\;zend_extension/g' /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini"
docker-compose restart phpfpm
Write-Host "Xdebug debug mode has been disabled." -ForegroundColor Green
}
Function Help-menu {
Write-Host "Magento 2 Docker Script " -NoNewline
Write-Host "1.0.0" -ForegroundColor Green
Write-Host
Write-Host "Runs Docker commands for Magento 2."
Write-Host "Unknown commands are passed to the bin/magento file."
Write-Host
Write-Host "Usage:" -ForegroundColor Yellow
Write-Host " magento [command] [arguments]"
Write-Host
# ------------------------------------------------------------------- #
Write-Host "Magento Commands:" -ForegroundColor Yellow
Write-Host " -h, --help, list " -ForegroundColor Green -NoNewline
Write-Host "Display Magento help message"
Write-Host " [command] " -ForegroundColor Cyan -NoNewline
Write-Host "Every unknown command is passed to bin/magento in the container"
Write-Host " cache:clean " -ForegroundColor Green -NoNewline
Write-Host "Clean Magento cache"
Write-Host " cache:status " -ForegroundColor Green -NoNewline
Write-Host "Checks Magento cache status"
Write-Host " clear-generated " -ForegroundColor Green -NoNewline
Write-Host "Clear generated code"
Write-Host " clear-static " -ForegroundColor Green -NoNewline
Write-Host "Clear static content "
Write-Host " clear-var " -ForegroundColor Green -NoNewline
Write-Host "Clear var/view_preprocessed"
Write-Host " clear-all " -ForegroundColor Green -NoNewline
Write-Host "Clear all of the above"
Write-Host " setup:upgrade " -ForegroundColor Green -NoNewline
Write-Host "Upgrades the Magento application, DB data, and schema"
Write-Host " deploy:mode:set " -ForegroundColor Green -NoNewline
Write-Host "Set Magento application mode"
Write-Host " indexer:reindex " -ForegroundColor Green -NoNewline
Write-Host "Reindexes Magento Data"
Write-Host " maintenance:enable " -ForegroundColor Green -NoNewline
Write-Host "Enables Magento maintenance mode"
Write-Host " maintenance:disable " -ForegroundColor Green -NoNewline
Write-Host "Disables Magento maintenance mode"
Write-Host " maintenance:status " -ForegroundColor Green -NoNewline
Write-Host "Displays Magento maintenance mode status"
Write-Host " module:enable " -ForegroundColor Green -NoNewline
Write-Host "Enables specificed Magento module"
Write-Host " module:disable " -ForegroundColor Green -NoNewline
Write-Host "Disables specified Magento module"
Write-Host " module:status " -ForegroundColor Green -NoNewline
Write-Host "Displays status of Magento modules"
Write-Host
# ------------------------------------------------------------------- #
Write-Host "Docker Commands:" -ForegroundColor Yellow
Write-Host " start " -ForegroundColor Green -NoNewline
Write-Host "Start the container"
Write-Host " start -d " -ForegroundColor Green -NoNewline
Write-Host "Start the container in the background"
Write-Host " stop " -ForegroundColor Green -NoNewline
Write-Host "Stop all containers"
Write-Host " bash " -ForegroundColor Green -NoNewline
Write-Host "Log into project bash in Docker container"
Write-Host " bash-nginx " -ForegroundColor Green -NoNewline
Write-Host "Log into nginx bash in Docker container"
Write-Host " bash-root " -ForegroundColor Green -NoNewline
Write-Host "Log into root project bash in Docker container"
Write-Host " bash-root-nginx " -ForegroundColor Green -NoNewline
Write-Host "Log into root nginx bash in Docker container"
Write-Host " cli " -ForegroundColor Green -NoNewline
Write-Host "[command] " -ForegroundColor Cyan -NoNewline
Write-Host "Run a command in the project container"
Write-Host " cli-nginx " -ForegroundColor Green -NoNewline
Write-Host "[command] " -ForegroundColor Cyan -NoNewline
Write-Host "Run a command in the nginx container"
Write-Host " cli-root " -ForegroundColor Green -NoNewline
Write-Host "[command] " -ForegroundColor Cyan -NoNewline
Write-Host "Run a command in the root project container"
Write-Host " cli-root-nginx " -ForegroundColor Green -NoNewline
Write-Host "[command] " -ForegroundColor Cyan -NoNewline
Write-Host "Run a command in the nginx root container"
Write-Host
# ------------------------------------------------------------------- #
Write-Host "Composer Commands:" -ForegroundColor Yellow
Write-Host " composer " -ForegroundColor Green -NoNewline
Write-Host "[command] " -ForegroundColor Cyan -NoNewline
Write-Host "Run a composer command in the project container"
Write-Host
# ------------------------------------------------------------------- #
Write-Host "PHP Commands:" -ForegroundColor Yellow
Write-Host " php " -ForegroundColor Green -NoNewline
Write-Host "[command] " -ForegroundColor Cyan -NoNewline
Write-Host "Run a PHP command in the project container"
Write-Host
# ------------------------------------------------------------------- #
Write-Host "Permission Commands:" -ForegroundColor Yellow
Write-Host " fixowns " -ForegroundColor Green -NoNewline
Write-Host "Fix ownerships inside container"
Write-Host " fixperms " -ForegroundColor Green -NoNewline
Write-Host "Fix permissions inside container"
Write-Host
# ------------------------------------------------------------------- #
Write-Host "Xdebug Commands:" -ForegroundColor Yellow
Write-Host " xdebug " -ForegroundColor Green -NoNewline
Write-Host "Toggle xdebug on/off"
Write-Host " xdebug-enable " -ForegroundColor Green -NoNewline
Write-Host "Enable xdebug"
Write-Host " xdebug-disable " -ForegroundColor Green -NoNewline
Write-Host "Disable xdebug"
Break Script
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment