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 ...
随机推荐
- 使用curl出现,curl: /usr/local/lib/libssl.so.1.1: version `OPENSSL_1_1_1' not found (required by /usr/lib/x86_64-linux-gnu/libcurl.so.4)
主要原因是curl找不到openssl的路径,所以只要将openssl的路径添加到相应的变量中就可以了. 参考连接https://blog.csdn.net/RookieWutongshu/artic ...
- 5天noip训练心得
day1 100+95+0=195 T1 二分答案,并查集,很像noip2017 day2 T1 T2 缩环,然后数据结构维护求答案,貌似也是原题 T3 树形dp,比赛上没有做出来, day2 90+ ...
- Sublime Text 3 全程详细图文使用教程
一. 前言 使用Sublime Text 也有几个年头了,版本也从2升级到3了,但犹如寒天饮冰水,冷暖尽自知.最初也是不知道从何下手,满世界地查找资料,但能查阅到的资料,苦于它们的零碎.片面,不够系统 ...
- mysql清表时有外键关联处理办法
可以忽视关联的情况下: 先取消主外键关系验证:SET FOREIGN_KEY_CHECKS = 0; 然后删除需要删除的数据:truncate table table_name; 最后恢复:SET F ...
- MapBox
MapBox的地图API大家用过吗 用作网站或者APP的底图,就不用自己架设地图服务器了 发布自己的地图了 这跟Google Map是一样的道理,类似的还有天地图,高德,百度地图API等等. 属于前端 ...
- fanout(Publish/Subscribe)发布/订阅
引言 它是一种通过广播方式发送消息的路由器,所有和exchange建立的绑定关系的队列都会接收到消息 不处理路由键,只需要简单的将队列绑定到交换机上 fanout交换机转发消息是最快的,它不需要做路由 ...
- SpringMVC工作原理的介绍
1.用户向服务器发送请求,请求被Spring前端控制Servlet DispatcherServlet捕获 2.DispatcherServlet对请求URL进行解析,得到请求资源标识符(URL).然 ...
- laravel 中first和find区别(总结一)
检索单个模型/集合 除了从指定的数据表检索所有记录外,你也可以通过 find 或 first 方法来检索单条记录.这些方法不是返回一组模型,而是返回一个模型实例: // 通过主键取回一个模型... $ ...
- 云服务器 ECS 是什么?
云服务器 Elastic Compute Service(ECS)是阿里云提供的一种基础云计算服务.使用云服务器 ECS 就像使用水.电.煤气等资源一样便捷.高效.您无需提前采购硬件设备,而是根据业务 ...
- Runtime 源码阅读
Runtime 属性说明 /** * 每一个 Java 应用程序都有一个关联的运行时对象 * * @author unascribed * @see java.lang.Runtime#getRunt ...