构建可以ssh连接的容器镜像

构建可以ssh连接的容器镜像

构建可以通过ssh进行连接容器镜像,实现远程登录容器的目的。

ubuntu ssh容器镜像

你可以使用以下Dockerfile来构建一个可以SSH的容器镜像:

dockerfile 复制代码
FROM ubuntu:20.04

MAINTAINER lldhsds

# 配置apt国内源
COPY sources.list /etc/apt/

# 安装OpenSSH Server
RUN apt-get update && apt-get install -y openssh-server

# 创建SSH服务的必要目录
RUN mkdir /var/run/sshd

# 设置root用户的密码
RUN echo 'root:ubuntu' | chpasswd

# 允许root用户通过SSH登录
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# 暴露SSH端口
EXPOSE 22

# 启动SSH服务
CMD ["/usr/sbin/sshd", "-D"]

在dockerfile同目录下,创建镜像源文件sources.list,内容如下:

bash 复制代码
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
# deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

使用以下命令构建和运行镜像:

bash 复制代码
docker build -t ubuntu-ssh .
docker run -d -p 2222:22 ubuntu-ssh

然后,你可以通过SSH连接到容器:

bash 复制代码
ssh root@localhost -p 2222

centos ssh容器镜像

可以使用以下Dockerfile来构建一个可以SSH的容器镜像:

dockerfile 复制代码
FROM centos:7

MAINTAINER lldhsds

RUN rm -rf /etc/yum.repos.d/*

ADD repo.tar.gz /etc/yum.repos.d/

RUN yum install openssh-clients openssh-server -y && \
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key && \
    ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key && \
    ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key && \
    sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config && \
    echo "root:centos"| chpasswd

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]

根据需要调整密码和配置!

dockerfile同目录下,创建repo.tar.gz,内容如下:

bash 复制代码
tar zcvf repo.tar.gz centos7.repo

其中cenos7.repo内容如下:

bash 复制代码
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
相关推荐
高山莫衣31 分钟前
Docker Desktop导致存储空间不足时的解决方案
docker·容器·eureka
鹏大师运维32 分钟前
在银河麒麟V10 SP1上手动安装与配置高版本Docker的完整指南
linux·运维·docker·容器·麒麟·统信uos·中科方德
Ahlson37 分钟前
【fnNAS】docker的nginx配置html
nginx·docker·容器·fnnas
LuckyLay37 分钟前
Compose 常用命令详解——AI教你学Docker
docker·容器·eureka
moppol1 小时前
容器化 vs 虚拟机:什么时候该用 Docker?什么时候必须用 VM?
运维·docker·容器
没有名字的小羊1 小时前
7.可视化的docker界面——portainer
docker·容器·eureka
电脑能手2 小时前
遇到该问题:kex_exchange_identification: read: Connection reset`的解决办法
linux·ubuntu·ssh
木头左3 小时前
Windows环境下Docker容器化的安装与设置指南
windows·docker·容器
&如歌的行板&4 小时前
如何在postman中动态请求k8s中的pod ip(基于nacos)
云原生·容器·kubernetes
泡泡_02244 小时前
云原生系统DOCKER中容器系统搭建
docker·云原生·容器