Docker-Compose运行Nginx+Redis+NetCoreAPI
Docker-Compose运行Nginx+Redis+NetCoreAPI
一、准备Docker-Compose
Docker
安装Docker-compose
- 参考官网:https://docs.docker.com/compose/install/
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
docker-compose version 1.24., build 0aa59064
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
二、准备NetCoreAPI项目
dotnet new webpai -n composeAPI --no-https
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddStackExchangeRedisCache(options => {
options.Configuration = Configuration.GetConnectionString("Redis");
}); services.AddHttpContextAccessor();
}
[HttpGet("{name}")]
public ActionResult<string> Get(string name)
{
var connection = _httpContextAccessor.HttpContext.Connection;
var ipv4 = connection.LocalIpAddress.MapToIPv4().ToString();
var cacheKey = $"test:{name}";
_distributedCache.SetString(cacheKey, $"{ipv4} {name}");
var message = _distributedCache.GetString(cacheKey);
return message;
}
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY . ./
RUN dotnet restore
RUN dotnet publish -c Release -o out FROM microsoft/dotnet:2.2-aspnetcore-runtime
WORKDIR /app
COPY --from=build /src/out .
ENTRYPOINT [ "dotnet", "composeAPI.dll" ]
- 查看`version`地址:https://docs.docker.com/compose/compose-file/compose-versioning/ 里面有docker与compose对应关系
- `docker-compose`配置可以参考:https://docs.docker.com/compose/compose-file/
version: "3.7"
services:
web-service:
container_name: composeapi
build:
context: .
dockerfile: Dockerfile
restart: always
ports:
- "10001:80"
volumes:
- ./appsettings.json:/app/appsettings.json
web-service-2:
container_name: composeapi-2
depends_on:
- web-service
image: composeapi_web-service
restart: always
ports:
- "10002:80"
volumes:
- ./appsettings.json:/app/appsettings.json
web-service-3:
container_name: composeapi-3
depends_on:
- web-service
image: composeapi_web-service
restart: always
ports:
- "10003:80"
volumes:
- ./appsettings.json:/app/appsettings.json
nginx-service:
container_name: composeapi-nginx
image: nginx
restart: always
ports:
- "80:80"
volumes:
- ./conf/nginx.conf:/etc/nginx/conf.d/default.conf
redis-service:
container_name: composeapi-redis
image: redis
restart: always
ports:
- "6379:80"
volumes:
- ./conf/redis.conf:/etc/redis/redis.conf
- 3个webapi服务(由于webapi服务所依赖的镜像都是一样的,所以后面2个我直接使用第一个服务创建出来的镜像,`docker-compose`创建出来的镜像名称格式`project_service`)
- 1个redis服务,直接使用redis镜像
- 1个nginx服务,直接使用nginx镜像(反向代理3个webapi服务,实现负载均衡)
- 在上一步`docker-compose.yml`文件中我们看到,需要将`./conf/nginx.conf`文件映射到容器的nginx默认配置文件,方便后续维护nginx配置,不然每次修改配置文件都需要进入docker容器再修改比较麻烦。
mkdir conf && cd conf
touch nginx.conf
upstream composeapi {
# 这里需要注意,server的名称需要与docker-compose.yml文件中的services名称一致
server web-service:;
server web-service-:;
server web-service-:;
}
server {
listen ;
location / {
proxy_pass http://composeapi;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
wget http://download.redis.io/redis-stable/redis.conf
docker-compose up -d
# 如果提示没有权限,加上sudo
sudo docker-compose up -d
# 运行结果
Creating composeapi-nginx ... done
Creating composeapi-redis ... done
Creating composeapi ... done
Creating composeapi- ... done
Creating composeapi- ... done
三、查看运行结果
sudo docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------
composeapi dotnet composeAPI.dll Up 0.0.0.0:->/tcp
composeapi- dotnet composeAPI.dll Up 0.0.0.0:->/tcp
composeapi- dotnet composeAPI.dll Up 0.0.0.0:->/tcp
composeapi-nginx nginx -g daemon off; Up 0.0.0.0:->/tcp
composeapi-redis docker-entrypoint.sh redis ... Up /tcp, 0.0.0.0:->/tcp
172.18.0.3 hello
172.18.0.5 hello
172.18.0.6 hello
- 上面的结果每次刷新都会循环显示
总结
Docker-Compose运行Nginx+Redis+NetCoreAPI的更多相关文章
- Docker中运行nginx
Docker中运行nginx 1.Docker中运行nginx 2.配置文件 2.1 nginx.conf 2.2 default.conf 3.docker的镜像可以挂什么卷 部分内容原文地址: C ...
- dotnet跨平台 - 使用Nginx+Docker Compose运行.NETCore项目
参考文档: https://docs.docker.com/install/linux/docker-ce/centos/ http://www.dockerinfo.net/document htt ...
- Docker Compose 部署Nginx服务实现负载均衡
Compose简介: Compose是Docker容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器,使用Docker Compose,不再需要使用shell脚本来启动容器.Comp ...
- 容器(docker)内运行Nginx
容器内运行nginx其实很简单,但是一开始还是浪费了我很多时间.这里写下来给大家省点时间. 1.创建nginx文件夹,放置各种配置及日志等. mkdir /docker/nginx docker 文件 ...
- Docker 自动运行Nginx容器
Dockerfile文件如下: FROM ubuntu #基础镜像 RUN apt-get update #更新apt RUN apt-get -y install nginx #安装nginx VO ...
- 阿里云服务器用Docker配置运行nginx并访问
一.Docker拉取nginx镜像 docker pull nginx:1.12.2 这里是下载的是nginx的1.12.2版本,其他版本的镜像请访问https://hub.docker.com/r/ ...
- [Docker] Win10中安装Docker并运行Nginx镜像
一.安装Docker 进入官网:https://www.docker.com/products/docker-desktop 可能需要先注册登录,很简单的. 点击 Download Desktop f ...
- Docker教程:使用Docker容器运行Nginx并实现反向代理
一.前言 我们知道,为了安全考虑,我们一般会设置反向代理,用来屏蔽应用程序真实的IP和端口号.在Linux系统上最常用的反向代理就是Nginx.本篇文章中,我们会通过Docker容器分别运行一个Ngi ...
- Docker Compose部署 nginx代理Tomcat集群
一.简介 使用Docker镜像部署Nginx代理的多个Tomcat集群: 使用Dockerfile构建nginx镜像 使用Dockerfile构建tomcat镜像 mysql镜像使用docker hu ...
随机推荐
- commons-codec-1.9.jar 是做什么用的?
commons-codec用来处理常用的编码方法的工具类包,例如DES.SHA1.MD5.Base64,URL,Soundx等等. 示例: 不可逆算法 1.MD5 String str = " ...
- [人物存档]【AI少女】【捏脸数据】1222今日份的推荐
AISChaF_20191030183624290.png
- CF990G GCD Counting 点分治+容斥+暴力
只想出来 $O(nlogn\times 160)$ 的复杂度,没想到还能过~ Code: #include <cstdio> #include <vector> #includ ...
- NOI2019 游记
day-1 广二真好看QAQ (要是我也能在这里读书就好了) 提供的餐饮好评QAQ 发现室友是雅礼集训时候的室友,衡水小姐姐zyn. 但是寝室没有网没有信号没有桌子真的不良心啊...... 发现小卖部 ...
- 2019南昌网络赛 J Distance on the tree 主席树+lca
题意 给一颗树,每条边有边权,每次询问\(u\)到\(v\)的路径中有多少边的边权小于等于\(k\) 分析 在树的每个点上建\(1\)到\(i\)的权值线段树,查询的时候同时跑\(u,v,lca ...
- CF1228F One Node is Gone
题目链接 问题分析 这题感觉就是有很多种方法,然后一种都写不明白-- 首先分为3种情况: 删了根节点下的一个节点,对应两个答案: 删了一个叶节点,对应一个答案: 删了一个其他节点,对应一个答案. 可以 ...
- 【面试】Redis
1.如果在setnx之后执行expire之前进程意外crash或者要重启维护了,那会怎么样? set指令有非常复杂的参数,这个应该是可以同时setnx和expire合成一条指令来用的! 2.使用过Re ...
- 域名与服务器 ip地址的理解
域名 服务器 ip地址具有怎样的关系呢 通俗的讲,我们访问一个网站就相当于访问一个服务器的文件,如果想要通过自己的域名来访问一个网站,首先得将域名部署到你的服务器上,然后就可以通过域名访问到你服务器上 ...
- Fastadmin 写关联命名时,最好前后台用同一个model,方便管理(会出现命名空间问题)
1.php think crud -t test --relation=category(外键表1) --relation=admin(外键表2) --relationforeignkey=categ ...
- Oracle、SQLServer 删除表中的重复数据,只保留一条记录
原文地址: https://blog.csdn.net/yangwenxue_admin/article/details/51742426 https://www.cnblogs.com/spring ...