【Docker】制作LNMP镜像

1、概述

2、制作 LNMP 镜像

2.1、非常有用的清理命令
sudo apt-get autoclean          #清理旧版本的软件缓存
sudo apt-get clean              #清理所有软件缓存
sudo apt-get autoremove         #删除系统不再使用的孤立软件
2.2、包管理的临时文件目录
/var/cache/apt/archives             #已下载完的 deb 包目录
/var/cache/apt/archives/partial     #未下载完的 deb 包目录
2.3、nginx 1.10.3
  • 安装目录 /etc/nginx/

  • 配置目录 /etc/nginx/sites-enabled/

  • 网站目录 /var/www/html/

  • 启动停止 service nginx start | service nginx stop

  • 配置PHP vim /etc/nginx/sites-enabled/default

location ~ \.php$ {
	include snippets/fastcgi-php.conf;
	# With php7.0-cgi alone:
	# fastcgi_pass 127.0.0.1:9000;
	# With php7.0-fpm:
	fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
2.4、php 7.1.18
  • 安装(配置)目前 /etc/php/7.1/
vim /etc/php/7.1/pool.d/www.conf
listen = /run/php/php7.1-fpm.sock
  • 启动停止 service php7.1-fpm start | service php7.1-fpm stop
2.5、mysql 5.7.22
  • 安装目录 /etc/mysql/

  • root密码:空

  • 启动停止 service mysql start | service mysql stop

2.6、ubuntu上用apt安装php7.1的扩展

非常简单,一条命令,都不用改配置,如下:

apt-get -y install php7.1-mbstring
apt-get -y install php7.1-curl
apt-get -y install php7.1-gd
apt-get -y install php7.1-intl
apt-get -y install php7.1-imagick
apt-get -y install php7.1-imap
apt-get -y install php7.1-mcrypt
apt-get -y install php7.1-common
apt-get -y install php7.1-pspell
apt-get -y install php7.1-recode
apt-get -y install php7.1-snmp
apt-get -y install php7.1-sqlite
apt-get -y install php7.1-tidy
apt-get -y install php7.1-xmlrpc
apt-get -y install php7.1-xsl
apt-get -y install php7.1-memcached
apt-get -y install php7.1-bz2
apt-get -y install php7.1-calendar
apt-get -y install php7.1-ctype
apt-get -y install php7.1-exif
apt-get -y install php7.1-fileinfo
apt-get -y install php7.1-ftp
apt-get -y install php7.1-gettext
apt-get -y install php7.1-gmp
apt-get -y install php7.1-iconv
apt-get -y install php7.1-json
apt-get -y install php7.1-shmop
apt-get -y install php7.1-simplexml
apt-get -y install php7.1-sockets
apt-get -y install php7.1-tokenizer
apt-get -y install php7.1-zip

假如你的版本是php7.0

apt-get install php7.0-mbstring

3、制作lnmp环境的docker镜像

3.1、安装软件如下
  • 中文语言包

  • 时区数据

  • vim

  • nginx \ php \ mysql

  • php-redis \ php-swoole

3.2、Dockerfile文件
FROM ubuntu:16.04
MAINTAINER zobeen [zobeen@163.com]

# 设置apt-get安装时采用静默安装
ENV DEBIAN_FRONTEND noninteractive

RUN /usr/bin/apt-get update

# 安装中文语言包
RUN /usr/bin/apt-get -y install language-pack-zh-hans
RUN /bin/echo 'export LANG="zh_CN.UTF-8"' >> /root/.profile
RUN /bin/echo 'export LC_ALL="zh_CN.UTF-8"' >> /root/.profile

# 设置系统时区
RUN /usr/bin/apt-get install tzdata
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN /bin/echo 'Asia/Shanghai' > /etc/timezone

RUN /usr/bin/apt-get -y install vim
RUN /usr/bin/apt-get -y install nginx
RUN /usr/bin/apt-get -y install software-properties-common
RUN LC_ALL=C.UTF-8 /usr/bin/apt-add-repository -y ppa:ondrej/php
RUN /usr/bin/apt-get update
RUN /usr/bin/apt-get -y install php7.1
RUN /usr/bin/apt-get -y install php7.1-fpm
RUN /usr/bin/apt-get -y install php7.1-dev
RUN /usr/bin/apt-get -y install mysql-server
RUN /usr/bin/apt-get -y install mysql-client
RUN /usr/bin/apt-get -y install wget
RUN /usr/bin/apt-get -y install net-tools
RUN /usr/bin/apt-get -y install netcat
RUN /usr/bin/apt-get -y install telnet

# 安装php扩展
RUN /usr/bin/apt-get -y install php7.1-mysql
RUN /usr/bin/apt-get -y install php7.1-mbstring
RUN /usr/bin/apt-get -y install php7.1-curl
RUN /usr/bin/apt-get -y install php7.1-gd
RUN /usr/bin/apt-get -y install php7.1-intl
RUN /usr/bin/apt-get -y install php7.1-imagick
RUN /usr/bin/apt-get -y install php7.1-imap
RUN /usr/bin/apt-get -y install php7.1-mcrypt
RUN /usr/bin/apt-get -y install php7.1-common
RUN /usr/bin/apt-get -y install php7.1-pspell
RUN /usr/bin/apt-get -y install php7.1-recode
RUN /usr/bin/apt-get -y install php7.1-snmp
RUN /usr/bin/apt-get -y install php7.1-sqlite
RUN /usr/bin/apt-get -y install php7.1-tidy
RUN /usr/bin/apt-get -y install php7.1-xmlrpc
RUN /usr/bin/apt-get -y install php7.1-xsl
RUN /usr/bin/apt-get -y install php7.1-memcached
RUN /usr/bin/apt-get -y install php7.1-bz2
RUN /usr/bin/apt-get -y install php7.1-calendar
RUN /usr/bin/apt-get -y install php7.1-ctype
RUN /usr/bin/apt-get -y install php7.1-exif
RUN /usr/bin/apt-get -y install php7.1-fileinfo
RUN /usr/bin/apt-get -y install php7.1-ftp
RUN /usr/bin/apt-get -y install php7.1-gettext
RUN /usr/bin/apt-get -y install php7.1-gmp
RUN /usr/bin/apt-get -y install php7.1-iconv
RUN /usr/bin/apt-get -y install php7.1-json
RUN /usr/bin/apt-get -y install php7.1-shmop
RUN /usr/bin/apt-get -y install php7.1-simplexml
RUN /usr/bin/apt-get -y install php7.1-sockets
RUN /usr/bin/apt-get -y install php7.1-tokenizer
RUN /usr/bin/apt-get -y install php7.1-zip

# 安装php-redis扩展
RUN /bin/mkdir -p /var/www/install_redis
RUN cd /var/www/install_redis
RUN /usr/bin/wget -P /var/www/install_redis http://pecl.php.net/get/redis-4.0.1.tgz
RUN /bin/tar -zxvf /var/www/install_redis/redis-4.0.1.tgz -C /var/www/install_redis
RUN cd /var/www/install_redis/redis-4.0.1 && /usr/bin/phpize7.1
RUN cd /var/www/install_redis/redis-4.0.1 && /var/www/install_redis/redis-4.0.1/configure  --with-php-config=/usr/bin/php-config
RUN /usr/bin/make -C /var/www/install_redis/redis-4.0.1
RUN /usr/bin/make  -C /var/www/install_redis/redis-4.0.1 install
RUN /usr/bin/make  -C /var/www/install_redis/redis-4.0.1 clean
RUN /bin/echo 'extension=redis.so' > /etc/php/7.1/mods-available/redis.ini
RUN /bin/ln -s /etc/php/7.1/mods-available/redis.ini /etc/php/7.1/cli/conf.d/20-redis.ini
RUN /bin/ln -s /etc/php/7.1/mods-available/redis.ini /etc/php/7.1/apache2/conf.d/20-redis.ini
RUN /bin/ln -s /etc/php/7.1/mods-available/redis.ini /etc/php/7.1/fpm/conf.d/20-redis.ini
RUN /bin/rm -rf /var/www/install_redis

# 安装swoole扩展
RUN /bin/mkdir -p /var/www/install_redis
RUN cd /var/www/install_redis
RUN /usr/bin/wget -P /var/www/install_redis http://pecl.php.net/get/swoole-2.2.0.tgz
RUN /bin/tar -zxvf /var/www/install_redis/swoole-2.2.0.tgz -C /var/www/install_redis
RUN cd /var/www/install_redis/swoole-2.2.0 && /usr/bin/phpize7.1
RUN cd /var/www/install_redis/swoole-2.2.0 && /var/www/install_redis/swoole-2.2.0/configure  --with-php-config=/usr/bin/php-config --enable-openssl
RUN /usr/bin/make -C /var/www/install_redis/swoole-2.2.0
RUN /usr/bin/make -C /var/www/install_redis/swoole-2.2.0 install
RUN /usr/bin/make -C /var/www/install_redis/swoole-2.2.0 clean
RUN /bin/echo 'extension=swoole.so' > /etc/php/7.1/mods-available/swoole.ini
RUN /bin/ln -s /etc/php/7.1/mods-available/swoole.ini /etc/php/7.1/cli/conf.d/20-swoole.ini
RUN /bin/ln -s /etc/php/7.1/mods-available/swoole.ini /etc/php/7.1/apache2/conf.d/20-swoole.ini
RUN /bin/ln -s /etc/php/7.1/mods-available/swoole.ini /etc/php/7.1/fpm/conf.d/20-swoole.ini
RUN /bin/rm -rf /var/www/install_redis

# 配置nginx的default配置
COPY default.nginx /etc/nginx/sites-available/default

# 初始化shell脚本
COPY init.sh /docker/init.sh
RUN /bin/chmod +x /docker/init.sh

# 非常有用的清理命令
RUN /usr/bin/apt-get autoclean
RUN /usr/bin/apt-get clean
RUN /usr/bin/apt-get autoremove

# 启动容器时,执行下面脚本
CMD ["/docker/init.sh"]
3.3、default.nginx文件
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #   
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #   
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #   
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #   
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #   
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / { 
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }   

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #   
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php7.0-cgi alone:
                # fastcgi_pass 127.0.0.1:9000;
                # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}
3.4、init.sh文件
#!/bin/bash

/usr/sbin/service nginx start
/usr/sbin/service php7.1-fpm start
/usr/sbin/service mysql start
/usr/bin/tail -f /var/log/nginx/error.log  # 要想容器不启动后就停止,这句很重要。当然你也可以使用其他命令来阻塞。
3.5、制作镜像

将上面三个文件放在同一个目录下,进入该目录,执行以下命令,开始制作镜像:

docker build -t zobeen/ubuntu16.04-lnmp:v1.0.0 .

4、镜像的使用

4.1、拉取镜像文件
docker pull zobeen/ubuntu16.04-lnmp:v1.0.0
4.2、实例化镜像容器
docker run -d -p 8000:80 -p 3307:3306 --name=lnmp -v /home/abin/docker/swoft/conf:/etc/nginx/sites-enabled -v /home/abin/docker/swoft/www:/var/www/ zobeen/ubuntu16.04-lnmp:v1.0.0

注意:

  • 将容器中的端口映射到宿主机上;
  • 将容器中的网站目录映射到宿主机上;
  • 将容器中的nginx的配置目录映射到宿主机上;
4.3、管理后台运行的容器
docker exec -i -t lnmp /bin/bash

进入到容器的命令行模式后,便可以执行诸如重启nginx服务,以使nginx的配置生效;重启mysql服务;重启php-fpm服务等。

5、实际应用该镜像:部署thinkcmf

5.1、创建要映射到 docker 实例的配置和工作目录
mkdir -p /home/abin/docker/workstation/conf
mkdir -p /home/abin/docker/workstation/www/thinkcmf
cd /home/abin/docker/workstation
5.2、添加 nginx 网络服务配置文件

打开下面的配置文件:

vim /home/abin/docker/workstation/conf/onethink.conf

添加如下代码:

server {
    listen       8001;

    location / {
        root   /var/www/onethink/public;
        index  index.html index.htm index.php;

        if (!-e $request_filename) {
                rewrite ^/index.php(.*)$ /index.php?s=$1 last;
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
        }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;   
    }

    location ~ \.php$ {
        root           /var/www/onethink/public;
        fastcgi_pass   unix:/run/php/php7.1-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht { 
        deny  all;
    }
}
5.3、实例化容器
docker run -d -p 8001:8001 -v /home/abin/docker/workstation/conf:/etc/nginx/sites-enabled -v /home/abin/docker/workstation/www:/var/www zobeen/ubuntu16.04-lnmp:v1.0.0

转载于:https://my.oschina.net/zobeen/blog/2250136

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值