基于alpine用dockerfile创建的ssh镜像

1、下载alpine镜像

1
2
3
4
5
6
7
8
9
10
[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.创建一个工作目录
1
2
3
4
5
6
[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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 指定创建的基础镜像
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.创建镜像
1
2
3
4
# 在dockerfile所在的目录下
[root@docker43 alpine_ssh] # pwd
/opt/alpine_ssh
[root@docker43 alpine_ssh] # docker build -t alpine:sshd .
 

3、创建容器测试

  创建容器

1
2
3
4
[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

  测试

1
2
3
4
5
6
7
8
9
10
11
12
13
[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:~ #
 

4、问题总结

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Docker Swarm是Docker的原生集群管理工具,可以用于在多个主机上运行和管理容器。Dockerfile是一种用于定义Docker镜像构建过程的文本文件。 使用Docker Swarm时,可以使用Dockerfile创建镜像,但其实创建镜像的过程与在单个主机上创建镜像的过程非常相似。主要的区别是,在Swarm中创建镜像将会在整个集群中被使用,而非仅限于单个主机。 首先,在Swarm集群的主节点上创建一个Dockerfile,该文件包含了构建镜像的指令和配置。以一个简单的Node.js应用为例,可以定义如下的Dockerfile: ``` FROM node:14-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD [ "npm", "start" ] ``` 然后,在主节点的终端中,使用`docker build`命令来构建镜像。例如,使用以下命令创建一个名为`my-app`的镜像: ``` docker build -t my-app . ``` 接下来,使用`docker push`命令将构建好的镜像推送到Docker镜像仓库。例如,可以推送到Docker Hub: ``` docker push username/my-app ``` 最后,在Swarm集群的其他节点上使用`docker service`命令来创建服务,并使用先前构建的镜像作为服务的副本。例如,使用以下命令创建一个名为`my-app`的服务: ``` docker service create --name my-app --replicas 3 username/my-app ``` 这将在集群的多个节点上运行三个容器的副本,以提供高可用性和负载均衡的服务。 通过以上步骤,我们可以使用DockerfileDocker Swarm集群中创建镜像,并在集群中运行多个容器的副本来提供服务。这种方式使得应用程序可以更加灵活地在集群中运行,并可以根据需要进行扩展和管理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值