docker(mysql+springboot+nginx)项目单宿主机部署方案

docker(mysql+springboot+nginx)项目单宿主机部署方案

微服务单宿主机情况下通过docker部署mysql+springboot+nginx 前后端分离web项目,

省去了安装nginx mysql 和jdk环境的配置

如有必要可以集成到容器管理平台 kubernetes spark等

 

一  .    安装docker

1.更新依赖

yum update

必须支持 以下以centos 7系统为例

2.安装 

yum -y install docker

3.启动docker 并设置开机自启动

service docker start

4.设置开机启动

systemctl enable docker

5.配置加速器 不配置会导致镜像下载缓慢

vi /etc/sysconfig/docker

尾行添加 此处采用腾讯云镜像加速器,也可采用daocloud,alibaba的

OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'

 

以下镜像均以daoclou.io为准

二 .    安装mysql

docker run --name zzfw-mysql -d -p 3306:3306 -P -e mysqld -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_USER=chengde_admin -e MYSQL_PASSWORD=chengde_admin -e MYSQL_DATABASE=chengde_db -v /mysql_data:/var/lib/mysql daocloud.io/library/mysql:5.7.4

--name zzfw-mysql 容器别名 便于查找

-p 3306:330 绑定对外端口3306 

root账号密码123456

默认数据库chengde_db 

默认账号密码:chengde_admin/chengde_admin

映射mysql相关配置及数据库 从容器/var/lib/mysql到宿主机/mysql_data

 

三.部署springboot应用

1.准备springboot.jar包 chengde-server-1.0-SNAPSHOT.jar  maven打包

2.准备DockerFile文件

FROM daocloud.io/library/java:openjdk-8u40



MAINTAINER v3ksing



RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone



ADD target/chengde-server-1.0-SNAPSHOT.jar /opt/chengde-server/target/



WORKDIR /opt/chengde-server/



EXPOSE 9090



ENV LANG C.UTF-8



CMD ["java","-jar","-Djava.security.egd=file:/dev/./urandom","target/chengde-server-1.0-SNAPSHOT.jar","--spring.profiles.active=prod"]

该文件包含docker镜像,时区,编码等,端口,启动命令等设置

 

3.上传jar包和Dockerfile文件,目录结构如下

--opt

----chengde-server

------Dockerfile

------target

--------chengde-server-1.0-SNAPSHOT.jar

 

4.执行镜像构建

cd /opt/chengde-server

docker build -t chengde-server:1.0 .

 

注意末尾句号不能少,如有需要可以docker push到镜像仓库

 

5.构建完毕 启动springbot镜像,绑定端口9090并映射日志文件到/opt/chengde-server/log

docker run --name chengde-server -p 9090:9090 -v /opt/chengde-server/log:/opt/chengde-server/log -t chengde-server:1.0

显示springboot日志表示启动成功

 

四.部署nginx web应用

 

静态文件可以通过Dockerfile构建镜像是一并打入,

这里仅单台部署,静态文件映射到单台宿主机

 

1.创建以下目录

mkdir /opt/nginx/conf.d

mkdir /opt/nginx/html

mkdir /opt/nginx/nginx.conf

2.nginx配置文件

文件示例:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
	
	location / {
		proxy_pass http://172.16.6.114:9090;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}

	location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|svg|woff2|woff|ttf|js\.map|css\.map)$ {
		index index.html;
		root /usr/share/nginx/html;
	}

    #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   /usr/share/nginx/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           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
    #}
}

 default.conf server节点

添加内容 

    location / {

        proxy_pass http://172.16.6.114:9090;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }



    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|svg|woff2|woff|ttf|js\.map|css\.map)$ {

        index index.html;

        root /usr/share/nginx/html;

    }

上传nginx配置文件default.conf 到/opt/nginx/conf.d目录

上传nginx配置文件nginx.conf 到/opt/nginx/nginx.conf目录

nginx.conf 暂不修改

 


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

}

3.上传前端打包好的项目到/opt/nginx/html 目录下

docker(mysql+springboot+nginx)项目单宿主机部署方案_第1张图片

app.html为应用端项目,

admin.html为后台管理端项目

 

4.启动nginx 

docker run --name my-nginx -d -p 80:80 -v /opt/nginx/html:/usr/share/nginx/html:ro -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf.d:/etc/nginx/conf.d:ro -v /opt/nginx/nginx.conf/nginx.conf:/etc/nginx/nginx.conf:ro -d daocloud.io/library/nginx:1.13.0-alpine

映射端口 

映射静态文件路径/opt/nginx/html

日志/opt/nginx/logs

配置文件等

 

可访问页面:

app端:http://172.16.6.114:81/app.html#/pre 

admin端:http://172.16.6.114:81/admin.html#/ 

此时静态页面完毕 但是后台接口504 bad gateway

需要在下一步开启防火墙端口

 

备注:重启nginx命令 

docker restart my-nginx

 

五.开启防火墙端口 centOS7

 

添加nginx,springboot端口,此处可优化9090采用内容容器访问,不对外开发

firewall-cmd --add-port=80/tcp  --permanent

firewall-cmd --add-port=9090/tcp  --permanent

 

其他命令:

#查看防火墙状态

firewall-cmd --state

#查看端口

firewall-cmd --zone=public --list-ports

#关闭端口

firewall-cmd --remove-port=80/tcp --permanent

 

springboot接口可访问,部署完毕

 

详细docker命令参考

https://www.gitbook.com/book/yeasy/docker_practice/details

 

 

你可能感兴趣的:(docker)