Docker构建LNMP环境并运行Wordpress平台

1.准备Nginx

上传文件

Dockerfile

FROM centos:7 as first

ADD nginx-1.24.0.tar.gz /opt/
COPY CentOS-Base.repo /etc/yum.repos.d/

RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make && \
    useradd -M -s /sbin/nologin nginx && \
    cd /opt/nginx-1.24.0/ && \
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module && \
    make && \
    make install
COPY nginx.conf /usr/local/nginx/conf/nginx.conf

FROM centos:7
COPY --from=first /usr/local/nginx/ /usr/local/nginx/
RUN useradd -M -s /sbin/nologin nginx
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]

nginx.conf

html

准备测试文件

2.准备Mysql

上传文件

Dockerfile

FROM centos:7 as first
ADD mysql-boost-5.7.44.tar.gz /opt
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN yum -y install ncurses ncurses-devel bison cmake openssl-devel gcc gcc-c++ make && \
    yum clean all && \
    cd /opt/mysql-5.7.44/ && \
    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 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && \
    make -j 2 && make install

FROM centos:7
COPY --from=first /usr/local/mysql/ /usr/local/mysql/
COPY  my.cnf /etc/my.cnf
RUN useradd -M -s /sbin/nologin mysql &&\
    chown -R mysql:mysql /usr/local/mysql/ &&\
    chown mysql:mysql /etc/my.cnf &&\
    /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
ENV PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]

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

3.准备php

上传文件

Dockerfile

--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 2 && make install
COPY php.ini /usr/local/php/lib/php.ini
COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf
COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf

FROM centos:7
COPY --from=first /usr/local/php/ /usr/local/php/
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN useradd -M -s /sbin/nologin nginx &&\
yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel &&\
yum clean all
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]

4.构建镜像

cd /opt/nginx_bian    #构建nginx镜像
docker build -t nginx:lnmp .

cd /opt/php    #构建php镜像
docker build -t php:lnmp .

docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork   #自定义网络
docker network ls 


 
docker run --name nginx -d -p 80:80 -v /opt/nginx_bian/html:/usr/local/nginx/html/ --network mynetwork --ip 172.18.0.10 nginx:lnmp
docker ps -a

访问测试页面

docker run --name php -d -p 9000:9000 --volumes-from nginx --network mynetwork --ip 172.18.0.30 php:lnnmp
 
docker ps -a

访问测试页面

cd /opt/mysql
docker build -t mysql:lnmp .  #构建mysql镜像
 
docker run --name mysql -d --privileged -v /usr/local/mysql --network mynetwork --ip 172.18.0.20 mysql:lnmp   #启动容器
docker ps -a

docker exec -it mysql bash
mysqladmin -u root -p password "abc123"  #设置密码
mysql -u root -pabc123  #登录mysql
grant all on *.* to 'root'@'%' identified by 'admin123';
quit
exit
docker stop php
docker rm php
 
docker run --name php -d -p 9000:9000 --volumes-from nginx --volumes-from mysql --network mynetwork --ip 172.18.0.30 php:lnnmp

5.运行wordpress

浏览器访问:http://192.168.9.113/wordpress/index.php

docker exec -it mysql bash
mysql -u root -pabc123
create database wordpress;
grant all on wordpress.* to 'wordpress'@'%' identified by 'admin123';
flush privileges;
quit
exit
cd /opt/nginx_bian/html
chmod 777 wordpress/
相关推荐
昌sit!2 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
追风林3 小时前
mac 本地docker-mysql主从复制部署
mysql·macos·docker
一坨阿亮3 小时前
Linux 使用中的问题
linux·运维
A ?Charis5 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
城南vision5 小时前
Docker学习—Docker核心概念总结
java·学习·docker
wclass-zhengge5 小时前
Docker篇(Docker Compose)
运维·docker·容器
李启柱5 小时前
项目开发流程规范文档
运维·软件构建·个人开发·设计规范
北漂IT民工_程序员_ZG6 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes
力姆泰克7 小时前
看电动缸是如何提高农机的自动化水平
大数据·运维·服务器·数据库·人工智能·自动化·1024程序员节
BPM_宏天低代码7 小时前
低代码 BPA:简化业务流程自动化的新趋势
运维·低代码·自动化