Docker 基础

Docker+nginx部署Vue项目

2021-07-14  本文已影响0人  yjtuuige

准备前端文件目录

[root@VM-0-6-centos home]# mkdir vue-test
[root@VM-0-6-centos home]# cd vue-test
[root@VM-0-6-centos vue-test]# ll
total 780
drwxrwxrwx 5 root root   4096 Jul 14 13:09 dist

编写构建脚本

# 指定基础镜像,必须为第一个命令
# 可以使用镜像ID或者镜像名称 例如 nginx:1.14
FROM nginx
# 维护者信息
MAINTAINER yj
# 将本地文件添加到容器中
# 将dist文件中的内容复制到 /usr/local/nginx/html/ 这个目录下面,该路径是nginx容器生成的一个虚拟路径,你的项目会存在这里。
COPY dist/  /usr/local/nginx/html/
COPY nginx.conf /etc/nginx/nginx.conf
# 构建镜像时执行的命令
RUN echo 'echo init ok!!'

注:如果指定的基础镜像没有找到的话,会自动从仓库中pull对应的镜像。这里还有一种情况,如果是内网情况并且镜像仓库中没有所需要的镜像。可以在有这个镜像的服务器上先导出镜像,然后再复制到当前服务器进行导入即可。

# nginx.tar是导出tar包的文件名,
# nginx:1.14是仓库中的镜像名称,这里也可以直接使用镜像ID
# 导出镜像
docker save -o nginx.tar nginx:1.14
 
# 导入镜像,nginx.tar就是刚才导出的包
docker load -i nginx.tar
worker_processes auto;
#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;
  
    client_max_body_size   20m;
    server {
        listen       80;
        server_name  localhost;
  
        #charset koi8-r;
  
        #access_log  logs/host.access.log  main;
     location / {
        root   /usr/local/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
        }
        #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   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;
        #}
    }
  
    # 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;
    #    }
    #}
  
}
[root@VM-0-6-centos vue-test]# ll
total 780
drwxrwxrwx 5 root root   4096 Jul 14 13:09 dist
-rw-r--r-- 1 root root    495 Jul 14 13:53 dockerfile
-rw-r--r-- 1 root root   2349 Jul 14 13:53 nginx.conf

构建镜像

[root@VM-0-6-centos vue-test]# docker build -t vue-test .
Sending build context to Docker daemon  1.577MB
Step 1/5 : FROM nginx
latest: Pulling from library/nginx
b4d181a07f80: Pull complete 
66b1c490df3f: Pull complete 
d0f91ae9b44c: Pull complete 
baf987068537: Pull complete 
6bbc76cbebeb: Pull complete 
32b766478bc2: Pull complete 
Digest: sha256:353c20f74d9b6aee359f30e8e4f69c3d7eaea2f610681c4a95849a2fd7c497f9
Status: Downloaded newer image for nginx:latest
 ---> 4cdc5dd7eaad
Step 2/5 : MAINTAINER liu
 ---> Running in 14d475ee1332
Removing intermediate container 14d475ee1332
 ---> fd560379db6d
Step 3/5 : COPY dist/  /usr/local/nginx/html/
 ---> 4bef97e68f76
Step 4/5 : COPY nginx.conf /etc/nginx/nginx.conf
 ---> 182782b8742d
Step 5/5 : RUN echo 'echo init ok!!'
 ---> Running in 222ef2f75359
echo init ok!!
Removing intermediate container 222ef2f75359
 ---> b5e318b51875
Successfully built b5e318b51875
Successfully tagged vue-test:latest        // 创建成功

启动镜像 docker run -p 8181:80 -d --name vue vue-test

# run: 创建一个新的容器并运行一个命令
# -d: 后台运行容器,并返回容器ID
# -p: 端口映射,格式为:主机(宿主)端口:容器端口
# --name="vue": 为容器指定一个名称
[root@VM-0-6-centos vue-test]# docker run -p 8181:80 -d --name vue vue-test
00feb52b68eda32e2f50bc798d9d45da0a274dd8e5509dfa9dc60c881fff9291
[root@VM-0-6-centos vue-test]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                   NAMES
00feb52b68ed   vue-test   "/docker-entrypoint.…"   52 seconds ago   Up 51 seconds   0.0.0.0:8181->80/tcp, :::8181->80/tcp   vue

测试

[root@VM-0-6-centos vue-test]# curl localhost:8181
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>vue-cli-test</title><link href="/css/app.064b75fd.css" rel="preload" as="style"><link href="/js/app.43eea191.js" rel="preload" as="script"><link href="/js/chunk-vendors.d300f0fc.js" rel="preload" as="script"><link href="/css/app.064b75fd.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but vue-cli-test doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/js/chunk-vendors.d300f0fc.js"></script><script src="/js/app.43eea191.js"></script></body></html>[root@VM-0-6-centos vue-test]# 

部署完毕后可以删除,创建的 /home/vue-test/dist 目录,不会影响镜像的运行。

上一篇 下一篇

猜你喜欢

热点阅读