Skip to content

Commit

Permalink
Merge pull request #50 from glo47154/feature/single-dockerfile
Browse files Browse the repository at this point in the history
- Improved and fixed help for m2d command;
- Fixed issue with a configuration file path when it is not yet created;
- Exclude .env.back file from the repository;
- Added project suffix;
- Fixed mailcatcher configuration;
- Project clean-up;
  • Loading branch information
glo47154 authored Mar 31, 2023
2 parents 854520a + 952206b commit 73975e5
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
# Set the base path were you want to keep Magento source files
# e.g. /var/www/html or ~/src/html
M2D_SOURCE_DIRECTORY='/var/www/html'
# Set the project suffix. It will be used to distinguish different instances of Magento2Docker
# If you only work with one instance, you can skip it.
M2D_PROJECT_SUFFIX=''
# Define a synchronisation type used to sync source directory between host and container
# - 'mutagen' container will use named volume and Mutagen [https://mutagen.io/] will be used to sync files (default)
# - 'sshfs' container will use named volume and SSHFS will be used to mount it to the host
Expand Down Expand Up @@ -60,6 +63,8 @@ M2D_WEB_SERVER_VENDOR='apache'
# Set which PHP version you want to use:
# - PHP versions: 7.3, 7.4, 8.1, 8.2
M2D_WEB_SERVER_PHP_VERSION='8.1'
# Sets amount of memory available for PHP (php.ini: memory_limit)
M2D_WEB_SERVER_PHP_MEMORY_LIMIT='2G'
# Set the port number used by SSH:
M2D_PORT_FOR_WEB_SERVER_SSH='2222'
# Set the port number used by HTTP:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env.back
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Key features of the project:
- Single docker-compose.yaml file approach with .env file to configure everything.
## Contents
- [Pre-requirements](#pre-requirements)
- [How to install it on Mac](#how-to-install-it-on-mac)
- [Directory requirements](#directory-requirements)
- [Installation](#installation)
- [Supported services and tools](#supported-services-and-tools)
- [Usage](#usage)
Expand All @@ -32,7 +34,24 @@ Key features of the project:
## Pre-requirements
- [Install Docker](https://docs.docker.com/engine/installation/mac/)
- [Install Mutagen](https://mutagen.io/documentation/introduction/installation/)
- [Install bash completion (optional)](https://github.com/bobthecow/git-flow-completion/wiki/Install-Bash-git-completion)
- Bash > 4.0
- Realpath

### How to install it on Mac
```bash
# Mutagen:
brew install mutagen-io/mutagen/mutagen
# Bash:
brew install bash
# Realpath:
brew install coreutils
```

### Directory requirements
1. Magento2Docker as a tool can be located in any directory, e.g. ~/tools/m2d
2. Web container will use /var/www/html as its root folder for web content.
3. The M2D_SOURCE_DIRECTORY must point to a local path where you want to sync /var/www/html from the web container.
4. The M2D_SOURCE_DIRECTORY must not point to symlink or it will trigger errors when Mutagen is responsible for sync process.

## Installation
You can download archive of this project on [Release Page](https://github.com/yvoronoy/magento2docker/releases).
Expand Down
93 changes: 74 additions & 19 deletions bin/m2d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ function _m2d_display_help_main ()
echo "
Usage: ./bin/m2d COMMAND [OPTIONS]
Examples:
- initial setup:
./bin/m2d setup init
- build and start containers:
./bin/m2d up --build
- stop and remove containers and networks:
./bin/m2d down
Options:
-h, --help Disply help for command
Expand All @@ -14,6 +25,7 @@ Commands:
disable Disable selected service or tool
down Stop and remove containers and networks
enable Enable selected service or tool
go Enters a running container
logs View logs from containers
set Sets configuration value
setup Configure project
Expand All @@ -29,11 +41,20 @@ function _m2d_display_help_set ()
echo "
Usage: ./bin/m2d set [OPTIONS] PARAMETER VALUE
Sets configuration value.
Examples:
./bin/m2d set M2D_XDEBUG_IDE_KEY VSCODE
./bin/m2d set es 8.4
Options:
-h, --help Disply help for command
Parameters:
es Alias for setting Elasticsearch as the search engine. It will set M2D_SEARCH_ENGINE_VENDOR to 'elasticsearch',
and M2D_SEARCH_ENGINE_VERSION to '7' if expected version is not provided as a value
php Alias for parameter M2D_WEB_SERVER_PHP_VERSION
suffix Alias for parameter M2D_PROJECT_SUFFIX
[M2D_*] Parameter name. List of possible parameters and their values is in .env.example
"
}
Expand All @@ -43,6 +64,11 @@ function _m2d_display_help_show ()
echo "
Usage: ./bin/m2d show [OPTIONS] PARAMETER
Shows configuration value.
Example:
./bin/m2d show M2D_SOURCE_DIRECTORY
Options:
-h, --help Disply help for command
Expand All @@ -56,6 +82,11 @@ function _m2d_display_help_setup ()
echo "
Usage: ./bin/m2d setup [OPTIONS] [COMMAND] [PROJECT_ID]
Configure project.
Example:
./bin/m2d setup init
Options:
-h, --help Disply help for command
Expand All @@ -68,27 +99,35 @@ Commands:

function _m2d_display_help_enable_disable ()
{
local command=$1
local Command=${1^}

echo "
Usage: ./bin/m2d $1 [OPTIONS] COMMAND
Usage: ./bin/m2d $command [OPTIONS] COMMAND
$Command selected service or tool.
Example:
./bin/m2d $command mailcatcher
Options:
-h, --help Disply help for command
Commands:
blackfire ${1^} blackfire container
db-cache ${1^} db-cache container
blackfire $Command blackfire container
db-cache $Command db-cache container
elastic Alias for 'search-engine ' command
m Alias for 'mailcatcher' command
mailcatcher ${1^} mailcatcher container
message-broker ${1^} message broker container
mailcatcher $Command mailcatcher container
message-broker $Command message broker container
rabbit Alias for 'message-broker' command
redis Alias for 'db-cache' command
search-engine ${1^} search engine container
selenium ${1^} selenium container
search-engine $Command search engine container
selenium $Command selenium container
varnish Alias for 'web-cache' command
web-cache ${1^} web-cache container
web-cache $Command web-cache container
After container is $1 it must be rebuild with: \`./bin/m2d build\` or \`./bin/m2d up --build\`
After container is $command it must be rebuild with: \`./bin/m2d build\` or \`./bin/m2d up --build\`
"
}

Expand All @@ -97,6 +136,8 @@ function _m2d_display_help_up ()
echo "
Usage: ./bin/m2d up [OPTIONS]
Create and start containers.
Options:
-h, --help Disply help for command
-b, --build Build images before starting containers.
Expand All @@ -118,6 +159,11 @@ function _m2d_display_help_go ()
echo "
Usage: ./bin/m2d go [OPTIONS] CONTAINER_NAME
Enters a running container.
Example:
./bin/m2d go web
Options:
-h, --help Disply help for command
Expand All @@ -134,6 +180,8 @@ function _m2d_display_help_logs ()
echo "
Usage: ./bin/m2d logs [OPTIONS] CONTAINER_NAME
View logs from containers.
Options:
-h, --help Disply help for command
Expand All @@ -147,6 +195,8 @@ function _m2d_display_help_sync ()
echo "
Usage: ./bin/m2d sync [OPTIONS] ACTION
Manage data sync from between host an containers.
Options:
-h, --help Disply help for command
Expand Down Expand Up @@ -292,14 +342,14 @@ function _m2d_sync ()
local sync_type=$(_m2d_env_get_parameter "$env_file" M2D_SOURCE_DIRECTORY_SYNC_TYPE)
local sync_dir=$(_m2d_env_get_parameter "$env_file" M2D_SOURCE_DIRECTORY)
local server_vendor=$(_m2d_env_get_parameter "$env_file" M2D_WEB_SERVER_VENDOR)
local ssh_port=$(_m2d_env_get_parameter "$env_file" M2D_PORT_FOR_WEB_SERVER_SSH)
local project_path="$3"

case $sync_type in
'bind')
echo "Nothing to do for '$action' in '$sync_type' sync type"
;;
'sshfs')
local ssh_port=$(_m2d_env_get_parameter "$env_file" M2D_PORT_FOR_WEB_SERVER_SSH)
case $action in
'start')
local identity_file="$project_path/services/web-servers/$server_vendor/etc/ssh/magento2docker"
Expand All @@ -321,12 +371,14 @@ function _m2d_sync ()
esac
;;
'mutagen')
local sync_name="m2d-sync$(_m2d_env_get_parameter $env_file M2D_PROJECT_SUFFIX)"
local sync_web="m2d-web$(_m2d_env_get_parameter $env_file M2D_PROJECT_SUFFIX)"
case $action in
'start')
if [[ -n $(mutagen sync list | grep m2d-sync) ]]; then
mutagen sync resume m2d-sync
if [[ -n $(mutagen sync list | grep $sync_name) ]]; then
mutagen sync resume $sync_name
else
mutagen sync create --name=magento2web \
mutagen sync create --name=$sync_name \
--default-group-beta=magento \
--default-owner-beta=magento \
--sync-mode=two-way-resolved \
Expand All @@ -344,17 +396,17 @@ function _m2d_sync ()
--ignore=/**/.DS_Store \
--symlink-mode=posix-raw \
"$sync_dir" \
docker://magento@m2d-web/var/www/html/
"docker://magento@$sync_web/var/www/html/"
fi
;;
'pause')
mutagen sync pause m2d-sync
mutagen sync pause $sync_name
;;
'stop')
mutagen sync terminate m2d-sync
mutagen sync terminate $sync_name
;;
'status')
mutagen sync monitor m2d-sync
mutagen sync monitor $sync_name
;;
'restart')
_m2d_sync stop "$env_file"
Expand All @@ -374,20 +426,23 @@ function _m2d_sync ()

M2D_SCRIPT_DIRECTORY=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)
M2D_PROJECT_PATH="$(realpath $M2D_SCRIPT_DIRECTORY/../)"
M2D_ENV_FILE="$(realpath $M2D_PROJECT_PATH/.env)"
M2D_ENV_FILE="$M2D_PROJECT_PATH/.env"

case $1 in
'set')
case $2 in
'-h'|'--help')
_m2d_display_help_set
;;
'suffix')
_m2d_env_set_parameter "$M2D_ENV_FILE" M2D_PROJECT_SUFFIX "$3"
;;
'php')
_m2d_env_set_parameter "$M2D_ENV_FILE" "M2D_WEB_SERVER_PHP_VERSION" "$3"
;;
'es')
_m2d_env_set_parameter "$M2D_ENV_FILE" "M2D_SEARCH_ENGINE_VENDOR" elasticsearch
_m2d_env_set_parameter "$M2D_ENV_FILE" "M2D_SEARCH_ENGINE_VERSION" "$3"
_m2d_env_set_parameter "$M2D_ENV_FILE" "M2D_SEARCH_ENGINE_VERSION" "${3:-7}"
;;
*)
_m2d_env_set_parameter "$M2D_ENV_FILE" "$2" "$3"
Expand Down
24 changes: 12 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ version: '3'

services:
web:
hostname: magento2.test
container_name: m2d-web
container_name: "m2d-web${M2D_PROJECT_SUFFIX:-}"
restart: unless-stopped
build:
context: .
Expand All @@ -13,11 +12,12 @@ services:
- M2D_WEB_SERVER_PHP_VERSION=${M2D_WEB_SERVER_PHP_VERSION:-8.1}
- M2D_ENABLE_BLACKFIRE=${M2D_ENABLE_BLACKFIRE:-no}
- M2D_XDEBUG_IDE_KEY=${M2D_XDEBUG_IDE_KEY:-PHPSTORM}
- M2D_HOSTNAME_FOR_MAILCATCHER="m2d-mailcatcher${M2D_PROJECT_SUFFIX:-}"
environment:
- BLACKFIRE_CLIENT_ID
- BLACKFIRE_CLIENT_TOKEN
- MAGENTO_CLOUD_CLI_TOKEN
- COMPOSER_AUTH
# - COMPOSER_AUTH
ports:
- "${M2D_PORT_FOR_WEB_SERVER_HTTP:-80}:80"
- "${M2D_PORT_FOR_WEB_SERVER_HTTPS:-443}:443"
Expand All @@ -38,7 +38,7 @@ services:
args:
- M2D_DB_ENGINE_VENDOR=${M2D_DB_ENGINE_VENDOR:-mariadb}
- M2D_DB_ENGINE_VERSION=${M2D_DB_ENGINE_VERSION:-10}
container_name: m2d-db
container_name: "m2d-db${M2D_PROJECT_SUFFIX:-}"
restart: unless-stopped
environment:
MYSQL_DATABASE: magento
Expand All @@ -59,7 +59,7 @@ services:
- M2D_ENABLE_SEARCH_ENGINE=${M2D_ENABLE_SEARCH_ENGINE:-yes}
- M2D_SEARCH_ENGINE_VENDOR=${M2D_SEARCH_ENGINE_VENDOR:-elasticsearch}
- M2D_SEARCH_ENGINE_VERSION=${M2D_SEARCH_ENGINE_VERSION:-7}
container_name: m2d-searchengine
container_name: "m2d-searchengine${M2D_PROJECT_SUFFIX:-}"
environment:
- "ES_JAVA_OPTS=-Xms128m -Xmx1g"
- "OPENSEARCH_JAVA_OPTS=-Xms128m -Xmx1g"
Expand All @@ -75,7 +75,7 @@ services:
- M2D_ENABLE_MESSAGE_BROKER=${M2D_ENABLE_MESSAGE_BROKER:-no}
- M2D_MESSAGE_BROKER_VENDOR=${M2D_MESSAGE_BROKER_VENDOR:-rabbitmq}
- M2D_MESSAGE_BROKER_VERSION=${M2D_MESSAGE_BROKER_VERSION:-3.8}
container_name: m2d-messagebroker
container_name: "m2d-messagebroker${M2D_PROJECT_SUFFIX:-}"
restart: unless-stopped
ports:
- "${M2D_PORT_FOR_MESSAGE_BROKER:-15672}:15672"
Expand All @@ -89,7 +89,7 @@ services:
- M2D_ENABLE_WEB_CACHE=${M2D_ENABLE_WEB_CACHE:-no}
- M2D_WEB_CACHE_VENDOR=${M2D_WEB_CACHE_VENDOR:-varnish}
- M2D_WEB_CACHE_VERSION=${M2D_WEB_CACHE_VERSION:-6}
container_name: m2d-webcache
container_name: "m2d-webcache${M2D_PROJECT_SUFFIX:-}"
restart: unless-stopped
ports:
- "${M2D_PORT_FOR_WEB_CACHE:-8080}:8080"
Expand All @@ -103,7 +103,7 @@ services:
- M2D_ENABLE_DB_CACHE=${M2D_ENABLE_DB_CACHE:-no}
- M2D_DB_CACHE_VENDOR=${M2D_DB_CACHE_VENDOR:-redis}
- M2D_DB_CACHE_VERSION=${M2D_DB_CACHE_VERSION:-6.0}
container_name: m2d-dbcache
container_name: "m2d-dbcache${M2D_PROJECT_SUFFIX:-}"
restart: unless-stopped

mailcatcher:
Expand All @@ -113,7 +113,7 @@ services:
target: m2d_mailcatcher
args:
- M2D_ENABLE_MAILCATCHER=${M2D_ENABLE_MAILCATCHER:-no}
container_name: m2d-mailcatcher
container_name: "m2d-mailcatcher${M2D_PROJECT_SUFFIX:-}"
ports:
- "${M2D_PORT_FOR_MAILCATCHER:-1080}:1080"

Expand All @@ -124,7 +124,7 @@ services:
target: m2d_blackfire
args:
- M2D_ENABLE_BLACKFIRE=${M2D_ENABLE_BLACKFIRE:-no}
container_name: m2d-blackfire
container_name: "m2d-blackfire${M2D_PROJECT_SUFFIX:-}"
environment:
- BLACKFIRE_SERVER_ID
- BLACKFIRE_SERVER_TOKEN
Expand All @@ -138,7 +138,7 @@ services:
- M2D_CPU_TYPE=${M2D_CPU_TYPE:-m1}
- M2D_ENABLE_SELENIUM=${M2D_ENABLE_SELENIUM:-no}
- M2D_SELENIUM_VERSION=${M2D_SELENIUM_VERSION:-'3.14.0'}
container_name: m2d-selenium
container_name: "m2d-selenium${M2D_PROJECT_SUFFIX:-}"
volumes:
- /dev/shm:/dev/shm
ports:
Expand All @@ -148,7 +148,7 @@ services:

networks:
default:
name: m2d-network
name: "m2d-network${M2D_PROJECT_SUFFIX:-}"

volumes:
home-volume:
Expand Down
Loading

0 comments on commit 73975e5

Please sign in to comment.