一键化部署docker-ce容器化lnmp运行wordpress

前言

这段时间潜心研究docker,想写点东西出来,最后写了个容器话运行的wordpress。
镜像还有脚本我打包了,挂在我的服务器上面,有时间的可以下载下来玩一下。

docker

我把打包的文件放在了服务器上,下载速度可能有点慢。

wget http://www.seeit.life/tools/lnmp.tar #这个是镜像包还有启动脚本
wget http://www.seeit.life/tools/startlnmp.sh #这是解压镜像还有启动tar包里面的启动脚本

全程自动化哦,只有最后的你要自己设置帐号密码。
只要

bash startlnmp.sh

dockerfile

下面我把dockerfile文件放出来,并且还有用到了哪些文件。

[ root@node3 ~/lnmp ]# ls
fpm  mariadb  nginx
[ root@node3 ~/lnmp ]# cd fpm/
[ root@node3 ~/lnmp/fpm ]# ls
CentOS-Base.repo  Dockerfile  epel.repo

Centos-Base.repo epel.repo文件我把里面的地址改成了自己的私有仓库速度会快一点。
不改也没关系的。

[ root@node3 ~/lnmp/fpm ]# cat Dockerfile 
# php-fpm server
FROM centos:7.4.1708

MAINTAINER "Rootygl "

COPY CentOS-Base.repo \ 
epel.repo /etc/yum.repos.d/

RUN yum -y install php-fpm php-mbstring php-mysql; \
    yum clean all; \
    rm -rf /var/cache/yum/*;

VOLUME "/webapp/wordpress"

CMD ["/usr/sbin/php-fpm","--nodaemonize"]
[ root@node3 ~/lnmp/mariadb ]# ls
CentOS-Base.repo  Dockerfile  epel.repo  start-db.sh

mariadb的Dockerfile文件

[ root@node3 ~/lnmp/mariadb ]# cat Dockerfile 
# Mariadb-server
FROM centos:7.4.1708

MAINTAINER "Rootygl "

COPY CentOS-Base.repo \ 
epel.repo /etc/yum.repos.d/

COPY start-db.sh /usr/bin/

RUN yum -y install mariadb-server; \ 
    yum clean all; \ 
    rm -rf /var/cache/yum/*;

VOLUME "/var/lib/mysql"

ENTRYPOINT ["/bin/bash","-c","/usr/bin/start-db.sh"]
[ root@node3 ~/lnmp/mariadb ]# cat start-db.sh 
#!/bin/bash
#
/usr/libexec/mariadb-prepare-db-dir

/usr/bin/mysqld_safe --basedir=/usr 

因为要修改里面的nginx.conf的配置文件反带到9000端口,我写在外面直接覆盖里面的就好了。

[ root@node3 ~/lnmp/nginx ]# ls
Dockerfile  nginx.conf
[ root@node3 ~/lnmp/nginx ]# cat Dockerfile 
#nginx server

FROM centos:7.4.1708

MAINTAINER "Rootygl @163.com>" 

ENV nginx_version 1.12.2
ENV nginx_url http://nginx.org/download/nginx-${nginx_version}.tar.gz

WORKDIR "/usr/local/src/"

EXPOSE 80/tcp

VOLUME "/webapp/wordpress" 

ADD ${nginx_url} /usr/local/src/


RUN tar xf nginx-${nginx_version}.tar.gz && yum -y install gcc pcre-devel openssl-devel make && yum clean all && rm -rf /var/cache/yum/* \ 
    && cd nginx-${nginx_version} && ./configure && make && make install 

COPY nginx.conf /usr/local/nginx/conf/
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
[ root@node3 ~/lnmp/nginx ]# cat nginx.conf 

#user  nobody;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  5;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /webapp/wordpress;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /webapp/wordpress;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

我没有使用docker-compose编排容器写yam文件,比较简单也懒得写了,就集成到了启动脚本上了。

[ root@localhost ~/lnmp ]# ls
docker-lnmp.sh  fpmv1.0.tar  mariadbv1.0.tar  nginxv1.0.tar  wordpress-4.7.4-zh_CN.tar.gz

三个docker导出的镜像加wordpress的压缩包,还有启动初始化脚本。

[ root@localhost ~/lnmp ]# cat docker-lnmp.sh 
#!/bin/bash

#nginx php-fpm mariadb in container
#MAINTAINER "Rootygl "


yum install -y wget > /dev/null 

wget https://download.docker.com/linux/centos/docker-ce.repo

mv docker-ce.repo /etc/yum.repos.d/

yum clean all && yum repolist all > /dev/null

yum -y install docker-ce 

mkdir -p /etc/docker

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://us67o6c8.mirror.aliyuncs.com"]
}
EOF

systemctl restart docker 

#wget 

docker load -i fpmv1.0.tar 
docker load -i nginxv1.0.tar 
docker load -i mariadbv1.0.tar 

docker run --name n1 -P -p80:80 -d nginx:v1.0
docker run --name fpm1 -d --volumes-from n1 --net container:n1 fpm:v1.0
docker run --name db1 --net container:fpm1 -d mariadb:v1.0

nginxworkdir=`docker inspect -f {{.Mounts}} n1 | cut -d " " -f 3`
mariadbworkdir=`docker inspect -f {{.Mounts}} db1 | cut -d " " -f 3`
tar xf wordpress-4.7.4-zh_CN.tar.gz 

mv wordpress/* $nginxworkdir

mkdir $mariadbworkdir/wordpress/

chown -R 27:27 $mariadbworkdir/wordpress/

cp $nginxworkdir/wp-config-sample.php $nginxworkdir/wp-config.php
sed -i s/database_name_here/wordpress/ $nginxworkdir/wp-config.php
sed -i s/username_here/root/ $nginxworkdir/wp-config.php
sed -i s/password_here// $nginxworkdir/wp-config.php
sed -i s/'localhost'/'127.0.0.1'/ $nginxworkdir/wp-config.php


echo '数据库名:  wordpress'
echo '用户名:    root'
echo '密码:      (空)'
echo '数据库主机:127.0.0.1' 

最后

基本上东西都放在这里了。
docker容器技术可以大大的减轻很多重复的工作,
一次创建,到处运行。资源弹性化利用…

你可能感兴趣的:(容器)