文章目录
1.安装 Docker
机票: Docker安装.
2.项目根目录创建 Dockerfile 文件
# image 文件继承官方的 node image,冒号表示标签,这里标签是10,即10版本的 node
FROM node:10 as firstImage
# 将当前目录下的所有文件(除了.dockerignore排除的路径),都拷贝进入
#image 文件的/app目录
COPY ./ /app
# 指定接下来的工作路径为/app
WORKDIR /app
# Dockerfile 的指令每执行一次都会在 docker 上新建一层。所以过多
# 无意义的层,会造成镜像膨胀过大;
# && 符号连接命令,这样执行后,只会创建 1 层镜像
RUN npm install && npm run build
#定制的镜像都是基于 FROM 的镜像,这里的 nginx 就是定制需要的基础镜像。
#后续的操作都是基于 nginx ;
FROM nginx
RUN mkdir /app
# --from=firstImage 从前边的阶段中拷贝文件到当前阶段中,多个FROM语句时,
# 0代表第一个阶段。除了使用数字,我们还可以使用 as 给阶段命名;
COPY --from=firstImage /app/dist /app
# 复制当前目录下的nginx.conf文件到image镜像生成/etc/nginx/nginx.conf
#文件
COPY nginx.conf /etc/nginx/nginx.conf
3.项目根目录创建 .dockerignore 文件
# 防止 node_modules 和其他中间构建产物被复制到镜像中导致构建问题
**/node_modules
**/dist
4.项目根目录创建 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 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /app;
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 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;
# }
#}
# 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.构建你的 Docker 镜像
docker build -t my-app .
# 末尾点-Dockerfile 文件路径
6.运行你的 Docker 镜像
【 8088容器端口对应nginx中service监听的8088端口】
# 8088容器端口对应nginx中service监听的8088端口
docker run -d -p 90:8088 my-app
# -d 后台运行容器