一、搭建LNMP
基础配置

1、制作Nginx镜像

制作dockerfile
vim dockerfileFROM centos:7
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum clean all
RUN yum makecache
RUN yum -y install zlib-devel pcre-devel gcc make openssl-devel
ADD nginx-1.19.5.tar.gz /opt
WORKDIR /opt/nginx-1.19.5
RUN ./configure --prefix=/usr/local/nginx && make && make install
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
EXPOSE 443
ADD run.sh /run.sh
RUN chmod +x /run.sh
CMD ["/run.sh"]

编辑nginx配置文件

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 /web;index index.html index.htm 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 /web;fastcgi_pass php01:9000;fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;include fastcgi.conf;}# 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;# }#}}
daemon off;
编辑容器前台运行脚本

vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
创建镜像
docker build -t mynginx .

查看
docker images

2、制作PHP镜像
制作dockerfile
vim dockerfileFROM centos:7
MAINTAINER jacker
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
RUN yum cleanall
RUN yum makecache
RUN yum install -y wget net-tools php php-fpm php-common php-devel php-mysql
RUN sed -i 's/127.0.0.1/0.0.0.0/g' /etc/php-fpm.d/www.conf
RUN sed -i 's/listen.allowed_clients/;listen.allowed_clients/g' /etc/php-fpm.d/www.conf
RUN sed -i 's/;default_charset/default_charset/g' /etc/php.ini
EXPOSE 9000
CMD ["php-fpm"]

创建PHP镜像
docker build -t myphp .

3、制作MySQL镜像
制作dockerfile
vim dockerfileFROM centos:7
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum clean all
RUN yum makecache
RUN yum -y install mariadb mariadb-server
RUN chown -R mysql:mysql /var/lib/mysql
ADD run.sh /run.sh
RUN chmod +x /run.sh
RUN /run.sh
EXPOSE 3306
CMD ["mysql_safe"]

编辑mysql配置文件
vim my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
编辑容器前台运行脚本
vim run.sh
#!/bin/bash
mysql_install_db --user=mysql
sleep=3
mysqld_safe &
sleep=3
mysqladmin -u "root" password "123456"
mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"
mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';"
mysql -uroot -p123456 -e "flush privileges;"


创建mysql镜像
docker build -t my_mysql .

4、启动容器
myphp
docker run -id --net=my_net -v /web:/web --name php01 myphp
mynginx
docker run -id --net=my_net -v /web:/web -v /opt/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf -p 80:80 --name nginx01 mynginx
my_mysql
docker run -p 3306:3306 --name mysql01 -v /opt/mysql/my.cnf:/etc/my.cnf --net=my_net -d my_mysql
查看
docker ps -a

5、测试一:
cd /webvim aaa.php
###编辑内容###
<?php
phpinfo();
?>vim bbb.php
###编辑内容###
<?php
$link=mysqli_connect('mysql01','root','123456');
if($link) echo "test OK";
mysqli_close($link);
?>


6、测试二:
cd /web
rm -rf ./*
cp -r wordpress/* /web/
chmod -R 777 /web/
docker exec -it mysql01 /bin/bash
mysql -uroot -p123456
create database wordpress;docker ps -a




7、制作tomcat镜像

制作dockerfile文件
vim dockerfileFROM centos:7
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
RUN mv /usr/local/apache-tomcat-8.5.16 /usr/local/tomcat
RUN mv /usr/local/jdk1.8.0_91 /usr/local/javaENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PARH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH $CLASSPATH:/usr/local/java/lib:/usrr/local/java/jre/libADD run.sh /run.sh
RUN chmod +x /run.sh
CMD ["/run.sh"]
修改内核参数
echo "vm.max_map_count=655360" >> /etc/sysctl.conf
sysctl -p
编辑容器前台运行脚本
vim run.sh
#!/bin/bash
export JAVA_OPTS ="-Xms2048m -Xmx4096m"
ulimit -n 655360
/usr/local/tomcat/bin/startup.sh
tailf /usr/local/tomcat/logs/catalina.out
创建镜像
docker build -t mytomcat .
创建容器
docker run -itd -p 12345:8080 --name tomcat01 mytomcat
访问测试
