From 987345f7240217b7b6d0c8ee746cf44745e7418e Mon Sep 17 00:00:00 2001 From: zy7y <13271962515@163.com> Date: Sat, 8 Oct 2022 12:09:20 +0800 Subject: [PATCH] feat: Deploy the project using Docker --- backend/.dockerignore | 140 +++++++++++++++++++++++++++++++++++++++ backend/Dockerfile | 5 ++ docker-compose.yml | 22 ++++++ frontend/.dockerignore | 129 ++++++++++++++++++++++++++++++++++++ frontend/.env.production | 4 +- frontend/Dockerfile | 11 +++ frontend/nginx.conf | 30 +++++++++ reset.sh | 3 + 8 files changed, 342 insertions(+), 2 deletions(-) create mode 100644 backend/.dockerignore create mode 100644 backend/Dockerfile create mode 100644 docker-compose.yml create mode 100644 frontend/.dockerignore create mode 100644 frontend/Dockerfile create mode 100644 frontend/nginx.conf create mode 100644 reset.sh diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..1c3b1bc --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,140 @@ +# Created by .ignore support plugin (hsz.mobi) +### Python template +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..63aeb9b --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,5 @@ +FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9 +COPY . /app +RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo 'Asia/Shanghai' >/etc/timezone && \ + pip install --no-cache-dir --upgrade -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ca43619 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +version: "3" +services: + backend: + build: + context: ./backend + dockerfile: Dockerfile + container_name: mini-rbac + ports: + - "1855:80" + + frontend: + build: + # 上下文管理, 执行frontend 下的 Dockerfile + context: ./frontend + dockerfile: Dockerfile + # 容器名称 + container_name: mini-web + # 宿主机2152 端口 绑定 容器中的80 端口 + ports: + - "1856:80" + depends_on: # 依赖于backend容器,被依赖容器启动后此web容器才可启动 + - backend \ No newline at end of file diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 0000000..691baa7 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,129 @@ +# Created by .ignore support plugin (hsz.mobi) +### Vue template +# gitignore template for Vue.js projects +# +# Recommended template: Node.gitignore + +# TODO: where does this rule come from? +docs/_book + +# TODO: where does this rule come from? +test/ + +### Node template +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverag +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* diff --git a/frontend/.env.production b/frontend/.env.production index 0b0b74d..c701187 100644 --- a/frontend/.env.production +++ b/frontend/.env.production @@ -1,2 +1,2 @@ -VITE_BASE_URL = http://127.0.0.1:8000/api -VITE_WEBSOCKET = ws://localhost:8000/ws +VITE_BASE_URL = http://localhost:1855 +VITE_WS = ws://localhost:1855/ws diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..72e06a6 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,11 @@ +FROM node:16.15-alpine +COPY ./ /app +WORKDIR /app +RUN npm config set registry https://registry.npmjs.org +RUN npm install +RUN npm run build + +FROM nginx:1.15.2-alpine +RUN mkdir /app +COPY --from=0 /app/dist /app +COPY nginx.conf /etc/nginx/nginx.conf diff --git a/frontend/nginx.conf b/frontend/nginx.conf new file mode 100644 index 0000000..6f61d6c --- /dev/null +++ b/frontend/nginx.conf @@ -0,0 +1,30 @@ +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; + keepalive_timeout 65; + server { + listen 80; + server_name localhost; + location / { + root /app; + index index.html; + try_files $uri $uri/ /index.html; + } + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + } +} diff --git a/reset.sh b/reset.sh new file mode 100644 index 0000000..c4fefd2 --- /dev/null +++ b/reset.sh @@ -0,0 +1,3 @@ +docker rm -f mini-web mini-rbac +docker rmi -f mini-rbac_backend mini-rbac_frontend mini-rbac-backend mini-rbac-frontend +docker-compose up -d