Docker 制作定制asp.netcore 的容器
上文Windows docker k8s asp.net core的k8swebap镜像只是一个asp.net core程序,在实际生产中我们希望容器中还有一些其他程序,比如ssh 和telegraf。
利用Dockerfile文件
只是网上比较推荐的一种方式,Dockerfile包含创建镜像所需要的全部指令,基于在Dockerfile中的指令,我们可以使用Docker build
命令来创建镜像,通过减少镜像和容器的创建过程来简化部署。这里我们以 asp.net core 添加ssh服务为例:
1.编译并发布项目(这里用发布后的文件):
2.首先创建一个sshd_config 文件如下:
Port
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress :: #HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key # Ciphers and keying
#RekeyLimit default none # Logging
#SyslogFacility AUTH
#LogLevel INFO # Authentication: #LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries
#MaxSessions #PubkeyAuthentication yes # Expect .ssh/authorized_keys2 to be disregarded by default in future.
#AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 #AuthorizedPrincipalsFile none #AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes # To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no # Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no # Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no # GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes
#GSSAPIKeyExchange no UsePAM yes #AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
X11Forwarding yes
#X11DisplayOffset
#X11UseLocalhost yes
#PermitTTY yes
PrintMotd no
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval
#ClientAliveCountMax
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups ::
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none # no default banner path
#Banner none # Allow client to pass locale environment variables
AcceptEnv LANG LC_* # override default of no subsystems
Subsystem sftp /usr/lib/openssh/sftp-server # Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
3.创建Dockerfile文件如下:
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY . .
EXPOSE RUN apt-get update -y && apt-get upgrade -y && apt-get install -y \
openssh-server \
&& rm -rf /var/lib/apt/lists/* RUN echo 'root:Harbor12345' | chpasswd
RUN mkdir /var/run/sshd COPY sshd_config /etc/ssh/sshd_config ENTRYPOINT ["/bin/bash", "-c", "/usr/sbin/sshd && dotnet k8sWebApi.dll"]
4.制作镜像biang验证
docker build -t k8swebapi . #自作镜像
docker run --rm -p8081: -p2222: k8swebapi #启动docker 实例
docker exec 649c hostname -I #查看容器ip
ssh root@172.17.0.2 #在宿主计算机上进入容器
在宿主进入容器如下:
在普通的计算机上进入容器如:
手动修改容器镜像
这里 我们以asp.net core 添加 telegraf 为例。首先我们需要一个含有asp.net core的容器。这里我们修改 上面的Dockerfile文件 如下:
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
EXPOSE RUN apt-get update -y && apt-get upgrade -y && apt-get install -y \
openssh-server \
&& rm -rf /var/lib/apt/lists/* RUN echo 'root:Harbor12345' | chpasswd
RUN mkdir /var/run/sshd COPY sshd_config /etc/ssh/sshd_config CMD ["/usr/sbin/sshd", "-D"]
然后制作镜像 并启动实例
docker build -t aspnetcore2. . #制作镜像
docker run -d -p2222: --name aspcor2. aspnetcore2. #启动容器
进入容器后安装telagraf
apt-get update
apt-get install apt-transport-https
apt-get install curl
apt-get install sudo
apt-get install gnupg2 && apt-get install gnupg1
cat <<EOF | sudo tee /etc/apt/sources.list.d/influxdata.list
deb https://repos.influxdata.com/ubuntu bionic stable
EOF
sudo curl -sL https://repos.influxdata.com/influxdb.key | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install telegraf
修改配置如图:
然后启动服务 制作新的镜像
sudo service telegraf start
sudo systemctl enable --now telegraf
docker commit aspcor2. 192.168.100.3:/repo-test/aspcore2.
这里我们可以在influxdb里面验证telegraf的数据, 然后关闭relegraf 服务 ,安装service和lsof
再次 提交镜像 docker commit aspcor2.1 192.168.100.3:80/repo-test/aspcore2.1 (实际先前那一次不需要提交)
最后修改程序的Dockerfile如下:(备注一下 ,如果写成 ENTRYPOINT ["/bin/bash", "-c", "/usr/sbin/sshd && /usr/bin/telegraf && dotnet k8sWebApi.dll"] 或有问题的)
FROM 192.168.100.3:/repo-test/aspcore2.
WORKDIR /app
COPY . .
EXPOSE CMD ["/usr/bin/telegraf", "-D"]
ENTRYPOINT ["/bin/bash", "-c", "/usr/sbin/sshd && dotnet k8sWebApi.dll"]
docker build -t k8swebapi .
docker run --rm -p8081: -p2222: k8swebapi
简单总结一下, 其实网上大家肌肤都推荐用Dockerfile来制作镜像,但是我个人比较推荐手动自作镜像,先看2个图吧
Dockerfile制作镜像(比较耗时,需要联网下载相关的软件,并且要求相对较高,验证的方式只能启动容器来验证):
手动安装(在引入docker开发,我相信一定会有私有仓库,所以这里的镜像制作非常快,只需要从本地下载镜像就可以,不需要下载其他软件,制作初始镜像比较麻烦, 但是相对简单, 验证也很方便):
参考
ubuntu docker inflxudb(安装 使用 备份 还原 以及python编码) telegraf Grafana
Docker 制作定制asp.netcore 的容器的更多相关文章
- 壹佰文章最全总结| 《关于ASP.NETCore的分享之路》
学习路线图 (关于学习ASP.NET Core需要了解和掌握的知识点图) 一言不合就来图,各位博客园小伙伴大家好,感觉好久没有写文章了,自从春节开始,中间经历种种,慢慢的就开始微信公众号发文了,原因有 ...
- Ubuntu18.04安装Docker并部署(编译、发布、构建镜像)Asp.NetCore项目全过程笔记
环境准备:阿里云Ubuntu18.04 全新安装 一.安装Docker 1.删除旧版本并更新包索引: sudo apt-get remove docker docker-engine dock ...
- Docker镜像管理基础与基于容器的镜像制作示例
一.Docker镜像 Docker镜像是启动Docker容器的一个非常重要的组件.Docker各组件之间的关系如图: Docker镜像含有启动容器所需要的文件系统及其内容,因此Docker镜像用于创建 ...
- .NETCore 实现容器化Docker与私有镜像仓库管理
原文:.NETCore 实现容器化Docker与私有镜像仓库管理 一.Docker介绍 Docker是用Go语言编写基于Linux操作系统的一些特性开发的,其提供了操作系统级别的抽象,是一种容器管理技 ...
- 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像
Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...
- ASP.NETCore 3.0 Autofac替换及控制器属性注入及全局容器使用
1.Autofac基础使用 参考: https://www.cnblogs.com/li150dan/p/10071079.html 2.ASP.NETCore 3.0 Autofac 容器替换 需要 ...
- asp.net core容器&mysql容器network互联 & docker compose方式编排启动多个容器
文章简介 asp.net core webapi容器与Mysql容器互联(network方式) docker compose方式编排启动多个容器 asp.net core webapi容器与Mysql ...
- 一套标准的ASP.NET Core容器化应用日志收集分析方案
讲故事 关注我公众号的朋友,应该知道我写了一些云原生应用收集和分析相关的文章,其中内容大多聚焦某个具体的组件: 超级有用的TraceId,快点用起来吧! 如何利用NLog输出结构化日志,并在Kiban ...
- Ubuntu & Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践
相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...
随机推荐
- win10自带截屏操作
1.win+shift+S,自由截屏 2.win+W,截屏后编辑 3.alt+PrtSc,截取当前活动界面,鼠标在微信就是微信,在浏览器就是浏览器.在桌面就是所有界面. 4.PrtScn,截取所有屏幕 ...
- linux函数深入探索——open函数打开文件是否将文件内容加载到内存空间
转自:https://blog.csdn.net/qq_17019203/article/details/85051627 问题:open(2)函数打开文件是否将文件内容加载到内存空间 首先,文件打开 ...
- 使用docker搭建redis-cluster环境
目录 基础环境信息 搭建步骤 搭建中遇到的问题 其他参考 临时接到一个测试任务,而测试需要用到redis-cluster环境,却没有现成的环境可用,于是只能自力更生搭建测试环境.一开始想采用在 ...
- ansible自动化运维02
ansible清单管理 inventory文件通常用于定义要管理主机的认证信息,例如:ssh登录用户名,密码,以及key相关信息. 举个例子:定义清单组 注意:组名为pro,关键字段children表 ...
- HTTP协议那些事儿
HTTP协议简介 超文本传输协议(英文:HyperText Transfer Protocol,缩写:HTTP)是一种用于分布式.协作式和超媒体信息系统的应用层协议.HTTP是万维网的数据通信的基础. ...
- 查看 Python 对象的属性
1 .dir函数可以返回一个对象的所有属性和方法. 示例:查看 int 对象的属性和方法 示例: 查看 dict 对象的属性和方法 标红的这些是不是遇到过? 2.help()调用内置帮助系统 示例 3 ...
- 注意力机制---Attention、local Attention、self Attention、Hierarchical attention
一.编码-解码架构 目的:解决语音识别.机器翻译.知识问答等输出输入序列长度不相等的任务. C是输入的一个表达(representation),包含了输入序列的有效信息. 它可能是一个向量,也可能是一 ...
- V4L2视频采集原理
一.简介 Video for Linuxtwo(Video4Linux2)简称V4L2,是V4L的改进版.V4L2是linux操作系统下用于采集图片.视频和音频数据的API接口,配合适当的视频采集设备 ...
- 201871020225-牟星源 《面向对象程序设计(java)》课程学习进度条
<2019面向对象程序设计(java)课程学习进度条> 周次 (阅读/编写)代码行数 发布博客量/评论他人博客数量 课余学习时间(小时) 学习收获最大的程序 阅读或编译让我 第一周 25/ ...
- turtle模块
turtle(海龟)绘图用法 import turtle -->调出turtle库 setup()-->设置窗体大小和位置 turtle.setup(width,height,startx ...