zoukankan      html  css  js  c++  java
  • Dockerfile编写制作centos7.7+php5.6+nginx镜像

    1、新建一个dockerfile文件

    touch test.Dockerfile

    2、找一个centos基础镜像

     可以去docker hub上寻找,链接:docker-hub 在搜索框搜索'centos',或者直接点击docker-hub-centos。里面有从centos 6 到最新的centos 8 等各版本的基础镜像,此处我选择版本:centos:7.7.1908,当然也可以选择其它版本。

     所以,在test.Dockerfile第一行为:

    FROM centos:7.7.1908

    3、MAINTAINER 指定作者  (非必要)

    MAINTAINER wu "471515***@qq.com"

    4、使用RUN命令安装php5.6

      因为我采用yum安装,所在需要先配置和更新yum源,然后再进行php5.6安装,。原本在centos的命令如下:

    yum install -y epel-release
    yum -y install wget
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
    yum -y install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-mysql php-odbc php-pdo php-mssql php-mysqli php-gd php-xml php-pear php-bcmath php-pecl-swoole php-pecl-redis php-pecl-mongo  --skip-broken

      其中yum后都加要-y,避免在执行dockerfile安装时因等待确认而失败。在dockerfile中为了避免太多层,可以使用‘&&’将多个使命合并。最终dockerfile中增加命令如下:

    RUN yum install -y epel-release 
        && yum -y install wget 
        && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 
        && rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm 
        && yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-mysql php-odbc php-pdo php-mssql php-mysqli php-gd php-xml php-pear php-bcmath php-pecl-swoole php-pecl-redis php-pecl-mongo  --skip-broken 

      注意:其中php扩展可以按需求增减。

    5、安装php-fpm

      原本在centos中执行的命令如下:

    yum install -y --enablerepo=remi --enablerepo=remi-php56 php-fpm 

      则在dockerfile增加:

    && yum install -y --enablerepo=remi --enablerepo=remi-php56 php-fpm 

    6、安装nginx

     原本在centos中执行的命令如下:

    yum install -y nginx

      则在dockerfile增加:

    && yum install -y nginx 

    7、添加www用户组和用户并安装vim

      原本在centos中执行的命令如下:

    groupadd www
    useradd -g www -s /sbin/nologin www
    yum install vim -y

      则在dockerfile增加:

        && groupadd www 
        && useradd -g www -s /sbin/nologin www 
        && yum install vim -y

    注意:每一行的 是为了合并命令之后的换行,所以最后一行不需要 。

    8、修改nginx配置

      此处用COPY命令直接将配置好的nginx.conf复制到镜像里面:

    COPY nginx.conf /etc/nginx/nginx.conf

      对于nginx.conf文件,增加了对php文件的解析:

    location ~ .php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
                include        fastcgi_params;
            }

    9、加入测试用的index.php文件

      此处用COPY命令直接将index.php文件复制到镜像里相应的目录:

    COPY index.php /usr/share/nginx/html/index.php

      index.php文件中内容为:

    <?php
    phpinfo();

    10、添加容器启动脚本文件run.sh

      为了让最终创建的容器在启动时能启动nginx和php-fpm服务,需要建一个run.sh,用于在启动时执行,其中命令为:

    #!/bin/bash
    
    nginx
    php-fpm
    tail -f /dev/null

      其中最后一行的'tail -f /dev/null',是为了避免在CMD执行完这个命令脚本后主进程退出时将导致容器也退出,才加的阻塞操作,因为这个命令永远都执行不完。然后在dockerfile文件中用COPY命令直接将run.sh文件复制到镜像里相应的目录:

    COPY run.sh /mnt/run.sh

      特别注意:run.sh必须要有执行权限!

    11、开放80端口

    EXPOSE 80

    12、CMD命令执行run.sh

    CMD ["/mnt/run.sh"]

    13、使用docker build命令构建镜像

      将上述步骤中建好的四个文件放在同一目录下,然后使用命令构建:

      

    sudo docker build -t centos7.7:v2 -f test.Dockerfile .

      等待几分钟,镜像就构建好了。因为涉及到下载安装过程,网络不好可能失败,可以重新运行命令。

      

    14、使用docker run创建一个容器并运行

    sudo docker run -d --name test -p 8010:80 -it centos7.7:v2

      然后,在浏览器中访问:http://127.0.0.1:8010/,即出现熟悉的页面:  

      

     15、附录:各文件中最终内容

      test.dockerfile文件内容如下:

    FROM centos:7.7.1908
    MAINTAINER wu "471515***@qq.com"
    
    RUN yum install -y epel-release 
        && yum -y install wget 
        && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 
        && rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm 
        && yum install -y --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-mysql php-odbc php-pdo php-mssql php-mysqli php-gd php-xml php-pear php-bcmath php-pecl-swoole php-pecl-redis php-pecl-mongo  --skip-broken 
        && yum install -y --enablerepo=remi --enablerepo=remi-php56 php-fpm 
        && yum install -y nginx 
        && groupadd www 
        && useradd -g www -s /sbin/nologin www 
        && yum install vim -y
    COPY nginx.conf /etc/nginx/nginx.conf
    COPY index.php /usr/share/nginx/html/index.php
    COPY run.sh /mnt/run.sh
    EXPOSE 80
    CMD ["/mnt/run.sh"]

      nginx.conf文件内容如下:

    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    
    include /usr/share/nginx/modules/*.conf;
    
    events {
        worker_connections 1024;
    }
    
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
        include /etc/nginx/conf.d/*.conf;
    
        server {
            listen       80;
            server_name  _;
            root         /usr/share/nginx/html;.
            include /etc/nginx/default.d/*.conf;
        
            location / {
            root   html;
            index  index.php index.html index.htm;
            }
        
            location ~ .php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
                include        fastcgi_params;
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }
    }
  • 相关阅读:
    [转]TeeChart经验总结 5.Axis
    查询
    [转]VS2010安装说明及所有安装出错的解决办法
    [转]游标
    [转]在C#中实现串口通信
    delphi日期的使用
    Http(1)
    表的操作
    存储过程
    CKeditor
  • 原文地址:https://www.cnblogs.com/aotudexiaowu/p/13186475.html
Copyright © 2011-2022 走看看