准备工作
#关闭防火墙和防护机制
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
docker network create --subnet=172.18.0.0/16
--opt "com.docker.network.bridge.name"="docker1" mynetwork#设置自定义网络模式,模式为bridge模式,
docker1的ip地址为172.18.0.1,显示的网络名称mynetwork。docker network ls
#查看使用的网络模式ifconfig
部署nginx
(容器IP 为 172.18.0.10)
编写Dockerfile文件
mkdir /opt/nginx
cd /opt/nginx/上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目录中#解压wordpress
mkdir /opt/nginx/html
tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
vim DockerfileFROM centos:7
#指定基础镜像MAINTAINER this is nginx image <wl>
#指定维护者信息RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
#安装依赖包,安装环境RUN useradd -M -s /sbin/nologin nginx
#创建nginx用户ADD nginx-1.12.0.tar.gz /usr/local/src/
#将本地安装包上传指定目录 解压WORKDIR /usr/local/src/nginx-1.12.0
#进入到/opt/nginx-1.12.2目录中RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
#开始编译安装,加上&&符号,只有前面执行完成,后面才会执行ENV PATH /usr/local/nginx/sbin:$PATHADD nginx.conf /usr/local/nginx/conf/
#开始编译安装,加上&&符号,只有前面执行完成,后面才会执行#ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
#上传论坛的包RUN chmod 777 -R /usr/local/nginx/html/
#给html文件所有权限EXPOSE 80
EXPOSE 443
#开放80 443 端口VOLUME ["/usr/local/nginx/html/"]
#挂载目录(相当于将/usr/local/nginx/html目录共享,
会在宿主机上自动生成共享目录,主要目的是为了让容器之间共享这个文件)(视情况可不需要)ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
#执行启动nginx命令,并且关闭nginx的后台启动,因为需要它占用前台,让docker不关闭
配置nginx.conf文件
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 utf-8;#access_log logs/host.access.log main;location / {root html;index index.html 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 html;fastcgi_pass 172.18.0.30:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;include fastcgi_params;}# 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;# }#}}
对接php的IP地址
构建启动nginx镜像
docker build -t nginx:lnmp .
#构建nginx镜像(别忘记最后的点)docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp
#启动容器docker ps -a
#查看容器
部署mysql
(容器IP 为 172.18.0.20)
mkdir /opt/mysqld
cd /opt/mysqld上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中
编写Dockerfile文件
vim DockerfileFROM centos:7
#基础镜像MAINTAINER this is mysql image <wl>
#维护人描述信息RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
#安装依赖环境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_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 && make install
#编译安装ADD my.cnf /etc/
#将主机上的 "my.cnf" 文件复制到镜像中的 "/etc/" 目录中ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
#设置环境变量RUN useradd -M -s /sbin/nologin mysql
创建mysql用户RUN chown mysql:mysql /etc/my.cnf
#将 "/etc/my.cnf" 文件的所有者和组设置为 "mysql:mysql"RUN chown -R mysql:mysql /usr/local/mysql/
#将 "/usr/local/mysql/" 目录及其子目录下的所有文件的所有者和组设置为 "mysql:mysql"RUN /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
#初始化mysqlEXPOSE 3306
#开放3306端口CMD ["/usr/local/mysql/bin/mysqld"]
配置my.conf文件
vim my.cnf[client]
port = 3306
socket = /usr/local/mysql/mysql.sock[mysql]
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
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
构建启动Mysql镜像
docker build -t mysql:lnmp .docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp-----------------------------------------
--privileged: #让容器内拥有root权限
-v /usr/local/mysql: #共享这个目录,其它容器可以使用--volumes-from 指定读取本容器的共享目录。然后也会生成一样的目录。
--net mynetwork: #指定--net网络模式,mynetwork为自定义网络模式。
--ip 172.18.0.20: #指定ip地址,自定义网络模式也可以指定ip地址。
-----------------------------------------
docker ps -a
#查看容器
部署php
(容器IP 为 172.18.0.30)
mkdir /opt/php
cd /opt/php上传 php-7.1.10.tar.bz2 到 /opt/php 目录中
编写Dockerfile文件
vim DockerfileFROM centos:7
MAINTAINER this is php image <wl>
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
RUN 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 && make install
#编译安装ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
设置环境变量ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
#上传配置文件到指定目录EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
#启动镜像 占用前台
配置php的配置文件
vim php.inivim www.confvim php-fpm.conf
构建启动php镜像
docker build -t php:lnmp .docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
登陆mysql容器 授权
docker exec -it mysql /bin/bash
#进入mysql容器mysql
#登录mysqlcreate database wordpress;
#创建数据库
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
访问测试wordpress
浏览器访问
http://20.0.0.60/wordpress/index.php