上文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开发,我相信一定会有私有仓库,所以这里的镜像制作非常快,只需要从本地下载镜像就可以,不需要下载其他软件,制作初始镜像比较麻烦, 但是相对简单, 验证也很方便):

参考

Installing Telegraf

ubuntu docker inflxudb(安装 使用 备份 还原 以及python编码) telegraf Grafana

hklcf/debian-ssh-docker

Docker 制作定制asp.netcore 的容器的更多相关文章

  1. 壹佰文章最全总结| 《关于ASP.NETCore的分享之路》

    学习路线图 (关于学习ASP.NET Core需要了解和掌握的知识点图) 一言不合就来图,各位博客园小伙伴大家好,感觉好久没有写文章了,自从春节开始,中间经历种种,慢慢的就开始微信公众号发文了,原因有 ...

  2. Ubuntu18.04安装Docker并部署(编译、发布、构建镜像)Asp.NetCore项目全过程笔记

      环境准备:阿里云Ubuntu18.04 全新安装   一.安装Docker 1.删除旧版本并更新包索引: sudo apt-get remove docker docker-engine dock ...

  3. Docker镜像管理基础与基于容器的镜像制作示例

    一.Docker镜像 Docker镜像是启动Docker容器的一个非常重要的组件.Docker各组件之间的关系如图: Docker镜像含有启动容器所需要的文件系统及其内容,因此Docker镜像用于创建 ...

  4. .NETCore 实现容器化Docker与私有镜像仓库管理

    原文:.NETCore 实现容器化Docker与私有镜像仓库管理 一.Docker介绍 Docker是用Go语言编写基于Linux操作系统的一些特性开发的,其提供了操作系统级别的抽象,是一种容器管理技 ...

  5. 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像

    Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...

  6. ASP.NETCore 3.0 Autofac替换及控制器属性注入及全局容器使用

    1.Autofac基础使用 参考: https://www.cnblogs.com/li150dan/p/10071079.html 2.ASP.NETCore 3.0 Autofac 容器替换 需要 ...

  7. asp.net core容器&mysql容器network互联 & docker compose方式编排启动多个容器

    文章简介 asp.net core webapi容器与Mysql容器互联(network方式) docker compose方式编排启动多个容器 asp.net core webapi容器与Mysql ...

  8. 一套标准的ASP.NET Core容器化应用日志收集分析方案

    讲故事 关注我公众号的朋友,应该知道我写了一些云原生应用收集和分析相关的文章,其中内容大多聚焦某个具体的组件: 超级有用的TraceId,快点用起来吧! 如何利用NLog输出结构化日志,并在Kiban ...

  9. Ubuntu & Docker & Consul & Fabio & ASP.NET Core 2.0 微服务跨平台实践

    相关博文: Ubuntu 简单安装 Docker Mac OS.Ubuntu 安装及使用 Consul Consul 服务注册与服务发现 Fabio 安装和简单使用 阅读目录: Docker 运行 C ...

随机推荐

  1. 基于 ECharts 封装甘特图并实现自动滚屏

    项目中需要用到甘特图组件,之前的图表一直基于 EChart 开发,但 EChart 本身没有甘特图组件,需要自行封装 经过一番鏖战,终于完成了... 我在工程中参考 v-chart 封装了一套图表组件 ...

  2. 关于C++中使用++it还是it++的问题

    我们经常使用for循环来遍历东西,循环变量可以前自增也可以后自增,发现对遍历结果没啥影响,但是该如何选择呢? 我们应该尽量使用前自增运算符而不是后自增运算符,即用 ++ Iter 代替 Iter++ ...

  3. VBA笔记

    1.VBA数据类型 数据类型 存储空间大小 范围 Byte 1个字节 0-255 Boolean 2个字节 true或false Integer 2个字节 -32768-32767 Long 4个字节 ...

  4. java 使用网建SMS发送短信验证码

    首先, 注册并登录网建用户, 新注册用户将获得5条的测试短信 网建短信通地址: http://sms.webchinese.cn/default.shtml 注册账号在此就不多做赘述了, 直接上代码 ...

  5. Java面试题:HashMap和HashTable的区别

    HashMap和HashTable的区别是面试时面试官经常问的问题,在回答的时候可以选择重点做回答,区别主要有下面几点:key和value的取值范围不同HashMap和HashTable都是基于哈希表 ...

  6. arXiv上传文章latex源码技巧

    <<2019.09.27>>更新 上传PS文件看来也是不行了,一大早收到邮件被arXiv标记为incomplete了.哎,还是老老实实提交Latex source files吧 ...

  7. axios get及post方法代码示例&&方法封装

    axios get及post方法代码示例 get方法: show: function(){ //get方式 //赋值给变量self var self = this; var url = "h ...

  8. Win7下安装openssl

    安装环境: 操作系统:window7(64位) C++编译器:VS2012 -------------------------------------------------------------- ...

  9. C#中的函数(二) 有参有返回值的函数

    接上一篇 C#中的函数(-) 无参无返回值的函数 http://www.cnblogs.com/fzxiaoyi/p/8502613.html 这次研究下C#中的函数(二) 有参有返回值的函数 依然写 ...

  10. RMP和YUM软件安装

    1.卸载RPM包 rpm -e rpm包的名称 2.安装rpm包 rmp -ivh xxx.rpm 3.查询yum服务器是否有需要安装的软件 yum list|grep xxx软件列表 4.yum安装 ...