Coder Social home page Coder Social logo

sta1r / docker-magento Goto Github PK

View Code? Open in Web Editor NEW

This project forked from markshust/docker-magento

0.0 1.0 0.0 371 KB

Mark Shust's Docker Configuration for Magento

Home Page: https://courses.markshust.com/p/setup-magento-2-development-environment-docker

License: MIT License

Shell 45.17% PowerShell 16.87% Dockerfile 37.96%

docker-magento's Introduction

markshust/docker-magento

Mark Shust's Docker Configuration for Magento

Supported Magento Versions Docker Hub Pulls - Nginx Docker Hub Pulls - PHP Maintained - Yes

Table of contents

Free Course

I created a free screencast course which details the basic usage of this project:

Setup a Magento 2 Development Environment with Docker
Setup a Magento 2 Development Environment with Docker

Docker Hub

View Dockerfiles:

Usage

This configuration is intended to be used as a Docker-based development environment for Magento 2.

Folders:

  • images: Docker images for nginx and php
  • compose: sample setups with Docker Compose

The Magento 1 version of this development environment has been deprecated and is no longer supported. PHP 5 was used as it's base, and that version has reached end-of-life. If you still wish to use this setup, please reference compose/magento-1 on tag 20.1.1, but please be aware these images are no longer maintained.

Prerequisites

This setup assumes you are running Docker on a computer with at least 4GB of allocated RAM, a dual-core, and an SSD hard drive. Download & Install Docker Desktop.

This configuration has been tested on Mac & Linux.

Windows Configurations: The Windows configuration does not currently work and is in need of a contributor to get functional once again. Please see issue 100 to contribute.

Setup

Automated Setup (New Project)

macOS & Linux Only

Run this automated one-liner from the directory you want to install your project to:

curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/onelinesetup | bash -s -- magento2.test 2.3.3

The magento2.test above defines the hostname to use, and the 2.3.3 defines the Magento version to install. Note that since we need a write to /etc/hosts for DNS resolution, you will be prompted for your system password during setup.

After the one-liner above completes running, you should be able to access your site at https://magento2.test.

Manual Setup

Same result as the one-liner above. Just replace magento2.test references with the hostname that you wish to use.

New Projects

# Download the Docker Compose template:
curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash -s -- magento-2

# Download the version of Magento you want to use with:
bin/download 2.3.3

# or if you'd rather install with Composer, run:
#
# OPEN SOURCE:
#
# rm -rf src
# composer create-project --repository=https://repo.magento.com/ --ignore-platform-reqs magento/project-community-edition=2.3.3 src
#
# COMMERCE:
#
# rm -rf src
# composer create-project --repository=https://repo.magento.com/ --ignore-platform-reqs magento/project-enterprise-edition=2.3.3 src

# Create a DNS host entry for the site:
echo "127.0.0.1 magento2.test" | sudo tee -a /etc/hosts

# Run the setup installer for Magento:
bin/setup magento2.test

open https://magento2.test

Existing Projects

# Download the Docker Compose template:
curl -s https://raw.githubusercontent.com/markshust/docker-magento/master/lib/template | bash -s -- magento-2

# Remove existing src directory:
rm -rf src

# Replace with existing source code of your existing Magento instance:
cp -R ~/Sites/existing src
# or: git clone [email protected]:myrepo.git src

# Create a DNS host entry for the site:
echo "127.0.0.1 magento2.test" | sudo tee -a /etc/hosts

# Copy some files to the containers and install dependencies, then restart the containers:
docker-compose up -d
bin/copytocontainer --all

# Install composer dependencies, then copy artifacts back to the host:
bin/composer install
bin/copyfromcontainer vendor

# Import existing database:
bin/clinotty mysql -hdb -umagento -pmagento magento < existing/magento.sql

# Update database connection details:
# vi src/app/etc/env.php

# Set base URLs to local environment URL:
bin/magento config:set web/secure/base_url https://magento2.test/
bin/magento config:set web/unsecure/base_url https://magento2.test/

bin/restart

open https://magento2.test

For more details on how everything works, see the extended setup readme.

Updates

To update your project to the latest version of docker-magento, run:

bin/update

We recommend keeping your docker config files in version control, so you can monitor the changes to files after updates. After reviewing the code updates and ensuring they updated as intended, run bin/restart to restart your containers to have the new configuration take effect.

It is recommended to keep your root docker config files in one repository, and your Magento code setup in another. This ensures the Magento base path lives at the top of one specific repository, which makes automated build pipelines and deployments easy to manage, and maintains compatibility with projects such as Magento Cloud.

Older Versions

Versions older than 24.0.0 did not include a bin/update helper script. In this situation, you can download the file to your project by running:

(cd bin && curl -OL https://raw.githubusercontent.com/markshust/docker-magento/master/compose/magento-2/bin/update && chmod +x update)

You'll now have the bin/update helper script, and can run it to update your project.

Custom CLI Commands

  • bin/bash: Drop into the bash prompt of your Docker container. The phpfpm container should be mainly used to access the filesystem within Docker.
  • bin/cli: Run any CLI command without going into the bash prompt. Ex. bin/cli ls
  • bin/clinotty: Run any CLI command with no TTY. Ex. bin/clinotty chmod u+x bin/magento
  • bin/composer: Run the composer binary. Ex. bin/composer install
  • bin/copyfromcontainer: Copy folders or files from container to host. Ex. bin/copyfromcontainer vendor
  • bin/copytocontainer: Copy folders or files from host to container. Ex. bin/copytocontainer --all
  • bin/dev-urn-catalog-generate: Generate URN's for PHPStorm and remap paths to local host. Restart PHPStorm after running this command.
  • bin/devconsole: Alias for bin/n98-magerun2 dev:console
  • bin/download: Download & extract specific Magento version to the src directory. Ex. bin/download 2.3.3
  • bin/fixowns: This will fix filesystem ownerships within the container.
  • bin/fixperms: This will fix filesystem permissions within the container.
  • bin/grunt: Run the grunt binary. Note that this runs the version from the node_modules directory for project version parity. Ex. bin/grunt exec
  • bin/magento: Run the Magento CLI. Ex: bin/magento cache:flush
  • bin/n98-magerun2: Access the n98 magerun CLI. Ex: bin/n98-magerun2 dev:console
  • bin/node: Run the node binary. Ex. bin/node --version
  • bin/npm: Run the npm binary. Ex. bin/npm install
  • bin/pwa-studio: (BETA) Start the PWA Studio server. Note that Chrome will throw SSL cert errors and not allow you to view the site, but Firefox will.
  • bin/redis: Run a command from the redis container. Ex bin/redis redis-cli monitor
  • bin/remove: Remove all containers.
  • bin/removevolumes: Remove all volumes.
  • bin/restart: Stop and then start all containers.
  • bin/root: Run any CLI command as root without going into the bash prompt. Ex bin/root apt-get install nano
  • bin/rootnotty: Run any CLI command as root with no TTY. Ex bin/rootnotty chown -R app:app /var/www/html
  • bin/setup: Run the Magento setup process to install Magento from the source code, with optional domain name. Defaults to magento2.test. Ex. bin/setup magento2.test
  • bin/setup-pwa-studio: (BETA) Install PWA Studio (requires NodeJS and Yarn to be installed on the host machine). Pass in your base site domain, otherwise the default master-7rqtwti-mfwmkrjfqvbjk.us-4.magentosite.cloud will be used. Ex: bin/setup-pwa-studio magento2.test
  • bin/start: Start all containers, good practice to use this instead of docker-compose up -d, as it may contain additional helpers.
  • bin/status: Check the container status.
  • bin/stop: Stop all containers.
  • bin/update: Update your project to the most recent version of docker-magento.
  • bin/xdebug: Disable or enable Xdebug. Accepts params disable (default) or enable. Ex. bin/xdebug enable

Misc Info

Database

The hostname of each service is the name of the service within the docker-compose.yml file. So for example, MySQL's hostname is db (not localhost) when accessing it from within a Docker container. Elasticsearch's hostname is elasticsearch.

Here's an example of how to connect to the MySQL cli tool of the Docker instance:

bin/cli mysql -h db -umagento -pmagento magento

You can use the bin/clinotty helper script to import a database. This example uses the root MySQL user, and looks for the dbdump.sql file in your local host directory:

bin/clinotty mysql -h db -u root -pmagento magento < dbdump.sql

Composer Authentication

First setup Magento Marketplace authentication (details in the DevDocs).

Copy src/auth.json.sample to src/auth.json. Then, update the username and password values with your Magento public and private keys, respectively. Finally, copy the file to the container by running bin/copytocontainer auth.json.

Email / Mailhog

View emails sent locally through Mailhog by visiting http://{yourdomain}:8025

Redis

Redis is now the default cache and session storage engine, and is automatically configured & enabled when running bin/setup on new installs.

Use the following lines to enable Redis on existing installs:

Enable for Cache:

bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=redis --cache-backend-redis-db=0

Enable for Full Page Cache: bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=redis --page-cache-redis-db=1

Enable for Session:

bin/magento setup:config:set --session-save=redis --session-save-redis-host=redis --session-save-redis-log-level=4 --session-save-redis-db=2

You may also monitor Redis by running: bin/redis redis-cli monitor

For more information about Redis usage with Magento, see the DevDocs.

Xdebug & VS Code

Install and enable the PHP Debug extension from the Visual Studio Marketplace.

Otherwise, this project now automatically sets up Xdebug support with VS Code. If you wish to set this up manually, please see the .vscode/launch.json file.

Xdebug & PHPStorm

  1. First, install the Chrome Xdebug helper. After installed, right click on the Chrome icon for it and go to Options. Under IDE Key, select PHPStorm from the list and click Save.

  2. Next, enable Xdebug in the PHP-FPM container by running: bin/xdebug enable, the restart the docker containers (CTRL+C then bin/start).

  3. Then, open PHPStorm > Preferences > Languages & Frameworks > PHP and configure:

    • CLI Interpreter

      • Create a new interpreter and specify From Docker, and name it markoshust/magento-php:7-2-fpm.
      • Choose Docker, then select the markoshust/magento-php:7-2-fpm image name, and set the PHP Executable to php.
    • Path mappings

      • Don't do anything here as the next Docker container step will automatically setup a path mapping from /var/www/html to ./src.
    • Docker container

      • Remove any pre-existing volume bindings.
      • Ensure a volume binding has been setup for Container path of /var/www/html mapped to the Host path of ./src.
  4. Open PHPStorm > Preferences > Languages & Frameworks > PHP > Debug and set Debug Port to 9001.

  5. Open PHPStorm > Preferences > Languages & Frameworks > PHP > DBGp Proxy and set Port to 9001.

  6. Open PHPStorm > Preferences > Languages & Frameworks > PHP > Servers and create a new server:

    • Set Name and Host to your domain name (ex. magento2.test)
    • Keep port set to 80
    • Check the Path Mappings box and map src to the absolute path of /var/www/html
  7. Go to Run > Edit Configurations and create a new PHP Remote Debug configuration by clicking the plus sign and selecting it. Set the Name to your domain (ex. magento2.test). Check the Filter debug connection by IDE key checkbox, select the server you just setup, and under IDE Key enter PHPSTORM. This IDE Key should match the IDE Key set by the Chrome Xdebug Helper. Then click OK to finish setting up the remote debugger in PHPStorm.

  8. Open up src/pub/index.php, and set a breakpoint near the end of the file. Go to Run > Debug 'magento2.test', and open up a web browser. Ensure the Chrome Xdebug helper is enabled by clicking on it > Debug. Navigate to your Magento store URL, and Xdebug within PHPStorm should now trigger the debugger and pause at the toggled breakpoint.

Linux

Running Docker on Linux should be pretty straight-forward. Note that you need to run some post install commands as well as installing Docker Compose. These steps are taken care of automatically with Docker Desktop, but not on Linux.

Credits

Mark Shust

I'm a Certified Magento Developer & Architect and Zend Certified Engineer, and available for consulting & development of your next project ๐Ÿค“. You can read technical articles on my blog at markshust.com or contact me directly at [email protected].

Nexcess

A special thanks goes out to Nexcess for hosting public archives of every version of Magento ๐Ÿ’™. I've used their Magento hosting services in the past also (both shared and enteprise offerings) and they're great, ...highly recommended!

License

MIT

docker-magento's People

Contributors

deanpodgornik avatar fprieur avatar ifiokjr avatar jerrylopez avatar markshust avatar phedoreanu avatar rataj86 avatar salgua avatar seb2nim avatar shkoliar avatar torhoehn avatar u-maxx avatar zsoerenm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.