ASP.NET Core开发环境搭建利用Docker Compose结合Nginx和Redis

AspNetCore 的本地开发环境,直接用 DockerCompose 搭出来,省事还不怕环境污染。Nginx 搞个统一入口,多个服务容器按模块划分,结构也清晰,扩展性也强。你要是经常要在不同项目之间切换环境,这套方案就挺合适的。

DockerCompose 的组合式环境,管理项目像搭积木,Web 程序、RedisSQL Server on LinuxNginx都能一起撸进去。整体思路就是用nginx-host做代理,把容器跑起来后直接用宿主机一个端口搞定访问,干净利落。

目录结构也简单,新建一个sites文件夹,把相关配置和容器构建文件都放进去,比如docker-compose.ymlDockerfileredis.conf,还有nginx/sites-enabled那些。结构一目了然,部署起来也快。

看看docker-compose.yml配置就知道套路不复杂:

version: "2"
services:
  redis:
    build: ./redis/
    restart: always
    container_name: mac-redis

identity: build: ~/identity/src/Web/ restart: always volumes: - ~/identity/src/Web/bin/Debug/netcoreapp1.1/publish:/app links: - 'ucenter:api.ucenter.com'

ucenter: build: ~/UCenter/src/WebAPI restart: always volumes: - ~/UCenter/src/WebAPI/bin/Debug/netcoreapp1.1/publish:/app links: - redis

nginx-host: build: ./nginx restart: always ports: - "80:80" links: - identity - ucenter volumes: - ./nginx/sites-enabled:/etc/nginx/sites-enabled - /WebCommon:/www/data logging: driver: "json-file" options: max-size: "50k" max-file: "10"

你可以根据项目结构换成你自己的服务,比如前端 React 项目也可以打包进一个容器,丢在 Nginx 里一起搞定。日志那块也帮你想好了,自动轮转,省心。

如果你想要一个灵活好用、可快速切换的本地环境,或者团队协作需要统一开发配置,这套方式真的蛮推荐。哦对了,相关技术比如Nginx + Docker 搭建方式你也可以参考下。

docx 文件大小:51.69KB