【docker实战】基于Dockerfile搭建LNMP+wordpress

架构:

nginx 172.111.0.10 docker-nginx
mysql 172.111.0.20 docker-mysql
PHP 172.111.0.30 docker-PHP

首先要创建172.111.0.0网桥

bash 复制代码
docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

基于Dockerfile创建nginx镜像:

bash 复制代码
cd /opt
mkdir nginx mysql php
cd nginx

将nginx的安装包放到nginx目录中

vim nginx.conf

要将nginx.conf文件COPY到容器中

bash 复制代码
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.111.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
}
}

nginx Dockerfile:

bash 复制代码
FROM centos:7
RUN yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
ADD nginx-1.22.0.tar.gz /usr/local/src/
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src/nginx-1.22.0
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module && make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
COPY nginx.conf /usr/local/nginx/conf/
ADD wordpress-6.4.2-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod -R 777 /usr/local/nginx/html
EXPOSE 80
VOLUME ["/usr/local/nginx/html/"]
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]

创建nginx容器:

bash 复制代码
docker build -t nginx:lnmp .

启动nginx容器:

bash 复制代码
docker run -itd --name nginx1 -p 80:80 -v /opt/nginx:/opt/nginxlogs --net mynetwork --ip 172.111.0.10 nginx:lnmp

检测:

20.0.0.51:80

基于Dockerfile创建mysql镜像:

cd /opt/mysql

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

general_log=ON

general_log_file=/usr/local/mysql/data/mysql_general.log

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

FROM centos:7

RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make && 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_EXTRA_CHARSETS=all

-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

COPY my.cnf /etc/my.cnf

EXPOSE 3306

RUN chown -R mysql:mysql /usr/local/mysql && chown mysql:mysql /etc/my.cnf

WORKDIR /usr/local/mysql/bin/

RUN ./mysqld

--initialize-insecure

--user=mysql

--basedir=/usr/local/mysql

--datadir=/usr/local/mysql/data && cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ && systemctl enable mysqld

ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

VOLUME ["/usr/local/mysql"]

ENTRYPOINT ["/usr/sbin/init"]

docker build -t mysql:lnmp .

创建mysql容器:

docker run -itd --name mysql -p 3306:3306 --privileged -v /opt/mysql1:/opt/mysql --net mynetwork --ip 172.111.0.20 mysql:lnmp

进入容器给权限:

create database wordpress;

grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';

grant all privileges on . to 'root'@'%' identified by '123456';

flush privileges;

基于Dockerfile创建php镜像:

FROM centos:7

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 && 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 -j 4 && make install

ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH

COPY php.ini /usr/local/php/lib

COPY php-fpm.conf /usr/local/php/etc/

COPY www.conf /usr/local/php/etc/php-fpm.d/

EXPOSE 9000

ENTRYPOINT ["/usr/local/php/sbin/php-fpm","-F"]

构建镜像

docker build -t php:lnmp .

创建容器:

docker run -itd --name php -p 9000:9000 --volumes-from nginx1 --volumes-from mysql --net mynetwork --ip 172.111.0.30 php:lnmp

docker exec -it php bash

网页构建wordpress论坛:

相关推荐
BillKu2 分钟前
Linux(CentOS)yum update -y 事故
linux·运维·centos
a266378967 分钟前
解决yum命令报错“Could not resolve host: mirrorlist.centos.org
linux·运维·centos
上海文顺负载箱20 分钟前
如何评估焊机测试负载均衡性能
运维·负载均衡
铁锤妹妹头发多1 小时前
新手用docker真**难受
运维·docker·容器
超栈1 小时前
HCIP(11)-期中综合实验(BGP、Peer、OSPF、VLAN、IP、Route-Policy)
运维·网络·网络协议·计算机网络·web安全·网络安全·信息与通信
Cachel wood1 小时前
Github配置ssh key原理及操作步骤
运维·开发语言·数据库·windows·postgresql·ssh·github
编程一生2 小时前
回调数据丢了?
运维·服务器·前端
华为云PaaS服务小智2 小时前
华为大咖说 | 浅谈智能运维技术
运维·华为·华为云
zhd15306915625ff4 小时前
化工厂主要涉及的自动化备件有哪些?
运维·自动化·化工厂
Jason-河山4 小时前
利用API返回值实现商品信息自动化更新:技术与实践
运维·自动化