Ubuntu 16.04下Docker部署SpringBoot、Mysql、Redis、Nginx和Vue

本文以开源项目SpringBlade和Saber为例。

1、创建自定义网络
目的是将用到的服务都放到同一个网络段,以方便互相通信。
docker network create --subnet 172.19.0.0/16 mynetwork

2、Docker安装MySQL、Redis、Nginx
(1)安装MySQL
docker pull mysql:5.7.30
cd ~
mkdir docker/mysql/{conf,logs,data} -p
cd docker/mysql
docker run --name mysql_blade --network=mynetwork --ip=172.19.0.6 -p 3307:3306 -v PWD/logs:/var/log/mysql -v PWD/conf:/etc/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.30 (2)安装Redis docker pull redis cd ~ mkdir docker/redis/redis-6389/{conf,data} -p cd docker/redis/redis-6389 wget https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf -O conf/redis.conf sed -i 's/logfile PWD/data:/data:rw -v $PWD/conf/redis.conf:/etc/redis/redis.conf:ro --privileged=true --name redis-6389 -d redis redis-server /etc/redis/redis.conf
(3)安装Nginx
将Saber发布到Nginx中要用到
docker pull nginx
2、Docker打包SpringBlade

3、Docker打包并发布Saber
(1)编写nginx.conf
cd Saber
touch nginx.conf,写入以下内容:
//nginx.conf开始
user root;
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;

upstream gateway {
             server 172.19.0.7:9001;
         }


server {
  listen       1889;
  server_name  web;
  root         /usr/share/nginx/html;

  location / {
       try_files $uri $uri/ /index.html;

  }

  location ^~/api {
       proxy_set_header Host $host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_buffering off;
       rewrite ^/api/(.*)$ /$1 break;
       proxy_pass http://gateway;
  }
}

}
//nginx.conf结束
(2)修改env.js文件
baseUrl要与下面的SpringBlade容器的地址和端口匹配:


image.png

(3)编写Dockerfile
编写dockerfile并将其放到与dist同一目录:
FROM nginx
VOLUME /tmp
ENV LANG en_US.UTF-8
ADD ./dist/ /usr/share/nginx/html/
COPY./nginx.conf /etc/nginx/
EXPOSE 1889
EXPOSE 443
(4)打包并发布
cd ~
mkdir docker/saber/conf -p
cd docker/saber
cp ~/Saber/nginx.conf conf
yarn run build
docker build -t saber:1.0 .(注意最后的.)
docker run -itd --name saber --network=mynetwork --ip=172.19.0.8 -p 1889:1889 -v $PWD/conf:/mnt/ saber:1.0

4、Docker打包并发布SpringBlade
(1)pom.xml配置
/
10.10.0.127:10080
blade
0.0.1
1.4.10


SpringBlade


src/main/resources


src/main/java

*/.xml






org.springframework.boot
spring-boot-maven-plugin
{project.build.finalName}




repackage





com.spotify
dockerfile-maven-plugin
{project.basedir}

true
{docker.registry.name}/{docker.image.tag}

target/{project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>{java.version}
${java.version}
UTF-8

-parameters




(2)yml配置
redis及mysql都要与前面的创建容器时的配置相同:


image.png

(3)创建私有仓库(利用Harbor)
在harbor管理界面创建项目blade,下面上传镜像的时候要加入项目路径。
(4)打包
mvn clean install dockerfile:build -Dmaven.test.skip=true
(5)上传到私有仓库
两种方式:
mvn dockerfile:push
或者docker push 10.10.0.127:10080/blade/springblade:0.0.1
然后在Harbor管理后台就可以看到镜像了。
要pull下来的话:
docker pull 10.10.0.127:10080/blade/springblade:0.0.1
(6)发布
cd ~
mkdir docker/blade/app -p
cd docker/blade
docker run -itd --name blade --network=mynetwork --ip=172.19.0.7 -p 9001:9001 -v $PWD/app:/mnt/ 10.10.0.127:10080/blade/springblade:0.0.1

至此,就可以通过localhost:1889来访问Saber了。

另外,我通过uri来区分多租户。例如localhost:1889是管理租户,localhost:1889/test是名为test的租户。这样就避免了在登录界面填写租户id。

你可能感兴趣的:(Ubuntu 16.04下Docker部署SpringBoot、Mysql、Redis、Nginx和Vue)