diff --git a/.env.example b/.env.example index efd437c..1f32994 100644 --- a/.env.example +++ b/.env.example @@ -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 @@ -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: diff --git a/.gitignore b/.gitignore index 4c49bd7..d0ae5a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .env +.env.back diff --git a/README.md b/README.md index 5e22cf8..f645535 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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). diff --git a/bin/m2d b/bin/m2d index 09bd5b7..fbc9403 100755 --- a/bin/m2d +++ b/bin/m2d @@ -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 @@ -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 @@ -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 " } @@ -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 @@ -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 @@ -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\` " } @@ -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. @@ -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 @@ -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 @@ -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 @@ -292,7 +342,6 @@ 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 @@ -300,6 +349,7 @@ function _m2d_sync () 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" @@ -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 \ @@ -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" @@ -374,7 +426,7 @@ 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') @@ -382,12 +434,15 @@ case $1 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" diff --git a/docker-compose.yml b/docker-compose.yml index 992c117..7d1bbbf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: . @@ -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" @@ -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 @@ -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" @@ -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" @@ -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" @@ -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: @@ -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" @@ -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 @@ -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: @@ -148,7 +148,7 @@ services: networks: default: - name: m2d-network + name: "m2d-network${M2D_PROJECT_SUFFIX:-}" volumes: home-volume: diff --git a/services/web-servers/apache/Dockerfile b/services/web-servers/apache/Dockerfile index 7db5461..b868bf7 100644 --- a/services/web-servers/apache/Dockerfile +++ b/services/web-servers/apache/Dockerfile @@ -1,5 +1,6 @@ ARG M2D_WEB_SERVER_PHP_VERSION='8.1' ARG M2D_ENABLE_BLACKFIRE='no' +ARG M2D_HOSTNAME_FOR_MAILCATCHER='m2d-mailcatcher' ## BASE PHP IMAGE - COMMON FOR ALL PHP VERSIONS ## FROM php:${M2D_WEB_SERVER_PHP_VERSION}-apache-bullseye AS m2d_web_server_apache_php_base_ @@ -64,12 +65,13 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local # TOOLS: COPY ./services/web-servers/apache/tools/* /usr/local/bin/ -# SENDMAIL: -RUN echo "sendmail_path=/usr/bin/msmtp -t" >> /usr/local/etc/php/conf.d/mailcatcher.ini \ - && echo "memory_limit=2G" >> /usr/local/etc/php/conf.d/custom.ini \ - && echo "max_input_vars=10000" >> /usr/local/etc/php/conf.d/custom.ini \ - && echo "account default" >> /etc/msmtprc \ - && echo "host mailcatcher" >> /etc/msmtprc \ +# PHP CONFIG FILES: +COPY ./services/web-servers/apache/etc/php/* /usr/local/etc/php/conf.d/ + +# MAILCATCHER: +ENV M2D_HOSTNAME_FOR_MAILCATCHER=$M2D_HOSTNAME_FOR_MAILCATCHER +RUN echo "account default" >> /etc/msmtprc \ + && echo "host $M2D_HOSTNAME_FOR_MAILCATCHER" >> /etc/msmtprc \ && echo "port 1025" >> /etc/msmtprc \ && echo "auto_from on" >> /etc/msmtprc @@ -111,10 +113,11 @@ ARG CACHEBUST=1 # CUSTOM CONFIGURATIONS: COPY ./services/web-servers/apache/etc/apache2 /etc/apache2 -COPY ./services/web-servers/apache/etc/git/gitconfig ${_HOME_DIRECTORY}/.gitconfig -COPY ./services/web-servers/apache/etc/composer/auth.json* /${_HOME_DIRECTORY}/.composer/ COPY ./services/web-servers/apache/etc/fixtures /etc/fixtures +COPY ./services/web-servers/apache/etc/git/gitconfig ${_HOME_DIRECTORY}/.gitconfig +COPY ./services/web-servers/apache/etc/composer/auth.json* ${_HOME_DIRECTORY}/.composer/ COPY ./services/web-servers/apache/etc/m2install/.m2install.conf* ${_HOME_DIRECTORY}/ +RUN chown -R "${_USER}:${_USER}" ${_HOME_DIRECTORY}/.gitconfig ${_HOME_DIRECTORY}/.composer ${_HOME_DIRECTORY}/.m2install.conf* # MAGENTO TOOLS: RUN curl -o /usr/local/bin/m2install.sh https://raw.githubusercontent.com/yvoronoy/m2install/master/m2install.sh \ diff --git a/services/web-servers/apache/etc/php/custom.ini b/services/web-servers/apache/etc/php/custom.ini new file mode 100644 index 0000000..c4ecc0b --- /dev/null +++ b/services/web-servers/apache/etc/php/custom.ini @@ -0,0 +1,2 @@ +memory_limit=${M2D_WEB_SERVER_PHP_MEMORY_LIMIT:-2G} +max_input_vars=10000 \ No newline at end of file diff --git a/services/web-servers/apache/etc/php/mailcatcher.ini b/services/web-servers/apache/etc/php/mailcatcher.ini new file mode 100644 index 0000000..fa91803 --- /dev/null +++ b/services/web-servers/apache/etc/php/mailcatcher.ini @@ -0,0 +1 @@ +sendmail_path=/usr/bin/msmtp -t diff --git a/src/.gitignore b/src/.gitignore deleted file mode 100644 index f935021..0000000 --- a/src/.gitignore +++ /dev/null @@ -1 +0,0 @@ -!.gitignore