docker-compose管理创建LNMP服务并运行Wordpress网站平台

文章目录

  • 一.项目环境
    • 1. 环境描述
    • 2.项目需求
  • 二.部署过程
    • 1.安装Docker
    • 2.安装Docker加速器
    • 3.Docker-Compose安装部署
    • 4.准备依赖文件、配置nginx
    • 5.配置mysql
    • 6.配置php
    • 7.编写docker-compose.yml
    • 8.验证
  • 三.容器快照,然后将Docker镜像打包成tar包备份到本地
  • 四.docker-compose.yml 下载到本地

一.项目环境

公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务使用docker-compose管理工作。

1. 环境描述

主机 操作系统 ip地址 主要软件
Docker CentOS 7.6 x86_64 192.168.198.13 Docker 20.08

2.项目需求

(1)使用Docker构建LNMP环境并运行Wordpress网站平台。

(2) 为了方便运维和统一管理使用进行docker-compose编排

(3)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。

(4)把docker-compose.yml 下载到本地

二.部署过程

1.安装Docker

#关闭防火墙
systemctl stop firewalld.service
setenforce 0
#安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2 
#设置阿里云镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
#安装 Docker-CE并设置为开机自动启动
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker.service
systemctl enable docker.service 

2.安装Docker加速器

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://90b8orj3.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker

3.Docker-Compose安装部署

#注意必须是在安装docker的基础上
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#下载安装包单独安装DockerCompose
chmod +x /usr/local/bin/docker-compose
#docker-compose添加执行权限
docker-compose --version
#查看docker-compose版本

4.准备依赖文件、配置nginx

#创建项目目录 compose_nginx; 
mkdir /opt/compose_nginx/
cd /opt/compose_nginx
kdir nginx mysql php wwwroot

#上传wordpress软件包
cd /opt/compose_nginx/wwwroot/
ls wordpress-4.9.4-zh_CN.tar.gz 
tar xf wordpress-4.9.4-zh_CN.tar.gz 

cd /opt/compose_nginx/nginx/
ls wordpress-4.9.4-zh_CN.tar.gz
tar xf wordpress-4.9.4-zh_CN.tar.gz 

#上传nginx软件包
cd /opt/compose_nginx/nginx/
ls nginx-1.12.0.tar.gz  
nginx-1.12.0.tar.gz 
cd /opt/compose_nginx/nginx/
vim Dockerfile
#编写nginx的Dockerfile文件
FROM centos:7
MAINTAINER this is nginx image 
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j2 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD 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/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
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;
            #此地址是php端地址
            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;
        }
    }
}

在这里插入图片描述

5.配置mysql

#进入mysql子目录
cd /opt/compose_nginx/mysql/
#将mysql包上传到此目录
ls mysql-boost-5.7.20.tar.gz 
#编写mysql的Dockerfile文件
cd /opt/compose_nginx/mysql/
vim Dockerfile

FROM centos:7
MAINTAINER this is mysql image 
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 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j2 && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin  mysql && chown mysql:mysql /etc/my.cnf && chown -R mysql:mysql /usr/local/mysql/ && /usr/local/mysql/bin/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/
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
vim my.cnf 

[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash

[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=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

在这里插入图片描述

6.配置php

cd /opt/compose_nginx/php/
#将php包上传
ls php-7.1.10.tar.bz2 
cd /opt/compose_nginx/php/

vim Dockerfile 

FROM centos:7
MAINTAINER this is php image 
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 -j2 && 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-fpm.conf php.ini、www.conf 配置文件
vim php.ini
#该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置
#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock

vim php-fpm.conf
#该配置文件位于 /usr/local/php/etc/php-fpm.conf.default
#17行,删除注释符号“;”
pid = run/php-fpm.pid

vim www.conf
#该配置文件位于 cd /usr/local/php/etc/php-fpm.d/
user = nginx
group = nginx
#36行,修改监听IP和端口为容器IP:9000端口
listen = 172.18.0.30:9000

在这里插入图片描述

7.编写docker-compose.yml

#拉取镜像centos:7
docker pull centos:7
vim docker-compose.yml 

version: '2'
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    networks:
      lnmp:
        ipv4_address: 172.18.0.10
    volumes:
      - ./wwwroot/:/usr/local/nginx/html
  mysql:
    hostname: mysql
    build:
      context: ./mysql
      dockerfile: Dockerfile
    container_name: mysql
    ports:
      - 3306:3306
    networks:
      lnmp:
        ipv4_address: 172.18.0.20
    volumes:
      - /usr/local/mysql
  php:
    hostname: php
    build:
      context: ./php
      dockerfile: Dockerfile
    container_name: php
    ports:
      - 9000:9000
    networks:
      lnmp:
        ipv4_address: 172.18.0.30
    volumes_from:
      - nginx
      - mysql
    depends_on:
      - nginx
      - mysql
    links:
      - nginx
      - mysql
networks:
  lnmp:
    driver: bridge
    ipam:
      config:
        - subnet: 172.18.0.0/16
docker-compose -f docker-compose.yml up -d

docker-compose管理创建LNMP服务并运行Wordpress网站平台_第1张图片

8.验证

#在工作目录,使用docker-compose ps 可以看到启动的容器状态
docker-compose ps

docker-compose管理创建LNMP服务并运行Wordpress网站平台_第2张图片

#进入mysql容器,配置用户授权.
[root@test3 compose_nginx]# docker exec -it mysql /bin/bash
[root@mysql mysql-5.7.20]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
#访问网站
http://192.168.198.13/wordpress/wp-admin/setup-config.php

docker-compose管理创建LNMP服务并运行Wordpress网站平台_第3张图片

三.容器快照,然后将Docker镜像打包成tar包备份到本地

cd /op/
docker save -o 保存目录 导出的镜像
例如:
docker save -o compose_nginx_php.tar compose_nginx_php:latest

docker-compose管理创建LNMP服务并运行Wordpress网站平台_第4张图片
在这里插入图片描述

四.docker-compose.yml 下载到本地

#sz选择存储路径下载
sz docker-compose.yml

你可能感兴趣的:(docker,容器,运维)