简介
Dockerfile 是一个文本文件,其内包含了一条条的指令(Instruction),用于构建镜像。每一条指令构建一层镜像,因此每一条指令的内容,就是描述该层镜像应当如何构建。
准备工作
vue项目打包
- 在项目根目录下创建 3 个文件分别为
nginx.conf
Dockerfile
run.sh
- nginx.conf 下的 location -> root 的静态资源目录要和 Dockerfile 中 COPY 指定的路径要一致
COPY dist /usr/share/nginx/html
指的把当前目录的 dist 复制到 /usr/share/nginx/html 下面
- Dockerfile 中
COPY nginx.conf /etc/nginx/conf.d/default.conf
意思是用我们自己的 nginx.conf 覆盖掉默认的
- 关于这三个配置文件,其实也可以用文本编辑器创建然后,直接上传到 Linux 服务器上,我放在项目里面是因为,如果文件有变更(一般来说配置一遍就很少变了)可以通过后面的步骤一键快速上传到 Linux 服务器,看个人偏好吧,如果放在项目里面也有可能其它的小伙伴编辑到了就不好了
Nginx配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| server { listen 80; server_name localhost;
access_log /var/log/nginx/host.access.log main; error_log /var/log/nginx/error.log error;
location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
|
Dockerfile
1 2 3
| FROM nginx COPY dist /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf
|
构建脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| img=front-image tag=1.0 container=vueDocker inPort=80 exPort=8081
if sudo docker ps -a --filter name=^/$container$ | grep -i "$container"; then echo "容器: $container 存在,删除容器..." sudo docker stop "$container" sudo docker rm -f "$container" else echo "容器: $container 不存在" fi
if sudo docker ps -a --filter name=^/$container$ | grep -i "$container"; then echo "容器: $container 存在,删除容器..." sudo docker stop "$container" sudo docker rm -f "$container" else echo "容器: $container 不存在" fi
if sudo docker images --filter=reference="$img:$tag" | grep -i "$img"; then echo "镜像: $img:$tag 镜像存在, 删除镜像重新构建..." sudo docker rmi $img:$tag fi
echo "构建新镜像: $img:$tag ..." sudo docker build -t $img:$tag .
echo "启动 $container 容器服务!" sudo docker run -d -p $exPort:$inPort --restart=always --name $container $img:$tag
if sudo docker ps --filter name=^/$container$ | grep -i "up"; then echo "容器 $container 启动成功Succeed" else echo "容器 $containe 启动失败Fail" fi
|
通过一键执行上面配置的的流程,将项目打包并且上传到服务器
- 需要在刚才文件上传的目录下进行执行命令
- 服务器不需要提前去拉取 Nginx,Dockerfile 中配置了会自动拉取
- 服务器需要开启对应的 Nginx 端口和 对外暴露的 端口,比如这里的 80、8081
- 按照上述步骤,其实比较繁琐,需要在本地的 IDEA 中执行触发上传,然后登录到服务器上,再分别执行两个命令,一个是打镜像的命令,一个是启动镜像的命令
- 可优化为:我们将服务器上打 docker image 镜像和启动 docker Container 容器做成一个 sh 脚本,然后 IDEA 上传完成后自动执行该 sh 脚本
- 上传 如下脚本 run.sh 和 dist/Dockerfile/nginx.conf 到同一目录后,并给与可执行权限
chmod 755 run.sh
优化启动脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| img=front-image tag=1.0 container=vueDocker inPort=80 exPort=8081
if sudo docker ps -a --filter name=^/$container$ | grep -i "$container"; then echo "容器: $container 存在,删除容器..." sudo docker stop "$container" sudo docker rm -f "$container" else echo "容器: $container 不存在" fi
if sudo docker ps -a --filter name=^/$container$ | grep -i "$container"; then echo "容器: $container 存在,删除容器..." sudo docker stop "$container" sudo docker rm -f "$container" else echo "容器: $container 不存在" fi
if sudo docker images --filter=reference="$img:$tag" | grep -i "$img"; then echo "镜像: $img:$tag 镜像存在, 删除镜像重新构建..." sudo docker rmi $img:$tag fi
echo "构建新镜像: $img:$tag ..." sudo docker build -t $img:$tag .
echo "启动 $container 容器服务!" sudo docker run -d -p $exPort:$inPort --restart=always --name $container $img:$tag
if sudo docker ps --filter name=^/$container$ | grep -i "up"; then echo "容器 $container 启动成功Succeed" else echo "容器 $containe 启动失败Fail" fi
|
SpringBoot 打包
- 其实和上面的 vue 都是一样的,先编译 vue 是生成 dist 目录,SpringBoot 是 打成 jar 包
- SpringBoot 不依赖
Nginx
,依赖 Java
- 流程和 vue 的基本一致,就是换个需要上传的文件路径
- 上传前的打包指令根据自己的项目配置一下
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| FROM openjdk:17-jdk-alpine
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
VOLUME /tmp
ADD helper.jar adalucky_helper.jar
EXPOSE 9999
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/adalucky_helper.jar"]
MAINTAINER ada
|
构建脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| img=adalucky_helper tag=1.0 container=adalucky_helper inPort=9999 exPort=9999
if sudo docker ps -a --filter name=^/$container$ | grep -i "$container"; then echo "容器: $container 存在,删除容器..." sudo docker stop "$container" sudo docker rm -f "$container" else echo "容器: $container 不存在" fi
if sudo docker ps -a --filter name=^/$container$ | grep -i "$container"; then echo "容器: $container 存在,删除容器..." sudo docker stop "$container" sudo docker rm -f "$container" else echo "容器: $container 不存在" fi
if sudo docker images --filter=reference="$img:$tag" | grep -i "$img"; then echo "镜像: $img:$tag 镜像存在, 删除镜像重新构建..." sudo docker rmi $img:$tag fi
echo "构建新镜像: $img:$tag ..." sudo docker build -t $img:$tag .
echo "启动 $container 容器服务!" sudo docker run -d -p $exPort:$inPort --restart=always --name $container $img:$tag
if sudo docker ps --filter name=^/$container$ | grep -i "up"; then echo "容器 $container 启动成功Succeed" else echo "容器 $containe 启动失败Fail" fi
|
- 专门创建一个配置文件目录,记得 run.sh 的权限需要 改成可执行权限
- 选择刚才的配置,点击对应旁边的执行按钮即可