通过 Laradock 学 Docker —— 配置篇

前言

众所周知docker https://www.docker.com 是当前最火热的容器引擎,因为沙盒特性,即插即用和快速方便的部署能力受到广大用户的喜爱。

docker 通常有两种执行方式,一种是直接通过命令行加参数拉取、运行容器,但因为用户的实际应用是复杂的,可能由多个容器组成,容器间存在关系,使用上可能需要重复多次部署。因此,需要一定的编排部署能力来简化这个操作。

另一种方式就是今天我们介绍的,通过编排文件部署的方式,也就是 compose 方式运行容器。Compose 运行需要有对应的 yml 文件以及 Dockerfile。通过调用 Docker/Swarm 的 API 进行容器应用的编排。

「laradock」http://laradock.io/ 就是一个利用 Compose 方式运行容器,功能完整的 PHP 集成研发环境。

环境依赖

  • Git
  • Docker >= 1.12

安装

安装 Docker

大家可以去官网https://store.docker.com/search?type=edition&offering=community,或者国内的服务提供商(如「Daocloud」https://download.daocloud.io/)下载相应操作系统的安装文件(或者在线安装),这里就不多叙述了。

如果是 linux 系统的话,安装完 docker,还需要单独安装 docker-compose。

安装 Laradock

1. 用 Git 克隆下源码

git clone https://github.com/laradock/laradock.git

2. 编辑配置文件

cp env-example .env

打开.env文件,修改公共变量 APPLICATION 的路径,默认路径是

APPLICATION=../

我们可以根据需要做相应修改,一般情况下,我们的主机上会有一个或多个项目,这样的话做如下设置:

APPLICATION=../www/

这是什么意思呢,我们来看下docker-compose.dev.yml文件

   version: "2"

   services:

   ### Applications Code Container #############################

       applications:
         volumes:
           - ${APPLICATION}:/var/www

我们看到我们配置了一个容器 applications,它映射了一个数据卷,把本地的 www 目录,映射到了容器内的 /var/www 目录

3. 查看相关编排文件配置

   version: '2'

   services:

   ### Applications Code Container #############################

       applications:
         image: tianon/true

   ### Workspace Utilities Container ###########################

       workspace:
         build:
           context: ./workspace
           args:
             - INSTALL_XDEBUG=${WORKSPACE_INSTALL_XDEBUG}
             - INSTALL_BLACKFIRE=${INSTALL_BLACKFIRE}
             - INSTALL_SOAP=${WORKSPACE_INSTALL_SOAP}
             - INSTALL_LDAP=${WORKSPACE_INSTALL_LDAP}
             - INSTALL_IMAP=${WORKSPACE_INSTALL_IMAP}
             - INSTALL_MONGO=${WORKSPACE_INSTALL_MONGO}
             - INSTALL_AMQP=${WORKSPACE_INSTALL_AMQP}
             - INSTALL_PHPREDIS=${WORKSPACE_INSTALL_PHPREDIS}
             - INSTALL_MSSQL=${WORKSPACE_INSTALL_MSSQL}
             - INSTALL_NODE=${WORKSPACE_INSTALL_NODE}
             - INSTALL_YARN=${WORKSPACE_INSTALL_YARN}
             - INSTALL_DRUSH=${WORKSPACE_INSTALL_DRUSH}
             - INSTALL_DRUPAL_CONSOLE=${WORKSPACE_INSTALL_DRUPAL_CONSOLE}
             - INSTALL_AEROSPIKE=${WORKSPACE_INSTALL_AEROSPIKE}
             - INSTALL_V8JS=${WORKSPACE_INSTALL_V8JS}
             - COMPOSER_GLOBAL_INSTALL=${WORKSPACE_COMPOSER_GLOBAL_INSTALL}
             - INSTALL_WORKSPACE_SSH=${WORKSPACE_INSTALL_WORKSPACE_SSH}
             - INSTALL_LARAVEL_ENVOY=${WORKSPACE_INSTALL_LARAVEL_ENVOY}
             - INSTALL_LARAVEL_INSTALLER=${WORKSPACE_INSTALL_LARAVEL_INSTALLER}
             - INSTALL_DEPLOYER=${WORKSPACE_INSTALL_DEPLOYER}
             - INSTALL_LINUXBREW=${WORKSPACE_INSTALL_LINUXBREW}
             - INSTALL_MC=${WORKSPACE_INSTALL_MC}
             - INSTALL_SYMFONY=${WORKSPACE_INSTALL_SYMFONY}
             - INSTALL_PYTHON=${WORKSPACE_INSTALL_PYTHON}
             - INSTALL_IMAGE_OPTIMIZERS=${WORKSPACE_INSTALL_IMAGE_OPTIMIZERS}
             - INSTALL_IMAGEMAGICK=${WORKSPACE_INSTALL_IMAGEMAGICK}
             - INSTALL_TERRAFORM=${WORKSPACE_INSTALL_TERRAFORM}
             - INSTALL_DUSK_DEPS=${WORKSPACE_INSTALL_DUSK_DEPS}
             - INSTALL_PG_CLIENT=${WORKSPACE_INSTALL_PG_CLIENT}
             - INSTALL_SWOOLE=${WORKSPACE_INSTALL_SWOOLE}
             - PUID=${WORKSPACE_PUID}
             - PGID=${WORKSPACE_PGID}
             - CHROME_DRIVER_VERSION=${WORKSPACE_CHROME_DRIVER_VERSION}
             - NODE_VERSION=${WORKSPACE_NODE_VERSION}
             - YARN_VERSION=${WORKSPACE_YARN_VERSION}
             - TZ=${WORKSPACE_TIMEZONE}
             - BLACKFIRE_CLIENT_ID=${BLACKFIRE_CLIENT_ID}
             - BLACKFIRE_CLIENT_TOKEN=${BLACKFIRE_CLIENT_TOKEN}
           dockerfile: "Dockerfile-${PHP_VERSION}"
         volumes_from:
           - applications
         extra_hosts:
           - "dockerhost:${DOCKER_HOST_IP}"
         ports:
           - "${WORKSPACE_SSH_PORT}:22"
         tty: true
         networks:
           - frontend
           - backend

   ### PHP-FPM Container #######################################

       php-fpm:
         build:
           context: ./php-fpm
           args:
             - INSTALL_XDEBUG=${PHP_FPM_INSTALL_XDEBUG}
             - INSTALL_BLACKFIRE=${INSTALL_BLACKFIRE}
             - INSTALL_SOAP=${PHP_FPM_INSTALL_SOAP}
             - INSTALL_MONGO=${PHP_FPM_INSTALL_MONGO}
             - INSTALL_AMQP=${PHP_FPM_INSTALL_AMQP}
             - INSTALL_MSSQL=${PHP_FPM_INSTALL_MSSQL}
             - INSTALL_ZIP_ARCHIVE=${PHP_FPM_INSTALL_ZIP_ARCHIVE}
             - INSTALL_BCMATH=${PHP_FPM_INSTALL_BCMATH}
             - INSTALL_GMP=${PHP_FPM_INSTALL_GMP}
             - INSTALL_PHPREDIS=${PHP_FPM_INSTALL_PHPREDIS}
             - INSTALL_MEMCACHED=${PHP_FPM_INSTALL_MEMCACHED}
             - INSTALL_OPCACHE=${PHP_FPM_INSTALL_OPCACHE}
             - INSTALL_EXIF=${PHP_FPM_INSTALL_EXIF}
             - INSTALL_AEROSPIKE=${PHP_FPM_INSTALL_AEROSPIKE}
             - INSTALL_MYSQLI=${PHP_FPM_INSTALL_MYSQLI}
             - INSTALL_PGSQL=${PHP_FPM_INSTALL_PGSQL}
             - INSTALL_PG_CLIENT=${PHP_FPM_INSTALL_PG_CLIENT}
             - INSTALL_TOKENIZER=${PHP_FPM_INSTALL_TOKENIZER}
             - INSTALL_INTL=${PHP_FPM_INSTALL_INTL}
             - INSTALL_GHOSTSCRIPT=${PHP_FPM_INSTALL_GHOSTSCRIPT}
             - INSTALL_LDAP=${PHP_FPM_INSTALL_LDAP}
             - INSTALL_SWOOLE=${PHP_FPM_INSTALL_SWOOLE}
             - INSTALL_IMAGE_OPTIMIZERS=${PHP_FPM_INSTALL_IMAGE_OPTIMIZERS}
             - INSTALL_IMAGEMAGICK=${PHP_FPM_INSTALL_IMAGEMAGICK}
           dockerfile: "Dockerfile-${PHP_VERSION}"
         volumes_from:
           - applications
         volumes:
           - ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
         expose:
           - "9000"
         depends_on:
           - workspace
         extra_hosts:
           - "dockerhost:${DOCKER_HOST_IP}"
         environment:
           - PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
         networks:
           - backend

   ### ...

   ### NGINX Server Container ##################################

       nginx:
         build:
           context: ./nginx
           args:
             - PHP_UPSTREAM_CONTAINER=${NGINX_PHP_UPSTREAM_CONTAINER}
             - PHP_UPSTREAM_PORT=${NGINX_PHP_UPSTREAM_PORT}
         volumes_from:
           - applications
         volumes:
           - ${NGINX_HOST_LOG_PATH}:/var/log/nginx
           - ${NGINX_SITES_PATH}:/etc/nginx/sites-available
         ports:
           - "${NGINX_HOST_HTTP_PORT}:80"
           - "${NGINX_HOST_HTTPS_PORT}:443"
         depends_on:
           - php-fpm
         networks:
           - frontend
           - backend

我们看到,刚才在 compose-dev.yml 定义的容器 applications 基于镜像:tianon/true。

容器 workspace 是一个操作台容器,可以通过配置文件安装各种需要的软件如:node, yarn 等等,它和 php-fpm、nginx 等容器都共用容器 applications 的数据卷,也就是说这几个写着 volumes_from: - applications 的容器中的路径 /var/www 都共用本机目录 ../www

4. 编辑 nginx 主机配置文件 laradock/nginx/sites

   server {

       listen 80 default_server;
       listen [::]:80 default_server ipv6only=on;

       server_name localhost;
       root /var/www/public;
       index index.php index.html index.htm;

       location / {
            try_files $uri $uri/ /index.php$is_args$args;
       }

       location ~ \.php$ {
           try_files $uri /index.php =404;
           fastcgi_pass php-upstream;
           fastcgi_index index.php;
           fastcgi_buffers 16 16k;
           fastcgi_buffer_size 32k;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
       }

       location ~ /\.ht {
           deny all;
       }

       location /.well-known/acme-challenge/ {
           root /var/www/letsencrypt/;
           log_not_found off;
       }
   }

因为 laradock 项目默认是为 laravel 框架配置的,所以我们看到网站的根目录默认设置为 /var/www/public 。这里我们把根目录修改为我们需要的路径,如 /var/www/your-project-name/public 。如果你的项目不是基于 laravel 的,则可以把 public 去掉,或做相应修改。

如果你使用 Chrome 63以上版本进行开发,就不要把 server_name 设置为.dev结尾 「为什么?」 https://laravel-news.com/chrome-63-now-forces-dev-domains-https。 你可以用 .localhost, .invalid, .test, .example 等代替,个人建议用 .localhost ,这样就不用修改容器的主机的 host 文件了,他会默认指向IP:127.0.0.1。

5. 运行容器(在laradock目录下)

docker-compose up -d nginx

结语

推荐:

到这里 laradock 的配置解析基本就介绍完毕了,小伙伴们赶快写个 hello world 试试吧,下一篇文章我们来讲讲如何通过容器,让我们的网站支持 https 协议。

qrcode

可以加作者「崔哥」交流

Copyright © coding01 2018 all right reserved,powered by Gitbook该文件修订时间: 2020-02-01 22:20:20

results matching ""

    No results matching ""