Giter VIP home page Giter VIP logo

docker-magento2-compose's Introduction

Docker Magento 2

A proof-of-concept repo for dockerising a Magento 2 development environment.

Usage

First-time

  • Clone the repo:
git clone git clone [email protected]:purenetgit/purenet-magento2-setup-docker.git new-magento2-project
  • Run the setup container to setup the environment. This downloads the images if you don't already have them locally and then creates all the containers.
$ docker-compose up -d setup

The -d flag tells Docker Compose to run all the containers in the background.

Now modify the env/setup.env file to suit the project requirements:

MAGENTO2_DB_HOST=db
MAGENTO2_DB_NAME=magento2
MAGENTO2_DB_USER=magento2
MAGENTO2_DB_PASSWORD=magento2
MAGENTO2_BASE_URL=http://new-magento2-project.dev:8000/
MAGENTO2_ADMIN_FIRSTNAME=Admin
MAGENTO2_ADMIN_LASTNAME=User
[email protected]
MAGENTO2_ADMIN_USER=admin
MAGENTO2_ADMIN_PASSWORD=password123
MAGENTO2_ADMIN_URI=admin
MAGENTO2_CURRENCY=GBP
MAGENTO2_LANGUAGE=en_GB
MAGENTO2_TIMEZONE=Europe/London
MAGENTO2_VERSION=2.2.4

Do the same for env/mysql.env.

Now we're ready to install Magento 2:

$ docker-compose run --rm setup

This ties all the containers we just created together and also installs Magento 2 in a volume (data container).

The --rm flag tells Docker to remove the setup container after it has finished executing the commands defined in the RUN section of the Dockerfile.

Finally, modify the hosts file on your host (not inside the container):

$ sudo echo "127.0.0.1       new-magento2-project.dev" >> /etc/hosts

You can now go to MAGENTO2_BASE_URL in your browser.

Every other time

  • Run the app container to start the environment:
$ docker-compose up -d app
  • Once you've finished working on a project, stop the containers:
$ docker-compose stop

If you use docker-compose down, all the containers (including data containers) will be destroyed and you will need to follow the 'First time' steps to work with the project again.

Where's the code?

The Magento 2 core code is installed inside the container, so it won't be present on the host.

For local development, copy the contents of the appdata data volume to the host. This will enable code completion features in your IDE.

Working with git

Create a new git repo locally:

git init

Now add the remote repo as a remote:

git remote add <remote-name> <remote-clone-url>

Switching projects

  • First, stop docker-compose using:
$ docker-compose stop
  • Second, switch to the other project folder and start that environment:
$ docker-compose up -d app

In detail

Composer Setup

Authentication

Uncomment the composer line from appdata to mount a .composer directory to the www-data user home directory. Please first setup Magento Marketplace authentication (details at http://devdocs.magento.com/guides/v2.0/install-gde/prereq/connect-auth.html).

Place your auth token at ~/.composer/auth.json with the following contents, like so:

{
    "http-basic": {
        "repo.magento.com": {
            "username": "MAGENTO2_PUBLIC_KEY",
            "password": "MAGENTO2_PRIVATE_KEY"
        }
    }
}

Data Volumes

The Magento core code is stored within the appdata data volume.

docker cp CONTAINERID:/var/www/html ./

You can find the CONTAINERID by running the command docker ps.

Then, just uncomment the ./html/app/code:/var/www/html/app/code and ./html/app/design:/var/www/html/app/design lines within your docker-compose.override.yml file (appdata > volumes). This mounts your local app/code and app/design directories to the Docker data volume. Then, just restart your containers:

docker-compose up -d app

Any edits to these directories will correctly sync with your Docker volume.

Running Magento CLI tool

To run the command line tool, use the PHP container to run the command:

docker-compose exec NAME_OF_PHPFPM_CONTAINER ./bin/magento

or by using Docker:

docker exec NAME_OF_PHPFPM_CONTAINER ./bin/magento

You can easily set these up as aliases inside your ~/.bash_aliases file (or a similar script) as so:

alias m2='docker-compose exec phpfpm ./bin/magento'

This will allow you to clear the cache by running the following command right in terminal:

m2 cache:flush

TODO

  • Add ability to specify custom admin URI
  • Refactor mounting of ~/.composer/ into COMPOSER_HOME env var to make it platform agnostic and in order to get passwordless-install when installing via composer and make installing quicker
  • Merge the copying of code from /var/www/html on the appdata to html on the host into the mage-setup-raw script
  • Update service versions (e.g. Upgrade nginx to 1.13)
  • Add XDebug support
  • Install frontend tools (yarn, snowdog frontools, etc)
  • Update the environment variables
  • Optimise the service configs for M2
    • Nginx
    • PHP
    • MySQL
  • Add Redis support
  • Create a version for updating projects
  • Lint Dockerfiles using FROM:latest
  • Make it possible to run multiple magento2-docker-compose installs without ports clashing
  • Replace tianon/true image
  • Replace the necessity of running the setup container a second time to setup Magento - migrate it into a bash script running on the host

Workflow for adding new features to docker-magento2-compose

  • Checkout this repo in a new folder, named after the feature you're implementing
  • Modify the docker-compose.yml and env files accordingly to support your new feature (if necessary)
  • Run $ docker-compose run --rm setup to generate the environment
  • Run $ docker-compose up -d app to run the environment

If modifying the base images:

  • Modify the images accordingly and tag them with a new version
  • Update the docker-compose.yml to use the new version of the image
  • Once the feature has been completed, re-build the image with the latest tag
  • You may need to run docker-composer down to destroy any previously built images and force Docker Compose to use the newly built image(s)

docker-magento2-compose's People

Contributors

processeight 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.