- 编写 Dockerfile 文件:
FROM nginx:latest
ADD dist /etc/nginx/html/dist
COPY nginx.conf /etc/nginx/nginx.conf
ENV PATH /usr/sbin:$PATH
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]
- 编写 nginx.conf 文件:
user root;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile off;
tcp_nopush on;
server_tokens off;
keepalive_timeout 65;
client_max_body_size 1000m;
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
server {
listen 80;
server_name localhost;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 6;
gzip_vary on;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
add_header X-Frame-Options "SAMEORIGIN";
add_header Cache-Control "no-cache";
root html/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
location /new/oliver/ {
proxy_pass http://127.0.0.1:9090;
rewrite "^/new/(.*)$" /$1 break;
}
location /oliver/ {
proxy_http_version 1.1;
proxy_pass http://172.30.10.141:11213;
#proxy_pass_request_headers on;
#proxy_set_header Host $http_host;
#proxy_set_header Connection "";
client_max_body_size 1024m;
client_body_buffer_size 128k;
proxy_redirect off;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream http_502 http_504 error invalid_header;
}
location /zhong/ {
proxy_pass http://api.oliver.com;
proxy_http_version 1.1;
proxy_set_header Connection "";
rewrite "^/zhong/(.*)$" /$1 break;
}
location /tyler/ {
proxy_pass http://192.168.56.200:8999;
rewrite "^/tyler/(.*)$" /$1 break;
}
location /ai {
proxy_pass https://tylerzhong.ai.com;
}
}
}
- 将 Dockerfile 打成镜像
docker build -t tylerApp:latest .
- 运行 tylerApp 镜像
docker run -id --name tylerApp -p 80:80 tylerApp
- 浏览器访问项目
http://ip:port/
扩展:
如果要将打包好的镜像发送给别人,让别人部署,则可以将镜像打成 tar 包,然后将 tar 包发送给别人。
docker save -o tylerApp.tar tylerApp
由于 Docker 镜像文件可能比较大,可以将 .tar 文件进行压缩以减少文件大小,便于发送。可以使用 tar 或 zip 等工具压缩:
tar -czvf tylerApp.tar.gz tylerApp.tar
或者使用 zip:
zip tylerApp.tar.gz tylerApp.tar
对方收到镜像文件后,需执行以下命令来加载 Docker 镜像:
docker load -i tylerApp.tar.gz
如果是压缩文件,需要先解压:
tar -xzvf tylerApp.tar.gz
docker load -i tylerApp.tar