Skip to content
This repository has been archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Add Magento 2 services
Browse files Browse the repository at this point in the history
  • Loading branch information
punkstar committed Feb 12, 2016
1 parent 191456e commit 5e55413
Show file tree
Hide file tree
Showing 17 changed files with 567 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/composer.env
/magento
55 changes: 55 additions & 0 deletions 7.0-cli/Dockerfile
@@ -0,0 +1,55 @@
#
# Command line, PHP 7.0, Magento 2 compatible container.
#
# Credit to Mark Shust <mark.shust@mageinferno.com> for the basis of this
#聽Dockerfile in the https://github.com/mageinferno/docker-magento2-php project.
#

FROM php:7.0-cli

MAINTAINER Nick Jones <nick@nicksays.co.uk>

# Install dependencies
RUN apt-get update \
&& apt-get install -y \
cron \
libfreetype6-dev \
libicu-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libxslt1-dev

# Configure the gd library
RUN docker-php-ext-configure \
gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/

# Install required PHP extensions
RUN docker-php-ext-install \
gd \
intl \
mbstring \
mcrypt \
pdo_mysql \
xsl \
zip

# Get composer installed to /usr/local/bin/composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

VOLUME /root/.composer/cache

ADD bin/* /usr/local/bin/
ADD etc/php.ini /usr/local/etc/php/conf.d/zz-magento.ini

ENV PHP_MEMORY_LIMIT 2G
ENV MAGENTO_ROOT /magento
ENV COMPOSER_GITHUB_TOKEN ""
ENV COMPOSER_MAGENTO_USERNAME ""
ENV COMPOSER_MAGENTO_PASSWORD ""
ENV DEBUG false
ENV IS_OSX false

ENTRYPOINT ["/usr/local/bin/docker-environment"]

CMD ["bash"]
29 changes: 29 additions & 0 deletions 7.0-cli/bin/docker-environment
@@ -0,0 +1,29 @@
#!/bin/bash

[ "$DEBUG" = "true" ] && set -x

# If we're using OSX then we need to mess with the permissions
if [[ "$IS_OSX" = "true" ]]; then
echo "OSX flag is set, changing www-data uid/gid"

usermod -u 501 www-data
groupmod -g 9920 dialout # Move dialout from 20 to make room for www-data
groupmod -g 20 www-data
fi

# Ensure our Magento directory exists
mkdir -p $MAGENTO_ROOT
chown www-data:www-data $MAGENTO_ROOT

# Configure PHP
[ ! -z "${PHP_MEMORY_LIMIT}" ] && sed -i "s/!PHP_MEMORY_LIMIT!/${PHP_MEMORY_LIMIT}/" /usr/local/etc/php/conf.d/zz-magento.ini

# Configure composer
[ ! -z "${COMPOSER_GITHUB_TOKEN}" ] && \
composer config --global github-oauth.github.com $COMPOSER_GITHUB_TOKEN

[ ! -z "${COMPOSER_MAGENTO_USERNAME}" ] && \
composer config --global http-basic.repo.magento.com \
$COMPOSER_MAGENTO_USERNAME $COMPOSER_MAGENTO_PASSWORD

exec "$@"
8 changes: 8 additions & 0 deletions 7.0-cli/bin/magento-command
@@ -0,0 +1,8 @@
#!/bin/bash

[ "$DEBUG" = "true" ] && set -x

MAGENTO_COMMAND="$MAGENTO_ROOT/bin/magento"

chmod +x $MAGENTO_COMMAND
su -c "$MAGENTO_COMMAND $*" -s /bin/bash www-data
47 changes: 47 additions & 0 deletions 7.0-cli/bin/magento-installer
@@ -0,0 +1,47 @@
#!/bin/bash

[ "$DEBUG" = "true" ] && set -x

# Get composer auth information into an environment variable to avoid "you need
# to be using an interactive terminal to authenticate".
COMPOSER_AUTH=`cat /root/.composer/auth.json`

composer --version

echo "Creating Magento ($M2SETUP_VERSION) project from composer"

composer create-project \
--repository-url=https://repo.magento.com/ \
magento/project-community-edition=$M2SETUP_VERSION \
--no-interaction \
$MAGENTO_ROOT

echo "Install Magento"

php /magento/bin/magento setup:install \
--db-host=$M2SETUP_DB_HOST \
--db-name=$M2SETUP_DB_NAME \
--db-user=$M2SETUP_DB_USER \
--db-password=$M2SETUP_DB_PASSWORD \
--base-url=$M2SETUP_BASE_URL \
--admin-firstname=$M2SETUP_ADMIN_FIRSTNAME \
--admin-lastname=$M2SETUP_ADMIN_LASTNAME \
--admin-email=$M2SETUP_ADMIN_EMAIL \
--admin-user=$M2SETUP_ADMIN_USER \
--admin-password=$M2SETUP_ADMIN_PASSWORD

php /magento/bin/magento index:reindex
php /magento/bin/magento setup:static-content:deploy

echo "Fixing file permissions.."

sed -i 's/0770/0775/g' $MAGENTO_ROOT/vendor/magento/framework/Filesystem/DriverInterface.php
sed -i 's/0660/0664/g' $MAGENTO_ROOT/vendor/magento/framework/Filesystem/DriverInterface.php

find $MAGENTO_ROOT/pub -type f -exec chmod 664 {} \;
find $MAGENTO_ROOT/pub -type d -exec chmod 775 {} \;
find $MAGENTO_ROOT/var/generation -type d -exec chmod g+s {} \;

chown -R www-data:www-data $MAGENTO_ROOT

echo "Installation complete"
3 changes: 3 additions & 0 deletions 7.0-cli/etc/php.ini
@@ -0,0 +1,3 @@
; This file is created automatically by the docker build

memory_limit = !PHP_MEMORY_LIMIT! ; Variable: PHP_MEMORY_LIMIT
50 changes: 50 additions & 0 deletions 7.0-fpm/Dockerfile
@@ -0,0 +1,50 @@
#
# FPM, PHP 7.0, Magento 2 compatible container.
#
# Credit to Mark Shust <mark.shust@mageinferno.com> for the basis of this
#聽Dockerfile in the https://github.com/mageinferno/docker-magento2-php project.
#

FROM php:7.0-fpm

MAINTAINER Nick Jones <nick@nicksays.co.uk>

# Install dependencies
RUN apt-get update \
&& apt-get install -y \
cron \
libfreetype6-dev \
libicu-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
libxslt1-dev

# Configure the gd library
RUN docker-php-ext-configure \
gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/

# Install required PHP extensions
RUN docker-php-ext-install \
gd \
intl \
mbstring \
mcrypt \
pdo_mysql \
xsl \
zip

VOLUME /root/.composer/cache

ADD bin/docker-environment /usr/local/bin/
ADD etc/php.ini /usr/local/etc/php/conf.d/zz-magento.ini
ADD etc/php-fpm.conf /usr/local/etc/

ENV PHP_MEMORY_LIMIT 2G
ENV MAGENTO_ROOT /magento
ENV MAGENTO_RUN_MODE developer
ENV DEBUG false
ENV IS_OSX false

ENTRYPOINT ["/usr/local/bin/docker-environment"]
CMD ["php-fpm", "-F"]
20 changes: 20 additions & 0 deletions 7.0-fpm/bin/docker-environment
@@ -0,0 +1,20 @@
#!/bin/bash

[ "$DEBUG" = "true" ] && set -x

# If we're using OSX then we need to mess with the permissions
if [[ "$IS_OSX" = "true" ]]; then
echo "OSX flag is set, changing www-data uid/gid"

usermod -u 501 www-data
groupmod -g 9920 dialout # Move dialout from 20 to make room for www-data
groupmod -g 20 www-data
fi

# Configure PHP
[ ! -z "${PHP_MEMORY_LIMIT}" ] && sed -i "s/!PHP_MEMORY_LIMIT!/${PHP_MEMORY_LIMIT}/" /usr/local/etc/php/conf.d/zz-magento.ini

# Configure PHP-FPM
[ ! -z "${MAGENTO_RUN_MODE}" ] && sed -i "s/!MAGENTO_RUN_MODE!/${MAGENTO_RUN_MODE}/" /usr/local/etc/php-fpm.conf

exec "$@"
29 changes: 29 additions & 0 deletions 7.0-fpm/etc/php-fpm.conf
@@ -0,0 +1,29 @@
; This file is created automatically by the docker build

[global]

error_log = /proc/self/fd/2
daemonize = no

[www]

; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2

user = www-data
group = www-data

listen = [::]:9000

pm = dynamic
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6

env[MAGE_MODE] = !MAGENTO_RUN_MODE!; #聽Variable: MAGENTO_RUN_MODE

clear_env = no

; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
3 changes: 3 additions & 0 deletions 7.0-fpm/etc/php.ini
@@ -0,0 +1,3 @@
; This file is created automatically by the docker build

memory_limit = !PHP_MEMORY_LIMIT! ; Variable: PHP_MEMORY_LIMIT
50 changes: 50 additions & 0 deletions Readme.md
@@ -0,0 +1,50 @@
#Magento 2 Docker

## Quick Start

cp composer.env.sample composer.env
# ..put the correct tokens into composer.env

docker-compose run cli magento-installer
docker-compose up -d
docker-compose restart

## Configuration

Configuration is driven through environment variables. A comprehensive list of the environment variables used can be found in each `Dockerfile` and the commands in each `bin/` directory.

* `PHP_MEMORY_LIMIT` - The memory limit to be set in the `php.ini`
* `MAGENTO_ROOT` - The directory to which Magento should be installed
* `MAGENTO_RUN_MODE` - Valid values, as defined in `Magento\Framework\App\State`: `developer`, `production`, `default`.
* `COMPOSER_GITHUB_TOKEN` - Your [GitHub OAuth token](https://getcomposer.org/doc/articles/troubleshooting.md#api-rate-limit-and-oauth-tokens), should it be needed
* `COMPOSER_MAGENTO_USERNAME` - Your Magento Connect public authentication key ([how to get](http://devdocs.magento.com/guides/v2.0/install-gde/prereq/connect-auth.html))
* `COMPOSER_MAGENTO_PASSWORD` - Your Magento Connect private authentication key
* `DEBUG` - Toggles tracing in the bash commands when exectued; nothing to do with Magento`
* `IS_OSX` - If this is set to "true" then the uid and gid of `www-data` will be modified in the container

A sample `docker-compose.yml` is provided in this repository.

## CLI Usage

A number of commands are baked into the image and are available on the `$PATH`. These are:

* `magento-command` - Provides a user-safe wrapper around the `bin/magento` command.
* `magento-installer` - Installs and configures Magento into the directory defined in the `$MAGENTO_ROOT` environment variable.

It's recommended that you mount an external folder to `/root/.composer/cache`, otherwise you'll be waiting all day for Magento to download every time the container is booted.

CLI commands can be triggered by running:

docker-compose run cli magento-installer

Shell access to a CLI container can be triggered by running:

docker-compose run cli bash

## Implementation Notes

* In order to achieve a sane environment for executing commands in, a `docker-environment` script is included as the `ENTRYPOINT` in the container.

## Credits

Thanks to [Mark Shust](https://twitter.com/markshust) for his work on [docker-magento2-php](https://github.com/mageinferno/docker-magento2-php) that was used as a basis for this implementation. You solved a lot of the problems so I didn't need to!
3 changes: 3 additions & 0 deletions composer.env.sample
@@ -0,0 +1,3 @@
COMPOSER_GITHUB_TOKEN=0000000000000000000000000000000000000000
COMPOSER_MAGENTO_USERNAME=00000000000000000000000000000000
COMPOSER_MAGENTO_PASSWORD=00000000000000000000000000000000
79 changes: 79 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,79 @@
version: "2"
services:
web:
# image: meanbee/magento2-nginx
build:
context: ./nginx
ports:
- "80:80"
links:
- fpm
- db
volumes_from:
- appdata
env_file:
- ./global.env
environment:
- VIRTUAL_HOST=magento2.docker

fpm:
# image: meanbee/magento2-php-fpm:7.0
build:
context: ./7.0-fpm
ports:
- 9000
links:
- db
volumes_from:
- appdata
env_file:
- ./global.env

db:
image: mariadb:10
ports:
- 3306
volumes_from:
- dbdata
environment:
- MYSQL_ROOT_PASSWORD=magento2
- MYSQL_DATABASE=magento2
- MYSQL_USER=magento2
- MYSQL_PASSWORD=magento2

cli:
# image: meanbee/magento2-php-fpm:7.0
build:
context: ./7.0-cli
links:
- db
volumes:
- ~/.composer/cache:/root/.composer/cache
volumes_from:
- appdata
env_file:
- ./global.env
- ./composer.env
environment:
- M2SETUP_DB_HOST=db
- M2SETUP_DB_NAME=magento2
- M2SETUP_DB_USER=magento2
- M2SETUP_DB_PASSWORD=magento2
- M2SETUP_BASE_URL=http://magento2.docker/
- M2SETUP_ADMIN_FIRSTNAME=Admin
- M2SETUP_ADMIN_LASTNAME=User
- M2SETUP_ADMIN_EMAIL=dummy@gmail.com
- M2SETUP_ADMIN_USER=admin
- M2SETUP_ADMIN_PASSWORD=password1
- M2SETUP_VERSION=2.0.2
# - M2SETUP_USE_SAMPLE_DATA=true

appdata:
image: tianon/true
volumes:
- ./magento:/magento

dbdata:
image: tianon/true
volumes:
- /var/lib/mysql
4 changes: 4 additions & 0 deletions global.env
@@ -0,0 +1,4 @@
MAGENTO_RUN_MODE=developer
PHP_MEMORY_LIMIT=2048M
IS_OSX=true
DEBUG=false

0 comments on commit 5e55413

Please sign in to comment.