dockerfile搭建lnmp

nginx1.22 172.111.0.10 docker部署nginx

mysql 8.0.30 172.111.0.20 docker部署mysql

php 8.1.27 172.111.0.30 docker部署php

docker:单节点部署,在一台机器上部署,跨机器 容器无法通信。做高可用只能在节点上增加同一服务的容器数量

部署nginx:

bash 复制代码
cd /opt
mkdir nginx mysql php
cd nginx
nginx wordpress的安装包拖进来

vim Dockerfile
FROM centos:7
MAINTAINER this is nginx <lnmp>
RUN rm -rf /etc/yum.repos.d/*
ADD http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/Centos-7.repo
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
ADD nginx-1.22.0.tar.gz /usr/local/src
RUN useradd -M -s /sbin/nologin nginx
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
COPY nginx.conf /usr/local/nginx/conf
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;"]

#修改配置文件
cd nginx
vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    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;
        }
}
}

#构建
docker build -t nginx:lnmp .

#创建自定义网络
docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

#查看是否有docker1网卡
ifconfig

#创建容器并运行
docker run -itd --name nginx -p 80:80 -m 512m -v /opt/nhtml:/opt/nginx --net mynetwork --ip 172.111.0.10 nginx:lnmp

#进入容器
docker exec -it nginx bash

cd /usr/local/nginx
cd conf
yum -y install net-tools
netstat -antp | grep 80

cd html
cd /usr/local/nginx/html
echo 123 > index.html

部署mysql:

bash 复制代码
cd mysql
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
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

vim Dockerfile
FROM centos:7
MAINTAINER this is mysql <lnmp>
RUN rm -rf /etc/yum.repos.d/*
ADD http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/Centos-7.repo
RUN yum -y install libaio numactl-libs pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-8.0.30-el7 -x86_64.tar.gz  /usr/local
RUN mv /usr/local/mysql-8.0.30-el7 -x86_64 /usr/local/mysql
COPY my.conf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql && chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin/:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/bin
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
VOLUME ["/usr/local/mysql"]
ENTRYPOINT ["./mysqld"]

docker build -t mysql:lnmp .

docker run -itd --name mysql -p 3306:3306 --privileged -v /opt/mysql1:/opt/mysql --net mynetwork --ip 172.111.0.20 mysql:lnmp

docker exec -it mysql bash

mysql -u root -p
create database wordpress;
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
CREATE USER 'wordpress'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%';
flush privileges;
进入navicat测试是否连接正常

exit

部署php:

bash 复制代码
cd php
php安装包和三个文本拖进来
vim Dockerfile
FROM centos:7
MAINTAINER this is php <lnmp>
RUN rm -rf /etc/yum.repos.d/*
ADD http://mirrors.aliyun.com/repo/Centos-7.repo /etc/yum.repos.d/Centos-7.repo
RUN yum -y install epel-release
RUN yum -y install gcc gcc-c++ make \
gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
oniguruma-devel \
sqlite-devel
ADD php-8.1.27.tar.gz /usr/local/src
WORKDIR /usr/local/src/php-8.1.27
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-pdo-mysql \
--with-openssl \
--with-sqlite-devel \
--with-oniguruma-devel \
--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
CMD ["/usr/local/php/sbin/php-fpm","-F"]

docker build -t php:lnmp .

docker run -itd --name php --net mynetwork --ip 172.111.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp

docker exec -it php bash
相关推荐
意疏12 小时前
【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行
linux·docker
墨鸦_Cormorant12 小时前
使用docker快速部署Nginx、Redis、MySQL、Tomcat以及制作镜像
redis·nginx·docker
Code_Artist12 小时前
Docker镜像加速解决方案:配置HTTP代理,让Docker学会科学上网!
docker·云原生·容器
wanmei00213 小时前
Dockerfile复制目录进入镜像里
docker
inter_peng13 小时前
[Docker-显示所有容器IP] 显示docker-compose.yml中所有容器IP的方法
tcp/ip·docker·eureka
Linux运维日记14 小时前
k8s1.31版本最新版本集群使用容器镜像仓库Harbor
linux·docker·云原生·容器·kubernetes
一名路过的小码农15 小时前
ceph 18.2.4二次开发,docker镜像制作
ceph·docker·容器
xiangshangdemayi18 小时前
Windows环境GeoServer打包Docker极速入门
windows·docker·容器·geoserver·打包·数据挂载
程序员JerrySUN18 小时前
熟悉的 Docker,陌生的 Podman
linux·docker·容器·系统架构·podman
gobeyye19 小时前
Docker 用法详解
运维·docker·容器