使用 Dockefile 创建一个带有 ssh 的 ubuntu 镜像

20190110 继续昨天 Commit 方式创建一个带有 ssh 的 ubuntu 镜像,使用 Dockefile 来完成

理解

1. Docker 容器使用 ssh 这个思路还是虚拟机的思路
  一个 Docker 容器就是一个服务
  一个 Docker 容器就是一个进程(执行程序)
  使用 ssh 登录到容器,确实不好理解!

2.容器既然是一个隔离的 sandbox,也只有 ssh 进去才可以最直观的了解他的环境

3.目前学习到这里还只是一些孤立的 Docker 容器
  孤立的 Docker 容器能有什么作用?
  一大堆不同形状的积木,只有拼接起来才能知道他们有什么用处!

4.只有面向一个 project ,才能理解 service 的用途!
  只有面向一个 task ,才能理解 project 的用途!

5. Dockerfile 可以创造各种容器(service),要让他们组合出来一个 project
   还要继续学习 Compose

1、准备一个空目录

mkdir ubuntu_ssh
cd sshd_ubuntu/
touch Dockerfile start.sh

start.sh 文件,加入一条 shell 命令
vim start.sh
	#!/bin/bash
	# by wzh 20190108
	/usr/sbin/sshd -D

2、添加以下内容

#This dockerfile uses the ubuntu latest(18.04)
# by wzh 20190110
FROM ubuntu
MAINTAINER Username 13501062476@139.com
RUN apt-get update -y

# 安装 sshd 和几个基础工具
RUN apt-get install openssh-server vim net-tools iputils-ping iproute2 -y && mkdir -p /var/run/sshd

# 插入一行:允许 root 登录 PermitRootLogin yes
RUN sed -i "N;92a PermitRootLogin yes" /etc/ssh/sshd_config

# 清除残留
RUN apt-get clean && apt-get autoclean && apt-get autoremove

# copy 开机启动 sshd 脚本
ADD start.sh /start.sh
RUN chmod 755 /start.sh

EXPOSE 22
CMD [/start.sh]

3、开始 build

** 这是第三次尝试成功的记录,前两次失败建大记录在后面

docker build -t ubuntu1804-ssh/v3 .
返回结果:
	...
	Successfully built c4274a7717b4
	Successfully tagged ubuntu1804-ssh/v2:latest`

4、建立容器

docker run -d -p 10033:22 --name ubuntu-ssh4 ubuntu1804-ssh/v3 /start.sh

5、进入容器,确认 sshd,设置密码

docker exec -it ubuntu-ssh4 bash

确认 sshd 服务
ps -ef |grep ssh

设置 root 密码 (ubuntu18.04 这是必须的!)
** 安全起见,不在 Dockerfile 里面设置 root 密码
passwd

查看容器 ip
ip a
	18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
		link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
		inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
		   valid_lft forever preferred_lft forever

6、 宿主机测试

ssh root@172.17.0.2

7、外网测试

ssh root@192.168.1.192 -p 10033

8、错误处理记录

1). 第一次Dockerfile apt-get mei you没有加上 -y
docker build -t ubuntu1804-ssh/v2 .
出现以下错误
	`After this operation, 158 MB of additional disk space will be used.
	Do you want to continue? [Y/n] Abort.
	The command '/bin/sh -c apt-get install openssh-server vim net-tools iputils-ping iproute2&& mkdir -p /var/run/sshd' returned a non-zero code: 1
`
** 原因:build 执行中不能再处理 apt 交互

2). 加上 -y,之后再来 build
还是出现错误!
	`E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/s/systemd/systemd_237-3ubuntu10.9_amd64.deb  Hash Sum mismatch
	   Hashes of expected file:
		- SHA256:be99852295ebb54570376f56df1e9481a40264b7c8d05dfab56a7805394f5793
		- SHA1:789ee5d1b3e47be7489271524dbf22e9d37ffb83 [weak]
		- MD5Sum:21cdcd6ccf6b635db5273b521a03b2ac [weak]
		- Filesize:2895496 [weak]
	   Hashes of received file:
		- SHA256:1a6e975dd88105609a20ab3fa1c961d735662716adb8e8c6303eac2017d3f802
		- SHA1:6f179e08e2a2d0871c6a1ad7ff30601ce21d44f9 [weak]
		- MD5Sum:f00fd875f5a0e29421ec25c9d85ee559 [weak]
		- Filesize:2895496 [weak]
	   Last modification reported: Mon, 19 Nov 2018 16:38:58 +0000
	E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
`
** 原因: 之前错误镜像已经存在!必须先删除掉!
查看发现 2 个 <none> 镜像
ssh$ docker images
`	REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
	<none>                       <none>              68624520350a        13 minutes ago      111MB
	<none>                       <none>              6453f0ba74e9        17 minutes ago      111MB
	...`

3). 删除以上 2 个 镜像,发现删除不了!
	先删除失败的 容器
	再删除失败的 镜像

4). 第三次重新 build
	docker build -t ubuntu1804-ssh/v2 .

	中途出现红色
	debconf: delaying package configuration, since apt-utils is not installed

	终于看到了
	Successfully built c4274a7717b4
	Successfully tagged ubuntu1804-ssh/v2:latest

5). 有个疑问:
	前面两次都出现错误,产生了 <none> 的镜像可以理解,怎么会产生新的容器呢?

转载于:https://my.oschina.net/u/1440971/blog/2999698

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值