1.准备Nginx
上传文件
Dockerfile
FROM centos:7 as firstADD 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.confFROM 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 installFROM 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 = 1sql_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.confFROM 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:lnnmpdocker 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 phpdocker 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/