当前位置:   article > 正文

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

【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
  • 1
  • 2
  • 3
  • 4

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

/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;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

/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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

/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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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

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

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/share/nginx/html
networks:
  lnmp:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

/opt/compose_nginx/nginx/Dockerfile

FROM centos:7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
cd /opt/compose_nginx/
docker-compose -f docker-compose.yml up -d
  • 1
  • 2

在这里插入图片描述

docker compose

Docker Compose是一个用于定义和运行多容器Docker应用程序的工具。它允许开发人员通过一个简单的YAML文件来定义应用程序的服务、网络和卷等配置,并使用一个命令来启动、停止和管理整个应用程序的容器。

使用Docker Compose可以方便地将多个相关的容器组合在一起,以形成一个完整的应用。在Compose文件中,可以定义多个服务(例如数据库、Web服务器等),并且可以指定它们之间的依赖关系和连接方式。

Docker Compose提供了一些常用的命令。

Docker Compose命令的基本格式如下:

docker-compose [options] [command] [arguments]
  • 1

其中,options是可选的命令选项,command是要执行的命令,arguments是命令的参数。

常用的Docker Compose命令有:

  • up:启动并创建容器。
  • down:停止并删除容器。
  • build:构建或重新构建服务。
  • start:启动已创建的容器。
  • stop:停止已启动的容器。
  • restart:重启容器。
  • logs:查看容器的日志输出。
  • ps:列出已启动的容器。
  • exec:在容器内部执行命令。
  • scale:设置服务的容器副本数量。

可以通过docker-compose [command] --help来查看具体命令的使用帮助。

除了基本的命令,Compose还支持一些高级功能,如覆盖默认配置、扩展服务、使用环境变量等。

总的来说,Docker Compose简化了多容器应用程序的管理和部署过程,使开发人员可以更轻松地构建和运行复杂的应用程序。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/1002366
推荐阅读
相关标签
  

闽ICP备14008679号