Docker-Compose编排lnmp(dockerfile) 完成Wordpress

时间:2024-05-04 14:47:01

目录

一、创建nginx镜像

二、创建mysql镜像

三、创建php镜像

四、启动wordpress

五、安装Compose

六、准备环境

​编辑

七、编写docker-compose.yml

八、启动并运行

九、浏览器访问


一、创建nginx镜像

#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <kcy>
#添加环境包
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#上传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 -j4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx","-g","daemon off;"]

二、创建mysql镜像

FROM centos:7
MAINTAINER this is mysql image <kcy>
RUN yum -y install ncurses ncurses-devel bison cmake
RUN yum -y install 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 -j4;make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
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" ]
CMD ["/usr/sbin/init"]
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
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
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

三、创建php镜像

vim Dockerfile 
 
FROM centos:7
MAINTAINER this is php image <kcy>
RUN yum install -y 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
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
CMD /usr/local/php/sbin/php-fpm -F
docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp

四、启动wordpress

[root@ebafbdc1c4dc bin]# mysql -u root -p
Enter password: 
#初始密码为空
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
 
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
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.00 sec)

浏览器访问http://192.168.91.105/wordpressindex.php

将下面方框中的代码全部复制到wp-config.php中

五、安装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版本

六、准备环境

[root@centos6 opt]# mkdir docker-compose_nginx
[root@centos6 opt]# cd docker-compose_nginx/
[root@centos6 docker-compose_nginx]# mkdir wwwroot
[root@centos6 docker-compose_nginx]# ls
wwwroot

[root@centos5 wordpress]# cd /opt
[root@centos5 opt]# ls
containerd  docker-compose_lnmp  mysqld  nginx  php  rh

[root@centos5 opt]# cp -r nginx/ /opt/docker-compose_lnmp/
[root@centos5 opt]# cd docker-compose_lnmp/
[root@centos5 docker-compose_lnmp]# ls
nginx  wwwroot

[root@centos5 docker-compose_lnmp]# cd ..
[root@centos5 opt]# cp -r mysqld/ /opt/docker-compose_lnmp/
[root@centos5 opt]# cp -r php/ /opt/docker-compose_lnmp/
[root@centos5 opt]# cd docker-compose_lnmp/
[root@centos5 docker-compose_lnmp]# ls
mysqld  nginx  php  wwwroot

[root@centos5 docker-compose_lnmp]# 

七、编写docker-compose.yml

version: '3'
services:
  nginx:
    container_name: nginx
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
    - 1212:80
    - 1616:443
    networks:
      lnmp:
        ipv4_address: 172.19.0.10
    volumes:
      - ./wwwroot:/usr/local/nginx/html

  mysql:
    container_name: mysql
    build:
      context: ./mysqld
      dockerfile: Dockerfile
    ports:
    - 3306:3306
    networks:
      lnmp:
        ipv4_address: 172.19.0.20
    volumes:
    - db-data:/usr/localmysql
    privileged: true
  php:
    container_name: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
    - 9000:9000
    networks:
      lnmp:
        ipv4_address: 172.19.0.30
    volumes:
    - db-data:/usr/local/mysql
    - ./nginx/html:/usr/local/nginx/html
    depends_on:
    - nginx
    - mysql

networks:
  lnmp:
    driver: bridge
    ipam:
      config:
        - subnet: 172.19.0.0/16

volumes:
  db-data:

八、启动并运行

注意:在执行之前,要先把之前创建的php、nginx、mysql容器删除,要不然会报错

报错之后在删除,可能会有错误

最好在之前就删除干净!!!

docker-compose -f docker-compose.yml up -d
###创建镜像并放入后台运行

docker exec -it mysql /bin/bash

create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;

九、浏览器访问

http://192.168.91.105/wordpress/index.php