1、下载alpine镜像

[root@docker43 ~]# docker pull alpine
Using default tag: latest
Trying to pull repository docker.io/library/alpine ...
latest: Pulling from docker.io/library/alpine
4fe2ade4980c: Pull complete
Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528
Status: Downloaded newer image for docker.io/alpine:latest
[root@docker43 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/alpine    latest              196d12cf6ab1        3 weeks ago         4.41 MB

2、编写dockerfile

2.1.创建一个工作目录

[root@docker43 ~]# cd /opt/
[root@docker43 opt]# mkdir alpine_ssh && cd alpine_ssh && touch Dockerfile[root@docker43 alpine_ssh]# ll
总用量 4
-rw-r--r-- 1 root root 654 10月  3 23:21 Dockerfile

2.2.编写Dockerfile

# 指定创建的基础镜像
FROM alpine# 作者描述信息
MAINTAINER alpine_sshd (zhujingzhi@123.com)# 替换阿里云的源
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
RUN echo "http://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories# 同步时间# 更新源、安装openssh 并修改配置文件和生成key 并且同步时间
RUN apk update && \apk add --no-cache openssh-server tzdata && \cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config && \ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key && \ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key && \ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key && \echo "root:admin" | chpasswd# 开放22端口
EXPOSE 22# 执行ssh启动命令
CMD ["/usr/sbin/sshd", "-D"]

2.3.创建镜像

# 在dockerfile所在的目录下
[root@docker43 alpine_ssh]# pwd
/opt/alpine_ssh
[root@docker43 alpine_ssh]# docker build -t alpine:sshd .

3、创建容器测试

创建容器

[root@docker43 alpine_ssh]# docker run -itd -p 10022:22 --name alpine_ssh_v1 alpine:sshd
[root@docker43 alpine_ssh]# docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
b353f5f3b703        alpine:sshd         "/usr/sbin/sshd -D"   17 minutes ago      Up 17 minutes       0.0.0.0:10022->22/tcp   alpine_ssh_v1

测试

[root@docker43 alpine_ssh]# ssh root@127.0.0.1 -p10022
root@127.0.0.1's password:
Welcome to Alpine!The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org>.You can setup the system with the command: setup-alpineYou may change this message by editing /etc/motd.b353f5f3b703:~#

4、问题总结

  这些都是我在手动测试的时候遇见的,已经在写Dockerfile的时候加进去了处理方法

[root@docker43 alpine_ssh]# ssh root@127.0.0.1 -p10022
root@127.0.0.1's password:
Welcome to Alpine!
The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org>.
You can setup the system with the command: setup-alpine
You may change this message by editing /etc/motd.
b353f5f3b703:~#1. apk add --no-cache openssh-server   # 安装openssh的问题/ # apk add --no-cache openssh-server
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/x86_64/APKINDEX.tar.gz
(1/3) Installing openssh-keygen (7.7_p1-r2)
ERROR: openssh-keygen-7.7_p1-r2: package mentioned in index not found (try 'apk update')
(2/3) Installing openssh-server-common (7.7_p1-r2)
(3/3) Installing openssh-server (7.7_p1-r2)
ERROR: openssh-server-7.7_p1-r2: package mentioned in index not found (try 'apk update')
2 errors; 4 MiB in 14 packages原因是:提示源没有这个openssh的包解决方式:
在dockerfile中改为国内的源
http://mirrors.aliyun.com/alpine/latest-stable/main/
http://mirrors.aliyun.com/alpine/latest-stable/community/创建容器文件修改
[root@docker43 ~]# docker run -it alpine
/ # vi /etc/apk/repositories
http://mirrors.aliyun.com/alpine/latest-stable/main/
http://mirrors.aliyun.com/alpine/latest-stable/community/#http://dl-cdn.alpinelinux.org/alpine/v3.8/main
#http://dl-cdn.alpinelinux.org/alpine/v3.8/community# 注释或者删除原来的默认源,添加阿里云的源,然后执行apk update,在进行安装就OK了2、ssh 启动问题
/ # /etc/init.d/sshd start
/bin/sh: /etc/init.d/sshd: not found这样的方式不能启动,需要安装一个alpine的管理工具
apk add --no-cache openrc
/ # /etc/init.d/sshd start* WARNING: sshd is already starting所以使用 /usr/sbin/sshd -D 方式启动。但是又出现如下错误/ # /usr/sbin/sshd -D
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
解决方式:
ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key
ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key再次启动
/ # /usr/sbin/sshd -D启动成功3、创建容器后的网络问题
[root@docker43 opt]# docker run -it alpine
WARNING: IPv4 forwarding is disabled. Networking will not work.解决方式:
[root@docker43 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1    # 添加这一行[root@docker43 ~]# docker run -it alpine
/ #

  

linux alpine 用dockerfile创建的ssh镜像相关推荐

  1. 使用Dockerfile创建一个tomcat镜像

    docker已经看了有一段时间了,对镜像和容器也有了一个大致了解,制作一个tomcat镜像 1.首先下载linux环境的tomcat,并解压至tomcat目录 ls /tomcat apache-to ...

  2. Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较

    1.概述 创建Docker镜像的方式有三种 docker commit命令:由容器生成镜像: Dockerfile文件+docker build命令: 从本地文件系统导入:OpenVZ的模板. 关于这 ...

  3. Docker(2):使用Dockerfile创建支持SSH服务的镜像

    1.创建工作目录 # mkdir sshd_ubuntu # ls 在其中,创建Dockerfile和run.sh文件 # cd sshd_ubuntu/ # touch Dockerfile run ...

  4. Docker之使用Dockerfile创建定制化镜像(四)--技术流ken

    前言 在之前的博客<Docker端口映射及创建镜像演示(二)--技术流ken>,演示了如何使用一个现有容器创建一个镜像,以及镜像在阿里云的上传和下载. 但是这样的镜像有很大的局限性,不能根 ...

  5. Linux下使用docker创建自己的镜像并提交到docker hub

    创建镜像,是在有基础镜像的前提下,开发者需要定义属于自己的独特的开发环境,比如在centos7上安装python3的完整环境,然后形成一个centos7+python3的镜像,这样,在下次使用,就不用 ...

  6. 用Dockerfile创建一个具有ssh服务的基础Ubuntu镜像

    1.创建文件目录与文件 创建所需的目录sshd_ubuntu用于存放Dockerfile和相关文件. $ mkdir sshd_ubuntu/ $ cd sshd_ubuntu/ $ touch Do ...

  7. docker2-镜像原理及创建新的镜像

    1,镜像是什么 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码.运行时.库.环境变量和配置文件 在docker中所有应用 ...

  8. 一机玩转docker之十:创建及使用ssh镜像

    目标 创建ssh镜像,并使用ssh镜像创建容器以便上传文件. 一.以centos镜像为基础构建ssh的镜像 mkdir -p /data/build/ssh/ #创建镜像构建目录 cd /data/b ...

  9. 创建支持ssh的docker镜像

    docker容器运行,一般不能ssh,这容器的管理带来麻烦,下面将介绍如何创建支持ssh的docker镜像 首先从dock hub  下载 ubuntu的镜像 命令: docker pull ubun ...

最新文章

  1. POM.xml 标签详解
  2. Spring Cloud Gateway 入门
  3. jsp中简易版本的图片上传程序
  4. PC 远程控制 android手机的方法之一VNC
  5. Auto-Configuration Error: Cannot find gcc or CC
  6. python语言实现reverse函数翻转字符串_python 实现字符串反转的几种方法
  7. Node.js 模块之Nimble流程控制
  8. ERROR: Unrecognized command line argument: #39;use#39;
  9. Script - MapSubString()使用
  10. 批处理(bat)xcopy详解
  11. android+接入易宝支付,iOS客户端连接易宝支付接口
  12. 一些比较好的国外IT网站
  13. 程序猿生存指南-46 暴走的鸟
  14. 中国世界500强数量首超美国,但别高兴得太早
  15. opencv 单个圆形孔和针检测
  16. 【Project】Face Recognition
  17. 世界知名计算机科学家,世界顶尖计算机科学家排名出炉!两位郑州大学校友分列这两项之首...
  18. 【PHP】进一法取整、四舍五入取整、忽略小数等的取整数方法大全
  19. 现代密码学之电子货币的加密
  20. Google camera

热门文章

  1. Altium Designer原理图模板设计
  2. 机器大神 Michael Jordan 教授主题演讲:机器学习——创新视角,直面挑战》
  3. android 反编译apktool工具
  4. 杂谈:选择游戏还是应用?
  5. Oracle单实例打补丁
  6. Apache Spark 技术团队开源机器学习平台 MLflow
  7. vue-awesome-swiper
  8. 自测之Lesson6:文件I/O
  9. 基于Reddsion分布式的锁实现
  10. 在AMD-M上安装x86 OS.X.10.4.3失败