Docker学习第五天——Dockerfile+lnmp+Discuzz搭建

前言

        本文为本人学习过程中搜集网络资料并自行实验操作所得,中间可能基于个人配置会有部分误差,欢迎一起学习探讨、共同攀登进步。

一、基本架构、环境

服务器 IP地址 所需软件包 所需配置文件
宿主机 192.168.233.20 couchbase/centos7-systemd镜像、docker容器
nginx容器 172.17.0.2 nginx-1.12.0.tar.gz、Discuz_X3.3_SC_UTF8.zip Dockerfile、nginx.conf
mysql容器 172.17.0.3 mysql-boost-5.7.20.tar.gz Dockerfile、my.cnf
php容器 172.17.0.4 php-7.1.10.tar.bz2 Dockerfile、php.ini、php-fpm.conf、www.conf

二、实验步骤

2.1 宿主机环境搭建

#关闭防火墙
[root@goodwood ~]# setenforce 0

#安装docker并启动
[root@goodwood ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@goodwood ~]# yum install -y docker-ce
[root@goodwood ~]# systemctl enable docker

#修改国内源仓库
 [root@goodwood ~]# vim /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://i4gs4xxq.mirror.aliyuncs.com"]
}

#获取centos7镜像源
[root@goodwood ~]# docker search centos
NAME                                         DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
centos                                       DEPRECATED; The official build of CentOS.        7573      [OK]       
kasmweb/centos-7-desktop                     CentOS 7 desktop for Kasm Workspaces             36                   
bitnami/centos-base-buildpack                Centos base compilation image                    0                    [OK]
bitnami/centos-extras-base                                                                    0                    
couchbase/centos7-systemd                    centos7-systemd images with additional debug…   7                    [OK]
continuumio/centos5_gcc5_base                                                                 3                
[root@goodwood ~]# docker pull couchbase/centos7-systemd 
[root@goodwood ~]# mkdir -p {nginx,mysql,php}_lnmp

2.2 创建nginx容器

[root@goodwood ~]# cd /root/nginx_lnmp

#通过rz命令从本地获取nginx-1.16.0.tar.gz Discuz_X3.3_SC_UTF8.zip源码包

#编写Dockerfile配置文件
[root@goodwood nginx_lnmp]# vim Dockerfile
FROM couchbase/centos7-systemd     #指定基础镜像
RUN yum install -y pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx     #安装依赖
ADD nginx-1.16.0.tar.gz /usr/local/src/   #获取并解压nginx源码包
WORKDIR /usr/local/src/nginx-1.16.0        #指定工作目录,类似cd
RUN ./configure \                        #配置编译安装
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module;make -j 2 && make install
ENV PATH /usr/local/nginx/sbin:$PATH        #设置优化nginx命令路径
ADD nginx.conf /usr/local/nginx/conf/        #获取nginx配置文件
ADD Discuz_X3.3_SC_UTF8.zip /usr/local/nginx/html    #获取Discuzz网站程序
WORKDIR /usr/local/nginx/html                #指定工作目录
RUN unzip Discuz_X3.3_SC_UTF8.zip;chmod 777 -R ./    #解压Discuzz压缩包
EXPOSE 80                                #暴露80端口
VOLUME [ "/usr/local/nginx/html/" ]        #指定挂载点,给php容器使用
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]    #指定容器启动时默认启动nginx

#创建nginx配置文件
[root@goodwood nginx_lnmp]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   172.17.0.4:9000;    #此处为php容器对应IP
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

#创建nginx镜像
[root@goodwood nginx_lnmp]# docker build -t nginx:lnmp .

#启动nginx容器
[root@goodwood nginx_lnmp]# docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g nginx:lnmp

#验证nginx容器是否搭建成功
[root@goodwood nginx_lnmp]# curl 192.168.233.20/upload

301 Moved Permanently

301 Moved Permanently


nginx/1.16.0

2.3 创建mysql容器

[root@goodwood nginx_lnmp]# cd /root/mysql_lnmp/
[root@goodwood mysql_lnmp]# vim Dockerfile
FROM couchbase/centos7-systemd       #指定基础镜像
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make;useradd -M -s /sbin/nologin  mysql            #安装依赖并创建mysql用户
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/    #获取并解压mysql源码包
WORKDIR /usr/local/src/mysql-5.7.20                #设置工作目录
RUN cmake \                                      #源码包安装
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j 4 && make install
RUN chown -R mysql:mysql /usr/local/mysql/        #更改mysql目录属主属组
ADD my.cnf /etc/                        #获取mysql新配置文件
RUN chown mysql:mysql /etc/my.cnf        #更改配置所属
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH    #优化命令路径
WORKDIR /usr/local/mysql/                #更改工作目录至mysql下
RUN bin/mysqld \                        #mysql初始化
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306                        #暴露mysql的3306端口
CMD /usr/local/mysql/bin/mysqld && systemctl enable mysqld     #容器启动时启动mysql

#编写mysql配置文件
[root@goodwood mysql_lnmp]# vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

#生成mysql镜像文件并启动镜像容器
[root@goodwood mysql_lnmp]# docker build -t mysql:lnmp .
[root@goodwood mysql_lnmp]# docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql mysql:lnmp

2.4 创建php容器

[root@goodwood mysql_lnmp]# cd /root/php_lnmp/

#通过rz获取本地php-7.1.10.tar.bz2源码包

#编写Dockerfile配置文件
[root@goodwood php_lnmp]# vim Dockerfile 
FROM couchbase/centos7-systemd            #指定基础镜像
RUN yum install -y gd \                      #安装依赖环境
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/   #获取并解压php源码包
WORKDIR /usr/local/src/php-7.1.10        #指定工作路径
RUN ./configure \                        #php源码安装
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip ; make  ; make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
CMD /usr/local/php/sbin/php-fpm -F

#创建php配置文件
[root@goodwood php_lnmp]# vim php.ini
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]
vim php-fpm.conf
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf

[root@goodwood php_lnmp]# vim www.conf
[www]
user = nginx
group = nginx
listen = 172.18.0.4:9000  #php容器ip
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

#构建php容器镜像
[root@goodwood php_lnmp]# docker build -t php:lnmp .
[root@goodwood php_lnmp]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
php                         lnmp      d8121da712a7   5 hours ago     1.58GB
mysql                       lnmp      9dca9ba6dbd8   6 hours ago     10.3GB
nginx                       lnmp      e241e0e16ddd   6 hours ago     826MB

#根据php镜像创建容器并启动
[root@goodwood php_lnmp]# docker run -itd --name php -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp
[root@goodwood php_lnmp]# docker ps -a
CONTAINER ID   IMAGE        COMMAND                   CREATED       STATUS                      PORTS                                       NAMES
cd84238895bd   php:lnmp     "/bin/sh -c '/usr/lo…"   5 hours ago   Up 1 minutes               0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   php
84ef1704cb94   mysql:lnmp   "/bin/sh -c '/usr/lo…"   5 hours ago   Up 2 minutes                                                mysql
b3209e48e224   nginx:lnmp   "/usr/local/nginx/sb…"   5 hours ago   Up 3 minutes               0.0.0.0:80->80/tcp, :::80->80/tcp           nginx

2.5 进入mysql容器进行授权

[root@goodwood ~]# docker exec -it mysql容器的ID bash
#进入mysql容器进行授权
mysql -u root -p
	create database bbs;
    grant all on *.* to 'bbs'@'localhost' identified by '123456';
	flush privileges;

三、验证

        访问:http://192.168.233.20/upload/install/

        可看到:

Docker学习第五天——Dockerfile+lnmp+Discuzz搭建_第1张图片

Docker学习第五天——Dockerfile+lnmp+Discuzz搭建_第2张图片

 Docker学习第五天——Dockerfile+lnmp+Discuzz搭建_第3张图片

四、经验总结

  • 由dockerfile生成镜像时无法进行yum安装,或者说docker无法连接外网 (解决:重启docker、宿主机打开转发功能)
  • 进行编译、数据库初始化操作时报错(解决:根据报错提示安装对应依赖程序,如make、openssl等)
  • 生成镜像时报错没有足够硬盘空间,是因为之前报错排错过程产生了过多缓存(解决:到/var/lib/docker/overlay2/目录下清除不使用的缓存文件、扩容硬盘空间、恢复快照)

        

        

         

你可能感兴趣的:(docker,学习,运维)