当前位置:   article > 正文

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

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

目录

一、创建nginx镜像

二、创建mysql镜像

三、创建php镜像

四、启动wordpress

五、安装Compose

六、准备环境

​编辑

七、编写docker-compose.yml

八、启动并运行

九、浏览器访问


一、创建nginx镜像

  1. #基于基础镜像
  2. FROM centos:7
  3. #用户信息
  4. MAINTAINER this is nginx image <kcy>
  5. #添加环境包
  6. RUN yum -y update
  7. RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
  8. RUN useradd -M -s /sbin/nologin nginx
  9. #上传nginx软件压缩包,并解压
  10. ADD nginx-1.22.0.tar.gz /usr/local/src/
  11. #指定工作目录
  12. WORKDIR /usr/local/src/nginx-1.22.0
  13. RUN ./configure \
  14. --prefix=/usr/local/nginx \
  15. --user=nginx \
  16. --group=nginx \
  17. --with-http_stub_status_module && make -j4 && make install
  18. ENV PATH /usr/local/nginx/sbin:$PATH
  19. ADD nginx.conf /usr/local/nginx/conf/
  20. RUN chmod 777 -R /usr/local/nginx/html/
  21. EXPOSE 80
  22. EXPOSE 443
  23. ENTRYPOINT [ "/usr/local/nginx/sbin/nginx","-g","daemon off;"]

二、创建mysql镜像

  1. FROM centos:7
  2. MAINTAINER this is mysql image <kcy>
  3. RUN yum -y install ncurses ncurses-devel bison cmake
  4. RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql
  5. ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
  6. WORKDIR /usr/local/src/mysql-5.7.20/
  7. RUN cmake \
  8. -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  9. -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
  10. -DSYSCONFDIR=/etc \
  11. -DSYSTEMD_PID_DIR=/usr/local/mysql \
  12. -DDEFAULT_CHARSET=utf8 \
  13. -DDEFAULT_COLLATION=utf8_general_ci \
  14. -DWITH_EXTRA_CHARSETS=all \
  15. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  16. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  17. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  18. -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
  19. -DMYSQL_DATADIR=/usr/local/mysql/data \
  20. -DWITH_BOOST=boost \
  21. -DWITH_SYSTEMD=1;make -j4;make install
  22. ADD my.cnf /etc/my.cnf
  23. EXPOSE 3306
  24. RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
  25. WORKDIR /usr/local/mysql/bin/
  26. RUN ./mysqld \
  27. --initialize-insecure \
  28. --user=mysql \
  29. --basedir=/usr/local/mysql \
  30. --datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
  31. ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
  32. VOLUME [ "/usr/local/mysql" ]
  33. CMD ["/usr/sbin/init"]
  1. [client]
  2. port = 3306
  3. default-character-set=utf8
  4. socket = /usr/local/mysql/mysql.sock
  5. [mysql]
  6. port = 3306
  7. default-character-set=utf8
  8. socket = /usr/local/mysql/mysql.sock
  9. [mysqld]
  10. user = mysql
  11. basedir = /usr/local/mysql
  12. datadir = /usr/local/mysql/data
  13. port = 3306
  14. character_set_server=utf8
  15. pid-file = /usr/local/mysql/mysqld.pid
  16. socket = /usr/local/mysql/mysql.sock
  17. server-id = 1
  18. 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镜像

  1. vim Dockerfile
  2. FROM centos:7
  3. MAINTAINER this is php image <kcy>
  4. RUN yum install -y gd \
  5. libjpeg libjpeg-devel \
  6. libpng libpng-devel \
  7. freetype freetype-devel \
  8. libxml2 libxml2-devel \
  9. zlib zlib-devel \
  10. curl curl-devel \
  11. openssl openssl-devel \
  12. gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
  13. ADD php-7.1.10.tar.bz2 /usr/local/src/
  14. WORKDIR /usr/local/src/php-7.1.10
  15. RUN ./configure \
  16. --prefix=/usr/local/php \
  17. --with-mysql-sock=/usr/local/mysql/mysql.sock \
  18. --with-mysqli \
  19. --with-zlib \
  20. --with-curl \
  21. --with-gd \
  22. --with-jpeg-dir \
  23. --with-png-dir \
  24. --with-freetype-dir \
  25. --with-openssl \
  26. --enable-fpm \
  27. --enable-mbstring \
  28. --enable-xml \
  29. --enable-session \
  30. --enable-ftp \
  31. --enable-pdo \
  32. --enable-tokenizer \
  33. --enable-zip ; make -j 4 ; make install
  34. ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
  35. ADD php.ini /usr/local/php/lib/
  36. ADD php-fpm.conf /usr/local/php/etc/
  37. ADD www.conf /usr/local/php/etc/php-fpm.d/
  38. EXPOSE 9000
  39. 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

  1. [root@ebafbdc1c4dc bin]# mysql -u root -p
  2. Enter password:
  3. #初始密码为空
  4. mysql> create database wordpress;
  5. Query OK, 1 row affected (0.00 sec)
  6. mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
  7. Query OK, 0 rows affected, 1 warning (0.00 sec)
  8. mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
  9. Query OK, 0 rows affected, 1 warning (0.00 sec)
  10. mysql> flush privileges;
  11. Query OK, 0 rows affected (0.00 sec)

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

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

五、安装Compose

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

六、准备环境

  1. [root@centos6 opt]# mkdir docker-compose_nginx
  2. [root@centos6 opt]# cd docker-compose_nginx/
  3. [root@centos6 docker-compose_nginx]# mkdir wwwroot
  4. [root@centos6 docker-compose_nginx]# ls
  5. wwwroot

  1. [root@centos5 wordpress]# cd /opt
  2. [root@centos5 opt]# ls
  3. containerd docker-compose_lnmp mysqld nginx php rh
  4. [root@centos5 opt]# cp -r nginx/ /opt/docker-compose_lnmp/
  5. [root@centos5 opt]# cd docker-compose_lnmp/
  6. [root@centos5 docker-compose_lnmp]# ls
  7. nginx wwwroot
  8. [root@centos5 docker-compose_lnmp]# cd ..
  9. [root@centos5 opt]# cp -r mysqld/ /opt/docker-compose_lnmp/
  10. [root@centos5 opt]# cp -r php/ /opt/docker-compose_lnmp/
  11. [root@centos5 opt]# cd docker-compose_lnmp/
  12. [root@centos5 docker-compose_lnmp]# ls
  13. mysqld nginx php wwwroot
  14. [root@centos5 docker-compose_lnmp]#

七、编写docker-compose.yml

  1. version: '3'
  2. services:
  3. nginx:
  4. container_name: nginx
  5. hostname: nginx
  6. build:
  7. context: ./nginx
  8. dockerfile: Dockerfile
  9. ports:
  10. - 1212:80
  11. - 1616:443
  12. networks:
  13. lnmp:
  14. ipv4_address: 172.19.0.10
  15. volumes:
  16. - ./wwwroot:/usr/local/nginx/html
  17. mysql:
  18. container_name: mysql
  19. build:
  20. context: ./mysqld
  21. dockerfile: Dockerfile
  22. ports:
  23. - 3306:3306
  24. networks:
  25. lnmp:
  26. ipv4_address: 172.19.0.20
  27. volumes:
  28. - db-data:/usr/localmysql
  29. privileged: true
  30. php:
  31. container_name: php
  32. build:
  33. context: ./php
  34. dockerfile: Dockerfile
  35. ports:
  36. - 9000:9000
  37. networks:
  38. lnmp:
  39. ipv4_address: 172.19.0.30
  40. volumes:
  41. - db-data:/usr/local/mysql
  42. - ./nginx/html:/usr/local/nginx/html
  43. depends_on:
  44. - nginx
  45. - mysql
  46. networks:
  47. lnmp:
  48. driver: bridge
  49. ipam:
  50. config:
  51. - subnet: 172.19.0.0/16
  52. volumes:
  53. db-data:

八、启动并运行

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

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

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

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

  1. docker exec -it mysql /bin/bash
  2. create database wordpress;
  3. grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
  4. grant all privileges on *.* to 'root'@'%' identified by 'abc123';
  5. flush privileges;

九、浏览器访问

http://192.168.91.105/wordpress/index.php

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

闽ICP备14008679号