Docker容器

Dockerfile项目——构建LNMP

本文主要是介绍Dockerfile项目——构建LNMP,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录

一.环境准备

二.构建Nginx镜像

三.构建MySQL容器

四.构建PHP镜像

五.验证


一.环境准备

关闭防火墙

systemctl stop firewalld 
systemctl disable firewalld
setenforce 0

先自定义网络,再使用指定IP运行docker

是LNMP各组件处于同一网段

docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1"  mynetwork

Docker各组件的ip为:

Nginx172.18.0.10
MySQL172.18.0.20
PHP172.18.0.30

此实验所需的软件有:

nginx-1.12.0.tar

wordpress-4.9.4-zh_CN.tar

mysql-boost-5.7.20.tar

php-7.1.10.tar

二.构建Nginx镜像

1.创建nginx的工作目录

mkdir /opt/nginx
cd /opt/nginx/

2.上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目录中

3.编辑Dockerfile配置文件

vim Dockerfile

FROM centos:7
MAINTAINER this is nginx image <wl>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]

4.准备Dockerfile文件中所需要的其他配置文件

vim nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   172.18.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

5.构建nginx镜像

docker build -t nginx:lnmp .

6.启动Nginx容器

docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp

三.构建MySQL容器

1.创建MySQL工作目录

mkdir /opt/mysqld
cd /opt/mysqld

2.上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中

3.编辑Dockerfile文件

vim Dockerfile

FROM centos:7
MAINTAINER this is mysql image <wl>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
RUN useradd -M -s /sbin/nologin  mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
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 && make install
RUN chown -R mysql:mysql /usr/local/mysql/
RUN rm -rf /etc/my.cnf
ADD my.cnf /etc/
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/
RUN bin/mysqld \
--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
ADD run.sh /usr/local/src
RUN chmod 755 /usr/local/src/run.sh
RUN sh /usr/local/src/run.sh
#CMD ["/usr/sbin/init"]

4.编辑mysql的配置文件

vim my.cnf

[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
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
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

5.编辑MySQL的启动脚本

vim run.sh

#!/bin/bash
/usr/local/mysql/bin/mysqld
systemctl enable mysqld

6.构建MySQL镜像

docker build -t mysql:lnmp .

7.启动MySQL镜像

docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:centos /usr/sbin/init

四.构建PHP镜像

1.创建工作目录

mkdir /opt/php
cd /opt/php

2.上传 php-7.1.10.tar.bz2 到 /opt/php 目录中

3.编辑Dockerfile文件

vim Dockerfile 

FROM centos:7
MAINTAINER this is php image <xcc>
RUN yum -y install 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
RUN useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--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
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]

4.准备Dockerfile需要的PHP配置文件

(1)www.conf

www.conf-Docker-LNMP的php配置文件-Docker文档类资源-CSDN文库

(2)php.ini

php.ini-Docker-LNMP的php配置文件-Docker文档类资源-CSDN文库

(3)php-fpm.conf 

php-fpm.conf-Docker-LNMP的php配置文件-Docker文档类资源-CSDN文库

5.构建php镜像

docker build -t php:lnmp .

6.启动PHP镜像

注意:此时MySQL容器要处于运行的状态

docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp

五.验证

1.MySQL给wordpress授权

进入mysql容器,登录数据库

docker exec -it mysql /bin/bash

mysql -u root -p

create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;

浏览器访问:

http://192.168.111.171/wordpress/index.php

 

这篇关于Dockerfile项目——构建LNMP的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!