mkdir /opt/lnmp
cd /opt/lnmp
mkdir nginx mysql php centos
mv /opt/nginx-1.24.0.tar.gz /opt/lnmp/nginx
mv /opt/wordpress-4.9.4-zh_CN.tar.gz /opt/lnmp/nginx
mv /opt/mysql-boost-5.7.20.tar.gz /opt/lnmp/mysql
mv /opt/php-7.1.10.tar.bz2 /opt/lnmp/php
cd /opt/lnmp/centos
vim Dockerfile
FROM centos:7
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make openssl-devel \
gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel \
ncurses \
ncurses-devel \
bison \
cmake
docker build -t centos:jiang . #有个点不能忘记,表示使用当前目录下的Dockerfile文件创建
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mylnmp
cd /opt/lnmp/nginx
vim Dockerfile
FROM centos:jiang AS first
MAINTAINER this is nginx image <jiang 202307025>
ADD nginx-1.24.0.tar.gz /usr/local
RUN useradd -M -s /sbin/nologin nginx && \
cd /usr/local/nginx-1.24.0 && \
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install && \
yum clean all
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的配置文件,以便第一步最后的COPY操作
此配置文件可以从别的机器上现成的nginx.conf文件复制过来,或者新建一个别的nginx镜像,将其配置文件从容器复制文件到主机中
这里是启动容器复制过来
docker cp 59a4c8b5589d:/etc/nginx/nginx.conf /opt/lnmp/nginx/nginx.conf
docker cp 59a4c8b5589d:/etc/nginx/conf.d/default.conf /opt/lnmp/nginx/default.conf
docker build -t nginx:jiang .
docker run -d -P nginx:jiang
访问http://192.168.52.150:32768
在nginx目录下准备html目录用来挂载
vim index.html
vim index.php
<?php
$link=mysqli_connect('172.18.0.20','admin','admin1234');
if($link) echo "Success!!
";
else echo "Fail!!";
?>
将wordpress-4.9.4-zh_CN.tar.gz
解压到html
目录下
tar xf wordpress-4.9.4-zh_CN.tar.gz
mv /opt/lnmp/nginx/wordpress /opt/lnmp/nginx/html
创建nginx:jiang容器
docker run -d --name nginx --network=mylnmp --ip=172.18.0.10 -p 80:80 -v /opt/lnmp/nginx/html/:/usr/local/nginx/html/ nginx:jiang
cd /opt/lnmp/mysql
vim Dockerfile
FROM centos7:jiang
ADD mysql-boost-5.7.41.tar.gz /opt/
RUN cd /opt/mysql-5.7.41/ && \
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 2 && make install
COPY my.cnf /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
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
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
vim /opt/lnmp/mysql/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
docker build -t mysql:jiang .
docker run -d --name mysql --privileged --network mylnmp --ip 172.18.0.20 -v /usr/local/mysql mysql:jiang
docker exec -it 06b73978c617 bash
mysql -u root -p
CREATE DATABASE wordpress;
show databases;
GRANT all ON wordpress.* TO 'admin'@'%' IDENTIFIED BY 'admin1234';
flush privileges;
cd /opt/lnmp/php
vim Dockerfile
FROM centos7:jiang #这里第一层没有AS设置别名
ADD php-7.1.10.tar.bz2 /opt
RUN cd /opt/php-7.1.10 && \
./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 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=0 /usr/local/php /usr/local/php #所以这里from直接用0代表第一层,如有第二层,则为1,依次类推
RUN useradd -M -s /sbin/nologin php && yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
准备php.ini php-fpm.conf www.conf 三个文件,原理同上
vim /opt/lnmp/php/php.ini
--939行--取消注释,修改------------------
date.timezone = Asia/Shanghai
--1170行--修改----------------------
mysqli.default_socket = /usr/local/mysql/mysql.sock
cd /opt/lnmp/php/etc/php-fpm.d/
mv www.conf.default www.conf
vim /opt/lnmp/php/etc/php-fpm.d/www.conf
-------------修改23行和24行---------------------------------------------------------
user = php
group = php
-------------修改36行---------------------------------------------------------------
listen = 172.18.0.30:9000
-------------修改62行---------------------------------------------------------------
listen.allowed_clients = 172.18.0.10
cd /opt/lnmp/php/etc/
mv php-fpm.conf.default php-fpm.conf
vim /opt/lnmp/php/etc/php-fpm.conf
--------------17行取消注释---------------------------------------------------------------
pid = run/php-fpm.pid
构建php:jiang镜像
docker build -t php:jiang .
创建php容器
docker run --name php -d -p 9000:9000 --network mylnmp --ip 172.18.0.30 --volumes-from nginx --volumes-from mysql php:jiang
http://192.168.52.150/wordpress/index.php
在宿主机或者进入容器内修改都可以,因为已经挂载过了
docker run -itd --name nginx -m 500m --memory-swap=0 --network=mylnmp --ip=172.18.0.10 -p 80:80 -v /opt/lnmp/nginx/html/:/usr/local/nginx/html/ nginx:clr
docker run -itd --name mysql --device-write-bps /dev/sda:10M --privileged --network mylnmp --ip 172.18.0.20 -v /usr/local/mysql mysql:centos7
docker ps -a
dd if=/dev/zero of=test.out bs=1M count=10 oflag=direct
将所有容器进行快照
docker ps -a
docker export -o lnmp-nginx.tar 98bf909e2749
docker export -o lnmp-mysql.tar 06b73978c617
docker export -o lnmp-php.tar b73195ab3433
ls
docker save -o lnmp-nginx nginx:jiang
docker save -o lnmp-mysql mysql:jiang
docker save -o lnmp-php php:jiang
减少存储空间
提高下载速度
加快构建时间:镜像小了,构建就快,需要处理的文件和依赖项更少。
提高安全性:较小的镜像就会减少攻击面,因为仅包含应用程序所需的最小组件和依赖项,这样就减少了漏洞和攻击点。
降低内存占用:运行时需要的内存就会更少,对于内存资源有限的环境或者运行大量容器的场景非常有效果。
(1)尽可能的使用小体积的基础镜像
(2)尽可能检查Dockerfile文件中指令的数量
(3)可以构建镜像步骤,最后添加清空系统和应用程序的缓存命令
(4)使用多阶段(多级)构建 FROM 第一阶段的基础镜像 [AS 别名]
.........
FROM 第二阶段的基础镜像
COPY --from=别名/0 第一阶段构建的文件/目录