`
xieye
  • 浏览: 805336 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

利用docker构建完整centos环境开发php

阅读更多
利用docker构建完整centos环境开发php

本人原创文章,转载请注明出处。注意,本文的开发环境是win7,使用docker-toolbox

当前日期:2019-06-16

概述
虽然已经有开发php的docker套件,但是有时仍然想在独立的centos环境下开发php,当然,宿主机是windows电脑。
此时,应用本文,可以方便的开发php,把自己的电脑当成centos。

要点:
阿里云的docker管理后台地址是特别的:
https://account.aliyun.com/login/login.htm?oauth_callback=https%3A%2F%2Fcr.console.aliyun.com%2F%3Fspm%3D5176.1971733.0.2.sezm8g%26accounttraceid%3Dca51394a-9a94-4bf4-af57-e60459167886#/imageSearch

https://cr.console.aliyun.com/cn-hangzhou/new

1、虽然docker教程上说RUN指令应该少些,可是实际构建时,每次失败只会从最近的RUN指令重新构建。所以,当构建命令多时,适当分隔成几个RUN指令,会很有利。
2、remi是一个重要的仓库,且其官网有中国镜像,但本文未考虑。
3、mysql那个有点麻烦,所以,我使用套件里的mysql。而不把mysql装在centos里。
4、利用docker-compose套件安装使用镜像的好处是,进入容器,启动容器的参数都可以配置在docker-compose.yml 这个文件中,明显比单独使用docker 命令好使。
5、安装centos的开发环境,时间较长,得有心理准备。但装好后,使用是很方便的。
6、本文构建未考虑清理,考虑是更好的。
7、利用本文构建php开发环境时,现在有两种方案,一种是使用标准的、docker官方推荐的每个进程使用一个容器的方法,本文代码里有php56和php72,另一种方法是使用独立的centos开发php,当然,这两种情况都需要使用mysql-db。
8、为了便于记住键盘命令,把.bash_history映射到共享文件夹。
9、.bashrc文件可以随意修改,自行配置喜欢的centos环境。
10、nginx的配置,和php的配置文件,可以随意修改。只需重新up镜像(不用重装)即可,太方便啦!!

命令列表
从下载地址clone后,首先得clone在 虚拟机的共享目录下。假设code是共享目录。在c盘根目录。


git clone https://github.com/xieye114/docker_multiple_php.git code

然后进入docker,
cd /code/myapp/build

下面这个文件从网上拷贝过来。进行构建centos。等构建完centos,再构建其他,其他都快。
快的原因是原始镜像较完备,而我是从标准的centos7.4的镜像构建的,差很多东西。
/code/docker-compose build centos
这一步很长时间,如果网络不顺畅,可以用ctrl+c强行中止,再重复执行。联通网较好。

等执行完之后
/code/docker-compose up -d centos
或者
/code/docker-compose up -d centos mysql-db
然后
/code/docker-compose exec centos bash
现在就可以愉快的在centos里玩耍了!

下载安装
https://github.com/xieye114/docker_multiple_php
里面有具体的安装步骤,但centos未来得及写进去。

部分源码
docker-compose.yml文件

services:

  centos:
    build: ./centos
    privileged: true
    ports:
      - "80:80"
      - "443:443"
      - "8001:8001"
    volumes:
      - ../../workspace:/var/www:rw
      - ../data/centos_composer/cache:/root/.composer/cache:rw
      - ../data/centos_composer_vendor:/root/vendor:rw
      - ../data/centos_redis:/data/centos_redis:rw
      - ./centos/centos_file/.bash_history:/root/.bash_history:rw
      - ./centos/centos_file/.bashrc:/root/.bashrc:rw
      - ./centos/centos_file/start:/bin/start:rw
      - ./centos/centos_file/crontab:/etc/crontab:rw
      - ./centos/php/php.ini:/opt/remi/php72/root/etc/php.ini:ro
      - ./centos/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./centos/nginx/conf.d:/etc/nginx/conf.d:ro
    restart: always
    command: "/usr/sbin/init"



centos的Dockerfile

FROM centos:7.4.1708

WORKDIR /root

RUN curl --silent --location https://rpm.nodesource.com/setup_10.x | bash - \
  && yum install -y nodejs \
  && curl -o- -L https://yarnpkg.com/install.sh | bash 
  

RUN yum install -y wget epel-release \
  && yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm \
  && yum install -y yum-utils \
  && yum-config-manager --enable remi-php72 \
  && yum install -y nginx php72 redis zip unzip rsync crontabs vim ntp supervisor 
  
RUN yum install -y php72-php-devel  php72-php-fpm  php72-php-mbstring php72-php-memcache php72-php-redis  php72-php-mysqli php72-php-mysqlnd  php72-php-pdo  php72-php-bcmath php72-php-dom php72-php-gd php72-php-gmp php72-php-igbinary php72-php-imagick   php72-php-mcrypt  php72-php-pdo_mysql php72-php-posix php72-php-simplexml  php72-php-opcache php72-php-xsl php72-php-xmlwriter php72-php-xmlreader php72-php-xml php72-php-swoole php72-php-zip php72-php-phalcon \
  && ln -s /usr/bin/php72 /usr/bin/php \
  && sed -i 's/user\ =\ apache/user\ =\ nginx/g' /etc/opt/remi/php72/php-fpm.d/www.conf \
  && sed -i 's/group\ =\ apache/group\ =\ nginx/g' /etc/opt/remi/php72/php-fpm.d/www.conf 
  
  
RUN curl https://setup.ius.io | sh \
  && yum remove -y git \
  && yum install -y git2u \
  && git config --global core.autocrlf false \
  && git config --global core.safecrlf false \
  && git config --global user.name 'test' \
  && git config --global user.email 'test@qq.com' \
  && curl -s http://getcomposer.org/installer | php \
  && mv composer.phar /usr/local/bin/ \
  && chmod a+x /usr/local/bin/composer.phar \
  && ln -s /usr/local/bin/composer.phar  /usr/local/bin/composer \
  && composer config -g repo.packagist composer https://packagist.laravel-china.org 

EXPOSE 80 443 6379


.bashrc 文件如下

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi


PS1='[\u@centos in docker \W]\$ '

timedatectl set-timezone Asia/Shanghai
localectl set-locale LANG=zh_CN.utf8

export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
yarn config set registry https://registry.npm.taobao.org

systemctl start php72-php-fpm
systemctl start redis
systemctl start nginx
systemctl start crond
systemctl start ntpd
# systemctl start supervisord


0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics