docker 安装SSH
RAE2,072人阅读
首先,docker并不是虚拟机,一般并不存在用户访问控制,但有些时候想让用户通过密码才能访问虚拟集命令行,则可以通过ssh连接来实现。
但有个前提就是不能让用户直接接触宿主机,因为使用docker exec
和docker cp
等,是可以不需密码直接访问docker内部文件。
下面开始正文为python-slim镜像配置ssh访问
为debian python 镜像安装sshd
启动docker image时可以指定端口映射 docker run -ditp 23:22 [imageID]
安装过程
#!/bin/bash
# 更新安装源
cd /etc/apt/
mv sources.list sources.list.backup
echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib" > sources.list
echo "deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib" >> sources.list
echo "deb http://mirrors.aliyun.com/debian-security buster/updates main" >> sources.list
echo "deb-src http://mirrors.aliyun.com/debian-security buster/updates main" >> sources.list
echo "deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> sources.list
echo "deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib" >> sources.list
echo "deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> sources.list
echo "deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" >> sources.list
# 更新软件源
apt-get update
# 安装SSHD
apt-get install openssh-server -y
# 安装完成后启动服务
service ssh start
# 查看是否启动成功
ps -e
# 看看是否出现sshd
# 配置SSH
vim /etc/ssh/sshd_config
# 修改允许root登录
PermitRootLogin yes
# 重启ssh服务
/etc/init.d/ssh restart
# 修改root用户
passwd root
生成docker镜像
配置好环境后,生成自己的镜像文件
# docker commit [容器ID] [名称]:[版本]
docker commit 42ee9eb65dcb jenkins-ssh:1.0
docker 配置开机自动启动脚本
配置脚本
#!/bin/bash
/etc/init.d/ssh start
java -jar /usr/share/jenkins/jenkins.war
配置docker文件
version: '3'
services:
jenkins:
hostname: jenkins
container_name: jenkins-java
image: jenkins-ssh:1.0
restart: always
privileged: true
user: root
command: /bin/bash /var/jenkins_home/run.sh
ports:
- "8083:8080"
- "23:22"
volumes:
- /data/docker/jenkins-java/:/var/jenkins_home
参考链接:https://blog.csdn.net/xzl9811wl/article/details/107060382
评论 | 0 条评论