【docker】dockerfile部署lnmp、docker compose初步

1、dockerfile部署lnmp

mkdir /opt/lnmp
cd /opt/lnmp
mkdir nginx mysql php
docker network create --subnet=20.0.0.0/24 lnmp-net

将wordpress文件夹拷贝到nginx、php文件夹
/opt/nginx/Dockerfile:

# 使用官方的nginx镜像作为基础镜像
FROM nginx:latest

# 复制默认配置文件
COPY ./nginx.conf /etc/nginx/nginx.conf

# 复制静态文件到容器中
COPY ./wordpress /var/www/html
# 指定容器启动时执行的命令
CMD ["nginx", "-g", "daemon off;"]
# 暴露容器的端口
EXPOSE 80

/opt/lnmp/nginx/nginx.conf:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /var/www/html;
            index  index.php index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           /var/www/html;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

/opt/lnmp/mysql/Dockerfile:

# 使用官方的mysql/mysql-server镜像
FROM mysql:5.7

# 设置MYSQL_ROOT_PASSWORD
ENV MYSQL_ROOT_PASSWORD=Admin!123

# 复制数据初始化脚本
COPY ./init.sql /docker-entrypoint-initdb.d/
EXPOSE 3306

/opt/lnmp/mysql/init.sql

-- 创建数据库
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'Admin!123';
flush privileges;

/opt/lnmp/php/Dockerfile

# 使用官方的php:fpm镜像
FROM php:7.4-fpm

# 安装必要的扩展
RUN docker-php-ext-install mysqli pdo_mysql

# 设置时区
RUN echo 'Asia/Shanghai' > /etc/timezone && \
    dpkg-reconfigure --frontend noninteractive tzdata


COPY ./wordpress /var/www/html

# 设置工作目录
WORKDIR /var/www/html
# 暴露端口9000
EXPOSE 9000
cd /opt/lnmp
docker build -t my-php -f ./php/Dockerfile ./php   
docker build -t my-mysql -f ./mysql/Dockerfile ./mysql     
docker build -t my-nginx -f ./nginx/Dockerfile ./nginx
docker run -d --name mysql --network lnmp-net --ip 20.0.0.3 my-mysql
docker run -d --name php --network lnmp-net --ip 20.0.0.4 -p 9000:9000 my-php
docker run -d --name nginx --network lnmp-net --ip 20.0.0.2 -p 80:80 my-nginx

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、docker-compose构建nginx镜像和容器

mkdir -p /opt/compose_nginx/wwwroot/
echo "<h1>this is test web</h1>" > /opt/compose_nginx/wwwroot/index.html

docker-compose.yml

version: '3'
services:
  nginx:
    container_name: web1
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - 1216:80
      - 1217:443
    networks:
      lnmp:
        ipv4_address: 172.20.0.10
    volumes:
        - ./wwwroot:/usr/local/nginx/html
networks:
  lnmp:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

/opt/compose_nginx/nginx/Dockerfile

FROM centos

RUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*

RUN yum clean all && yum makecache 
RUN yum install -y nginx

CMD ["nginx", "-g", "daemon off;"]
# 暴露容器的端口
EXPOSE 80
cd /opt/compose_nginx/
docker-compose -f docker-compose.yml up -d

在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值