nginx | 172.111.0.10 | docker-nginx |
mysql | 172.111.0.20 | docker-mysql |
php | 172.111.0.30 | docker-php |
Nginx
[root@docker1 nginx]# systemctl stop firewalld
[root@docker1 nginx]# setenforce 0
[root@docker1 opt]# mkdir nginx mysql php
[root@docker1 opt]# ls
containerd mysql nginx php rh
[root@docker1 opt]# cd nginx
[root@docker1 nginx]# ls
Dockerfile nginx-1.22.0.tar.gz nginx.conf wordpress-6.4.2-zh_CN.tar.gz
[root@docker1 nginx]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 172.111.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
[root@docker1 nginx]# vim Dockerfile
#申明基础镜像
FROM centos:7
#安装依赖环境
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin nginx
#解压源码包
ADD nginx-1.22.0.tar.gz /usr/local/src/
#指定工作目录
WORKDIR /usr/local/src/nginx-1.22.0
#配置和编译安装:
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
# 设置 PATH 环境变量以包含 Nginx 二进制文件目录
# 确保在运行命令时,系统会在 /usr/local/nginx/sbin 中查找可执行文件
COPY nginx.conf /usr/local/nginx/conf/
# 将 nginx.conf 文件从构建上下文复制到图像中的 Nginx 配置目录
# 这用于提供自定义的 Nginx 配置文件,适用于你的应用程序
ADD wordpress-6.4.2-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod 777 -R /usr/local/nginx/html
EXPOSE 80
#创建容器内的数据卷,既可以对外给宿主机挂载,也可以对内,给容器做为挂载点
VOLUME ["/usr/local/nginx/html/"]
#设定容器启动时的命令
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[root@docker1 nginx]# docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
#如果非语法报错,可以尝试重启docker即可解决
[root@docker1 ~]# docker network ls
#查看当前docker容器的所有网络
NETWORK ID NAME DRIVER SCOPE
720d5cc3891b bridge bridge local
d6a752897091 host host local
d550d1203c25 mynetwork bridge local
2f239f6b35e1 none null local
[root@docker1 ~]# docker network rm -f 加上id或者name
#删除docker的网络 -f 表示强制
[root@docker1 ~]# ifconfig
docker1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.111.0.1 netmask 255.255.0.0 broadcast 172.111.255.255
ether 02:42:24:8e:69:2d txqueuelen 0 (Ethernet)
#有表示已经成功生成
[root@docker1 nginx]# cd /opt/nginx
[root@docker1 nginx]# ls
Dockerfile nginx-1.22.0.tar.gz nginx.conf wordpress-6.4.2-zh_CN.tar.gz
[root@docker1 nginx]# docker build -t nginx:lnmp .
[root@docker1 nginx]# docker run -itd --name nginx -p 80:80 -v /opt/nginx:/opt/nginxlogs --net mynetwork --ip 172.111.0.10 nginx:lnmp
3fea20197e2c516b4fe5e0fc7ce004531597eda9d0959dc1cd087403f53c59c7
[root@docker1 nginx]# curl 192.168.233.11
#访问成功说明没问题
MYSQL
[root@docker1 opt]# cd mysql/
[root@docker1 mysql]# ls
Dockerfile my.cnf mysql-boost-5.7.20.tar.gz
[root@docker1 mysql]# vim 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
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
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
[root@docker1 mysql]# vim Dockerfile
FROM centos:7
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin mysql
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_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 4 && make install
COPY my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql && chown mysql:mysql /etc/my.cnf
#目录下的所有文件和子目录的所有者设置为 mysql 用户和 mysql 组。这对于确保 MySQL 数据目录及其子目录中的文件都属于 MySQL 进程是很重要的。这是为了确保 MySQL 服务在运行时有足够的权限来读取和写入这些文件,
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data && cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ && systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME ["/usr/local/mysql"]
ENTRYPOINT ["/usr/sbin/init"]
[root@docker1 mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql lnmp 14e2a67287fe About a minute ago 10.1GB
nginx lnmp 2eeae94a8b03 17 minutes ago 627MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
[root@docker1 mysql]# docker run -itd --name mysql -p 3306:3306 --privileged -v /opt/mysql1:/opt/mysql --net mynetwork --ip 172.111.0.20 mysql:lnmp
[root@docker1 mysql]# docker exec -it mysql bash
[root@dbb0d9cac686 bin]# cd ..
[root@dbb0d9cac686 mysql]# ls
COPYING README-test docs man mysql.sock.lock support-files
COPYING-test bin include mysql-test mysqld.pid usr
README data lib mysql.sock share
#没有mysql.sock文件是进不去的
[root@dbb0d9cac686 bin]# mysql -u root -p
#无密码直接进
mysql> create database wordpress;
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
mysql> flush privileges;
PHP
[root@docker1 mysql]# cd /opt/php/
[root@docker1 php]# ls
Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
#php三个文件要改一下
[root@docker1 php]# vim Dockerfile
FROM centos:7
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 && 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 -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
COPY php.ini /usr/local/php/lib
COPY php-fpm.conf /usr/local/php/etc/
COPY www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT ["/usr/local/php/sbin/php-fpm","-F"]
[root@docker1 php]# docker build -t php:lnmp .
[root@docker1 php]# docker run -itd --name php -p 9000:9000 --volumes-from nginx --volumes-from mysql --net mynetwork --ip 172.111.0.30 php:lnmp
f494c8a3d77acd1578d1b1bffcf4e1f3c57d73b1c8929c44385121f5d4880485
[root@docker1 php]# docker exec -it php bash
[root@f494c8a3d77a php-7.1.10]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 113028 7524 pts/0 Ss+ 00:16 0:00 php-fpm: master proc
nginx 7 0.0 0.0 113028 3772 pts/0 S+ 00:16 0:00 php-fpm: pool www
nginx 8 0.0 0.0 113028 3772 pts/0 S+ 00:16 0:00 php-fpm: pool www
root 24 0.1 0.0 11828 1884 pts/1 Ss 00:18 0:00 bash
root 38 0.0 0.0 51732 1728 pts/1 R+ 00:18 0:00 ps -aux
[root@f494c8a3d77a php-7.1.10]# cd /usr/local
[root@f494c8a3d77a local]# ls
bin etc games include lib lib64 libexec mysql nginx php sbin share src
#有mysql跟nginx目录表示同步成功
开始访问 192.168.233.11/wordpress
如显示需要配置文件,要给nginx中RUN chmod 777 -R /usr/local/nginx/html 权限
[root@docker1 nginx]# docker exec -it php bash
[root@902336691c89 php-7.1.10]# cd /usr/local
[root@902336691c89 local]# ls
bin etc games include lib lib64 libexec mysql nginx php sbin share src
[root@902336691c89 local]# cd nginx/
[root@902336691c89 nginx]# ls
html
#这里html必须要是有权限才写
给完权限删除nginx与php的容器重新build编译nginx再 docker run,