1. 程式人生 > 其它 >docker 你會了嗎

docker 你會了嗎

技術標籤:前端知識經驗總結

Docker的思想來自於集裝箱
映象就是封裝好的環境,保留一份可以到處執行,當需要在一個新伺服器上部署環境的時候就可以直接執行映象,不需要再裝一次環境

映象

docker 內可建立容器即 image 映象(映象可以理解為配置好環境的壓縮包,啟動以後可以建立容器)
docker run nginx:alpine 建立簡潔版的nginx映象
docker images 可檢視映象
在這裡插入圖片描述
REPOSITORY: 來自於哪個倉庫;
TAG: 映象的標籤資訊,比如 5.7、latest 表示不同的版本資訊;
IMAGE ID: 映象的 ID, 如果您看到兩個 ID 完全相同,那麼實際上,它們指向的是同一個映象,只是標籤名稱不同罷了;

CREATED: 映象最後的更新時間;
SIZE: 映象的大小,優秀的映象一般體積都比較小,這也是我更傾向於使用輕量級的 alpine 版本的原因;
docker inspect REPOSITORY 可檢視映象的詳細資訊例如 docker inspect nginx
docker rmi REPOSITORY 刪除映象

容器

docker 可啟動映象建立容器,一個映象可建立多個容器(每個容器就是一個獨立的小型Linux系統)

docker run --name=smartPacking -dit -p 3002:80 -v D:\test-peizhi\www\:/usr/share/nginx/html/ -v D:\ test-peizhi \nginx.conf:/etc/nginx/nginx.conf -v D:\test-peizhi\logs\:/var/log/nginx nginx:apline

smartPacking 就是容器的名稱,-p 3002:80即埠對映,本地訪問localhost:3002,就會訪問容器內部的80埠

docker ps 可檢視正在執行的容器
container ID 就是容器的id,Image 就是啟動的容器名稱 狀態 up表示正在執行中,ports表示埠對映
在這裡插入圖片描述
docker ps -a 可檢視所有的容器
docker inspect containerID 可檢視容器的詳細資訊
docker stop containerID 表示停止某個容器

部署vue 專案

  1. 編譯vue 專案
  2. 將vue 專案放dist資料夾中的檔案放到 一個根目錄下 我放的是www資料夾下
  3. 在www資料夾的同級目錄下 放 nginx.conf 檔案
user  nginx;
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;

    server_tokens  off;

    server {
        listen 80;
        listen [::]:80;
        charset utf-8;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
}
  1. 在www資料夾的同級目錄下新建一個log資料夾
  2. 放置一個default.conf檔案
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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;
    #}
}
  1. 執行兩個命令 第一個 docker run nginx:alpine
    第二個
docker run --name=smartPacking -dit -p 3002:80 -v D:\test-peizhi\www\:/usr/share/nginx/html/ -v D:\ test-peizhi \nginx.conf:/etc/nginx/nginx.conf -v D:\test-peizhi\logs\:/var/log/nginx nginx:apline

注意對應的資料夾變一下就可以了

完成