Magento2 使用Docker的开发环境

2020年6月11日 浏览量 26 3 min read
Magento2 使用Docker的开发环境

Docker执行操作系统级虚拟化(容器化),这是指内核允许存在多个独立的用户空间实例的操作系统功能,更多详细信息请查看http://www.docker.com

让我们在http://m2.docker上运行Magento 2

首先要做的是在/etc/hosts文件中的添加以下代码:

127.0.0.1 m2.docker www.m2.docker

我们需要一些文件系统结构,因此执行以下操作:

tomas ~ $ mkdir m2.docker
tomas ~ $ cd m2.docker/
tomas ~/m2.docker $ mkdir docker
tomas ~/m2.docker $ mkdir html
tomas ~/m2.docker $ mkdir -p docker/db
tomas ~/m2.docker $ touch docker/xdebug.ini
tomas ~/m2.docker $ touch docker/apache.conf
tomas ~/m2.docker $ touch docker-compose.yml
tomas ~/m2.docker $ touch .env

 

现在,让我们逐个文件:

.env

在这个文件中,您可以编写将在docker-compose.yml中使用的变量

例如:

CONTAINER_PREFIX=m2docker
SERVER_NAME=m2.docker
SERVER_ALIAS=www.m2.docker
DIRECTORY_NAME=m2.docker
WEB_USER=magease
WEB_ROOT = /var/www/html
MYSQL_DB_HOST = ${CONTAINER_PREFIX}_db_1
MYSQL_DATABASE=magease
MYSQL_ROOT_USERNAME=root
MYSQL_ROOT_PASSWORD=magease
MYSQL_USER=magease
MYSQL_PASSWORD=magease
DOCKER_EXEC=docker exec
DOCKER_EXEC_INTERACTIVE=docker exec -i
DOCKER_EXEC_TTY=${DOCKER_EXEC_INTERACTIVE} -t

对于docker-compose.yml,您需要以下代码。

# https://docs.docker.com/compose/compose-file
version: "3.6"
 
# https://docs.docker.com/compose/compose-file/#service-configuration-reference
services:
 
    #custom name
    apache-php:
        # https://docs.docker.com/compose/compose-file/#image
        # https://githheizenberg ub.com/udovicic/echo => https://hub.docker.com/r/udovicic/echo/
        image: udovicic/echo:apache-php7.1
 
        # https://docs.docker.com/compose/compose-file/#ports
        ports:
        - "80:80"
 
        # https://docs.docker.com/compose/compose-file/#expose
        expose:
        - "9000"
 
        # https://docs.docker.com/compose/compose-file/#volumes
        volumes:
        - ./docker/apache.conf:/etc/apache2/sites-available/000-default.conf
        - ./docker/xdebug.ini:/etc/php/7.1/mods-available/xdebug.ini
        - ./html:/var/www/html
 
        # https://docs.docker.com/compose/compose-file/#environment
        environment:
        - TERM=xterm-256color
        - APACHE_RUN_USER=1000
 
        # https://docs.docker.com/compose/compose-file/#network-configuration-reference
        networks:
            default:
                aliases:
                - ${SERVER_NAME}
                - ${SERVER_ALIAS}
 
    db:
 
        # https://hub.docker.com/_/mysql/
        image: mysql:5.7
 
        volumes:
        - ./docker/db/data:/var/lib/mysql
 
        environment:
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
 
    redis:
 
        # https://hub.docker.com/_/redis/
        image: redis:latest
 
    phpmyadmin:
 
        # https://hub.docker.com/r/phpmyadmin/phpmyadmin/
        image: phpmyadmin/phpmyadmin
 
        ports:
        - "8080:80"
 
        environment:
            MYSQL_USERNAME: ${MYSQL_ROOT_USERNAME}
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

xdebug.ini

zend_extension=xdebug.so
xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=1

apache.conf

ServerAdmin webmaster@m2-sample.docker
    ServerName m2.docker
    Serveralias www.m2.docker
 
    DocumentRoot /var/www/html/pub
 
        Options FollowSymLinks
        AllowOverride None
 
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
 
        SetHandler "proxy:unix:/run/php/php-fpm.sock|fcgi://localhost/"
 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

我们有了工作环境,现在我们尝试运行它:

tomas ~/m2.docker $ docker-compose up -d
Creating network "m2docker_default" with the default driver
Creating m2docker_apache-php_1 ... done
Creating m2docker_redis_1      ... done
Creating m2docker_phpmyadmin_1 ... done
Creating m2docker_db_1         ... done
tomas ~/m2.docker $ docker ps
CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS              PORTS                            NAMES
e3384b0eff8c        mysql:5.7                     "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        3306/tcp                         m2docker_db_1
d283bf018330        phpmyadmin/phpmyadmin         "/run.sh phpmyadmin"     3 seconds ago       Up 2 seconds        9000/tcp, 0.0.0.0:8080->80/tcp   m2docker_phpmyadmin_1
ded8bce1d993        redis:latest                  "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        6379/tcp                         m2docker_redis_1
fe8a80763ac6        udovicic/echo:apache-php7.1   "/start.sh"              3 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp, 9000/tcp     m2docker_apache-php_1
tomas ~/m2.docker $

 

什么时候工作

Magento应该安装在“html”目录中。
请注意,在引用数据库地址时需要使用“db”,依此类推,只需检查docker-compose.yml(服务名称)。
另外,相关凭据位于.env文件中。

PhpMyAdmin可在 @ http://m2.docker:8080获得

这是一个非常基本的设置。我通常创建git存储库,最初有2个分支,例如m2-docker和master,然后我将.git目录复制到html和checkout master分支,而在基本目录中我检查m2-docker分支。

这是一个非常基本的设置。我通常创建git存储库,最初有2个分支,例如m2-docker和master,然后我将.git目录复制到html和checkout master分支,而在基本目录中签出m2-docker分支。

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1