美文网首页docker
docker-compose一键解决环境困扰

docker-compose一键解决环境困扰

作者: 中v中 | 来源:发表于2018-09-04 18:00 被阅读29次

    上一篇利用 不同的命令来构建 nginx,mysql,php镜像 和 容器。 这样做比较麻烦,也很容易出错,参考文末(docker搭建lnmp(一)),当然可以写入 sh脚本来执行。但是可以通过 docker-compose 来达到效果,管理起来更加简单方便。

    docker-compose安装

    github地址:https://github.com/docker/compose

    curl -L https://github.com/docker/compose/releases/download/1.13.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    

    目录结构

    lemon@lemon:~/docker$ tree
    .
    ├── docker-compose.yml
    ├── mysql
    │   └── dockerfile
    ├── nginx
    │   ├── conf
    │   │   ├── nginx.conf
    │   │   └── vhost
    │   ├── dockerfile
    └── php7
        ├── conf
        │   └── php.ini
        └── dockerfile
    

    编写yml文件

    version : '3'
    services :
       mysql :
         build :
            context : ~/docker/mysql
            dockerfile : dockerfile
         ports :
            - "3306:3306"
         volumes :
            - ~/docker/mysql/data:/var/lib/mysql
         #初始密码
         environment :
            - MYSQL_ROOT_PASSWORD=123456
         container_name : mysql
         restart : always   
       php7 :
         build :
            context : ~/docker/php7
            dockerfile : dockerfile
         ports :
            - "9000:9000"
         volumes :
            - ~/web:/usr/local/nginx/html
            - ~/docker/php7/conf/php.ini:/usr/local/php7/lib/php.ini:ro
         container_name : php7
         links :
            - mysql
         restart : always
       nginx : 
         build :
            context : ~/docker/nginx
            dockerfile : dockerfile
         ports :
            - "80:80"
            - "433:433"
         volumes :
            - ~/docker/nginx/logs:/usr/local/nginx/logs
            - ~/web:/usr/local/nginx/html
            - ~/docker/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro
            - ~/docker/nginx/conf/vhost:/usr/local/nginx/conf/vhost:ro
         container_name : nginx
         links :
            - php7
         restart : always
    

    运行命令自动构建容器

    sudo docker-compose up -d
    

    查看状态

    emon@lemon:~/docker$ sudo docker-compose ps
    Name               Command               State                         Ports                       
    --------------------------------------------------------------------------------------------------
    mysql   docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp                            
    nginx   /usr/local/nginx/sbin/ngin ...   Up      0.0.0.0:433->433/tcp, 443/tcp, 0.0.0.0:80->80/tcp 
    php7    /usr/local/php7/sbin/php-f ...   Up      0.0.0.0:9000->9000/tcp                            
    

    具体文件查看地址:https://github.com/zhaocong222/docker-lnmp

    最后

    有了 Docker 之后,搭建环境从未有过如此简单。

    纠正原文错误:
    修改php7的dockerfile为(swoole和compose安装错误)

    FROM centos:latest
    MAINTAINER lemon <lemon616@outlook.com>
    
    ENV TZ "Asia/Shanghai"
    
    #安装扩展包
    RUN yum install -y epel-release
    
    #安装依赖
    RUN yum -y update && \
        yum install -y wget gcc automake autoconf libtool make gcc-c++ vixie-cron  file openssl-devel sharutils zip  bash vim cyrus-sasl-devel libmemcached libmemcached-devel libyaml libyaml-devel unzip libvpx-devel openssl-devel ImageMagick-devel autoconf tar gcc libxml2-devel libmcrypt mcrypt mhash libmcrypt-devel libxml2 libxml2-devel bzip2 bzip2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel bison libtool-ltdl-devel net-tools
    
    RUN yum install -y \
            git zlib zlib-devel glibc glibc-devel \
        glib2 glib2-devel \
        ncurses curl openssl-devel \
        gdbm-devel db4-devel libXpm-devel \
        libX11-devel gd-devel gmp-devel \
        readline-devel libxslt-devel \
        expat-devel xmlrpc-c xmlrpc-c-devel \
        libicu-devel  php-devel \
         yum clean all
    
    #install php
    RUN cd /tmp && \
         wget http://cn.php.net/distributions/php-7.1.5.tar.gz && \
         tar -zxvf php-7.1.5.tar.gz && \
         cd php-7.1.5 && \
         ./configure --prefix=/usr/local/php7 \
        --with-mysql-sock --with-mysqli \
        --enable-fpm  --enable-soap \
        --with-libxml-dir --with-openssl \
        --with-mcrypt --with-mhash \
        --with-pcre-regex  --with-zlib \
        --enable-bcmath --with-iconv \
        --with-bz2 --enable-calendar \
        --with-curl --with-cdb --enable-dom \
        --enable-exif --enable-fileinfo \
        --enable-filter --with-pcre-dir \
        --enable-ftp --with-gd \
        --with-openssl-dir --with-jpeg-dir \
        --with-png-dir --with-zlib-dir \
        --with-freetype-dir \
        --enable-gd-native-ttf \
        --enable-gd-jis-conv --with-gettext \
        --with-gmp --with-mhash \
        --enable-json --enable-mbstring --enable-mbregex \
        --enable-mbregex-backtrack \
        --with-libmbfl --with-onig \
        --enable-pdo --with-pdo-mysql \
        --with-zlib-dir  --with-readline \
        --enable-session --enable-shmop \
        --enable-simplexml --enable-sockets \
        --enable-sysvmsg --enable-sysvsem \
        --enable-sysvshm --enable-wddx \
        --with-libxml-dir  --with-xsl \
        --enable-zip --enable-mysqlnd-compression-support \
        --with-pear --enable-intl --enable-pcntl \
        --enable-opcache && \
        make && make install
    
    #config
    RUN ln -sf /usr/local/php7/bin/php    /usr/local/bin/php && \
        ln -sf /usr/local/php7/bin/phpize /usr/local/bin/phpize && \
        ln -sf /usr/local/php7/bin/pecl   /usr/local/bin/pecl
    
    RUN cp /tmp/php-7.1.5/php.ini-development /usr/local/php7/lib/php.ini && \
        cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf && \
        cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
    
    
    #配置php
    RUN sed -i -e 's/listen = 127.0.0.1:9000/listen = 9000/' /usr/local/php7/etc/php-fpm.d/www.conf
    
    #install swoole
    #RUN pecl install swoole
    RUN cd /tmp && \
         wget https://github.com/swoole/swoole-src/archive/swoole-1.7.6-stable.tar.gz && \
         tar -zxvf swoole-1.7.6-stable.tar.gz && \
         cd swoole-src-swoole-1.7.6-stable && \
         phpize && \
         ./configure && \
         make  && make install
    
    #add extension
    
    #安装composer.phar
    RUN cd /tmp
    RUN wget https://getcomposer.org/composer.phar && \
        mv composer.phar /usr/local/bin/composer && \
        chmod 755 /usr/local/bin/composer
    
    EXPOSE 9000
    
    #启动php
    ENTRYPOINT ["/usr/local/php7/sbin/php-fpm", "-F", "-c", "/usr/local/php7/lib/php.ini"]
    
    

    参考:

    docker搭建lnmp(一)

    docker搭建lnmp(二)
    (https://github.com/zhaocong222/docker-lnmp)

    https://github.com/helingfeng/Docker-LNMP

    https://github.com/yeszao/dnmp

    How to use PHP, Apache, MySQL within Docker containers

    相关文章

      网友评论

        本文标题:docker-compose一键解决环境困扰

        本文链接:https://www.haomeiwen.com/subject/qornwftx.html