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

本文以開源項(xiàng)目SpringBlade和Saber為例。

1、創(chuàng)建自定義網(wǎng)絡(luò)
目的是將用到的服務(wù)都放到同一個(gè)網(wǎng)絡(luò)段,以方便互相通信。
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/data:/var/lib/mysql -vPWD/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 ""/logfile "access.log"/' conf/redis.conf sed -i 's/# requirepass foobared/requirepass 123456/' conf/redis.conf sed -i 's/appendonly no/appendonly yes/' conf/redis.conf sed -i 's/protected-mode yes/protected-mode no/' conf/redis.conf vi conf/redis.conf并將bind 127.0.0.1注釋掉 docker run --network=mynetwork --ip=172.19.0.5 -p 6389:6379 -vPWD/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發(fā)布到Nginx中要用到
docker pull nginx
2、Docker打包SpringBlade

3、Docker打包并發(fā)布Saber
(1)編寫nginx.conf
cd Saber
touch nginx.conf,寫入以下內(nèi)容:
//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結(jié)束
(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)打包并發(fā)布
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打包并發(fā)布SpringBlade
(1)pom.xml配置
/
<docker.repostory>10.10.0.127:10080</docker.repostory>
<docker.registry.name>blade</docker.registry.name>
<docker.image.tag>0.0.1</docker.image.tag>
<docker.maven.plugin.version>1.4.10</docker.maven.plugin.version>

<build>
<finalName>SpringBlade</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>*/.xml</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{spring.boot.version}</version> <configuration> <finalName>{project.build.finalName}</finalName>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>{docker.maven.plugin.version}</version> <executions> <execution> <id>default</id> <goals> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> <configuration> <contextDirectory>{project.basedir}</contextDirectory>

<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
<repository>{docker.repostory}/{docker.registry.name}/{project.artifactId}</repository> <tag>{docker.image.tag}</tag>
<buildArgs>
<JAR_FILE>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}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

(2)yml配置
redis及mysql都要與前面的創(chuàng)建容器時(shí)的配置相同:


image.png

(3)創(chuàng)建私有倉庫(利用Harbor)
在harbor管理界面創(chuàng)建項(xiàng)目blade,下面上傳鏡像的時(shí)候要加入項(xiàng)目路徑。
(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)發(fā)布
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來區(qū)分多租戶。例如localhost:1889是管理租戶,localhost:1889/test是名為test的租戶。這樣就避免了在登錄界面填寫租戶id。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容