docker的问世,让开发者可以快速的部署自己的应用,如果出现的故障,可以通过镜像,快速恢复服务。让更多的开发人员可以自己实现部署,但是对于以前的不学习的部署人员可能面临的就是失业。本文章教你快速部署你的项目。
docker部署前后分离项目
(1)部署前端vue项目
前端vue项目我一般部署在nginx服务器上,当我们的项目在本地服务器可以跑通的时候就可以部署到我们ESC服务器上了。
1.使用npm run build打包我们的项目,生成(默认的)dist文件夹,vue3.0之后不用做任何修改。
2.把该文件夹上传到我们的ESC服务器的 /home文件夹下
3.在/home文件夹下,创建Dockerfile文件
# 设置基础镜像
FROM nginx
# 定义作者
MAINTAINER dangle
# 将dist文件中的内容复制到 /usr/share/nginx/html/ 这个目录下面
COPY dist/ /usr/share/nginx/html/
#将nginx.conf的内容复制
COPY nginx.conf /etc/nginx/nginx.conf
RUN echo 'echo init ok!!'
4./home文件夹下,创建nginx.conf文件
#user nobody;
worker_processes 1;
#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 65;
#gzip on;
server{
listen 8080; #服务器的端口号
server_name 60.205.231.39; #IP或者域名,localhost:127.0.0.1 本地的IP
location / {
try_files $uri $uri/ /index.html; #针对vue项目的history路由模式
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 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;
# }
#}
}
5.在ESC服务器中进入我们的/home文件夹
6.构建vue镜像
#输入命令
docker build -t store_vue:0.0.1 .
#store_vue是镜像的名字 0.0.1是版本号
#注意后面的 .
#查看我们的镜像
7.使用docker run就可以启动我们的镜像容器了
(2)部署后端spring boot项目
1.用maven去构建我们的项目
2.修改宿主机的docker配置,让其可以远程访问
vi /lib/systemd/system/docker.service
其中ExecStart=后添加配置 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
3.在pom.xml中配置dockermaven插件
<build>
<finalName>app</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<!--docker的maven插件,官网: https://github.com/spotify/docker‐maven‐plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.3.14</version>
<configuration>
<!--docker私有仓库生成镜像名字-->
<imageName>${project.artifactId}:${project.version} </imageName>
<!--baseImage基于镜像的名字,docker里面的jdk镜像名字-->
<baseImage>registry.cnqingdao.aliyuncs.com/dangle/jdk:1.8</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<!--docker地址-->
<dockerHost>http://60.205.231.39:2375</dockerHost>
</configuration>
</plugin>
</plugins>
</build>
4.命令构建docker镜像
#在我们的idea终端界面输入命令
mvn clean package docker:build
#镜像生成之后我们就可以启动我们的容器了,访问一下试试吧!