docker compose部署wordpress

准备机器:

192.168.58.151 (关闭防火墙和selinux)

安装好docker服务 (详细参照:http://t.csdnimg.cn/usG0s 中的国内源安装docker)

部署wordpress:

创建目录:

[root@docker ~]# mkdir wordpress
[root@docker ~]# ls
anaconda-ks.cfg        wordpress       ip_fixed.sh      

获取wordpress的安装包: cn.wordpress.org

服务器端下载:

[root@docker ~]# cd wordpress/
[root@docker wordpress]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz

修改nginx.conf文件,并将其放在wordpress目录下

[root@docker wordpress]# cat nginx.conf 
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
 
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
 
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
 
events {
    worker_connections 1024;
}
 
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
 
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
 
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
 
server {
        listen      80;
        server_name www.itnzs.top;
        location / {
                        root /usr/share/nginx/wordpress;
                        index index.php;
                }
        location ~ \.php$ {
            root          /usr/share/nginx/wordpress;  #指定网站目录
            fastcgi_pass   127.0.0.1:9000;    #指定访问地址
            fastcgi_index  index.php;           #指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
            include        fastcgi_params;  #包含nginx常量定义
                        }
        }
 
# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
 
}

在wordpress目录下创建Dockerfile

[root@docker wordpress]# vim Dockerfile 
# 设置基础镜像
FROM daocloud.io/library/centos:7
 
# 安装环境
RUN yum install -y epel-release \
; yum install -y nginx \
; yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm \
; yum install -y php80-php-xsl php80-php php80-php-cli php80-php-devel php80-php-gd php80-php-pdo php80-php-mysql php80-php-fpm \
; yum clean all
 
# 将代码包拷贝到镜像中
# RUN wget https://cn.wordpress.org/latest-zh_CN.tar.gz
ADD latest-zh_CN.tar.gz /usr/share/nginx
 
# 将配置文件拷贝到镜像中
COPY nginx.conf /etc/nginx/
 
# 将启动脚本拷贝到镜像中
COPY docker-entrypoint.sh /
 
# 在启动容器时执行脚本
CMD ["sh","/docker-entrypoint.sh"]

创建docker-entrypoint.sh文件并编辑,其目的是执行启动服务并将其hang住。

[root@docker wordpress]# vim docker-entrypoint.sh 
chmod 777 /usr/share/nginx/wordpress -R
nginx
/opt/remi/php80/root/usr/sbin/php-fpm
echo "nginx 和 php 启动成功"
tail -f /var/log/nginx/*

创建docker-compose.yaml文件,并进行编辑

[root@docker wordpress]# vim docker-compose.yaml 
version: '3'
services:
  wordpress:
    build: .
    container_name: wordpess-web
    ports:
      - "80:80/tcp"
      - "443:443/tcp"
    links:
      - mysql:wordpess.db.com
    depends_on:
      - mysql
 
  mysql:
    image: mysql:5.7
    container_name: wordpess-db
    environment:
      - MYSQL_ROOT_PASSWORD=123456
      - MYSQL_DATABASE=wordpress
    volumes:
      - /opt/wordpress/:/var/lib/mysql

查看文件及镜像

[root@docker wordpress]# ls
docker-entrypoint.sh  Dockerfile  latest-zh_CN.tar.gz  nginx.conf  docker-compose.yaml
 
[root@docker wordpress]# docker pull daocloud.io/library/centos:7
[root@docker wordpress]# docker pull mysql:5.7
[root@docker wordpress]# docker images
REPOSITORY                                               TAG       IMAGE ID       CREATED         SIZE
daocloud.io/library/centos                               7         7e6257c9f8d8   3 years ago     203MB
mysql                                                    5.7       c20987f18b13   23 months ago   448MB

使用docker compose命令进行启动

[root@docker wordpress]# docker compose up
[+] Building 0.0s (0/0)                                                              
[+] Running 3/3
 ✔ Network wordpress_default  Created                                           0.1s 
 ✔ Container wordpess-db      Created                                           0.1s 
 ✔ Container wordpess-web     Created                                           0.1s 
Attaching to wordpess-db, wordpess-web
····
wordpess-web  | nginx 和 php 启动成功
wordpess-web  | ==> /var/log/nginx/access.log <==
wordpess-web  | 
wordpess-web  | ==> /var/log/nginx/error.log <==

另启一个端口查询端口

[root@docker ~]# ss -nplt | grep 80
LISTEN     0      128          *:80                       *:*                   users:(("docker-proxy",pid=26740,fd=4))
LISTEN     0      128       [::]:80                    [::]:*                   users:(("docker-proxy",pid=26746,fd=4))

浏览器访问 ip (192.168.58.151)

获取数据库ip (下图中的IPAddress: 172.19.0.2)

[root@docker ~]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                   CREATED         STATUS         PORTS                                                                      NAMES
5d29b8a45292   wordpress-wordpress   "sh /docker-entrypoi..."   4 minutes ago   Up 4 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   wordpess-web
0251a7f3cd9c   mysql:5.7             "docker-entrypoint.s..."   4 minutes ago   Up 4 minutes   3306/tcp, 33060/tcp                                                        wordpess-db
 
[root@docker ~]# docker inspect 0
······
                    "NetworkID": "1f2731089eddc996fec23766d403d49a4a7999eaedeadab0ebabeffbf4ad80e4",
                    "EndpointID": "158d94250df1b4e69aac769f9834f7a74ab7054fc995108bef873af5657d9115",
                    "Gateway": "172.19.0.1",
                    "IPAddress": "172.19.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:13:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@docker ~]# 

浏览器端进行填写

后续操作可按需进行操作即可。

相关推荐
cdut_suye1 小时前
基于Linux环境的进度条实现
linux·运维·服务器·数据库·人工智能·深度学习·中间件
daydayup95271 小时前
FTP服务
linux·运维·服务器
dessler1 小时前
Docker-番外篇之下载镜像
linux·运维·docker
神秘的土鸡1 小时前
如何通过 Nginx 配置防盗链保护静态资源(详细配置)
linux·运维·nginx
一张假钞1 小时前
Nginx 配置支持 HTTPS 代理
运维·nginx
云川之下1 小时前
【k8s】用户和服务账户联系(user、serviceaccount、sa)
云原生·容器·kubernetes
0709004 小时前
docker更换镜像源脚本
java·python·docker·容器
ks胤墨4 小时前
本地服务器Docker搭建个人云音乐平台Splayer并实现远程访问告别烦人广告
运维·服务器·docker
木子欢儿4 小时前
服务器多节点 Grafana、Prometheus 和 Node-Exporter Docker版本部署指南
服务器·docker·eureka·grafana·prometheus
刘争Stanley4 小时前
容器技术全面攻略:Docker的硬核玩法
运维·python·docker·容器·柔性数组