使用docker打包前端项目详解

【注】强烈不建议使用windows使用docker,会出各种各样奇怪的错误,没有意义
本文中采用阿里云的linux服务器,打包和推送镜像到阿里云远端(本机是windows)

首先准备好你要打包的前端项目在这里插入图片描述
图中的dist就是你打包的前端项目,然后,你需要编写两个文件,default.conf和Dockerfile
default.conf是nginx的配置文件

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;    # 这个非常重要,采用vue-router的时候,必须配置这个
  }
 
  #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;
  #}
}

直接复制即可,注意本文没有配置代理转发,有需要的可以自行配置
然后编写Dockerfile文件

server {
  listen       80;
 
  #charset koi8-r;
  access_log  /var/log/nginx/host.access.log;
 
  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;    # 这个非常重要,采用vue-router的时候,必须配置这个
  }
 
  #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;
  #}
}

同样,直接复制即可,(路径后面再配置)
本机连接上linux环境,进行文件传输,我本机用的是finalshell
将文件放入此目录下对照nginx的配置
在这里插入图片描述
在linux上安装docker(安装过的跳过此步)
命令行上直接敲
yum -y install docker
等待安装完成后,启动docker
systemctl start docker
在这里插入图片描述
切到这个目录
执行构建镜像的命令
docker build -t html .
注意最后有一个点
在这里插入图片描述
等待完成后,docker images,查看镜像
在这里插入图片描述
会发现多了一条镜像
在这里插入图片描述
然后登录阿里云服务器,跟着步骤走,就能把镜像推送上去

下面附上一些docker常用的操作命令

启动镜像的方式
docker run -p 8079:8079 re…

登录方式
docker login --username=

查看本地的docker镜像
docker images

查看本地的容器
docker ps -a

删除本地的镜像
docker rmi 镜像ID

删除本地的容器
docker rm 容器ID

批量删除本地的镜像
docker rmi $(docker image ls -a -q)

批量删除本地的容器
docker rm $(docker container ls -a -q)

批量停止本地的容器
docker container stop $(docker container ls -a -q)

拉取远端镜像
docker pull re…

重新连接启动的容器
docker exec -it 容器id /bin/bash

停止启动的容器
docker kill 容器ID

启动容器
docker start 容器ID

导出docker容器
docker save REPOSITORY :TAG > plm.tar

导入docker容器
docker load -i plm.tar

后台运行
-d

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值