一看必会系列:dockerfile 练习一 创建支持sshd 的ubuntu镜像

来源:本站原创 Docker 超过178 views围观 0条评论

dockerfile 创建支持sshd 的ubuntu镜像

先建目录

root@docker:/opt/dockerfile/ubuntu_sshd# !t
tree ../
../
└── ubuntu_sshd
    ├── AZURE-FAT-OPS-PRI
    ├── dockerfile
    └── run.sh

dockfile的内容

root@docker:/opt/dockerfile/ubuntu_sshd# cat dockerfile
FROM reg.ccie.wang/library/ubuntu:18.04
MAINTAINER jeff@jdccie.com
RUN sed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list #替换本地原
RUN apt-get update
RUN apt-get install -y openssh-server   #安装sshd
RUN mkdir /var/run/sshd                   #需要建议目录不然无法启动
RUN mkdir /root/.ssh
RUN useradd jeff001                      #新建用户
RUN echo jeff001:111111 | chpasswd       #修改密码
ADD run.sh /run.sh                       #增加脚本启动 sshd
RUN chmod 755 /run.sh                    #修改执行权限
EXPOSE 22                                #容器暴露 22端口

启动执行脚本的网容
root@docker:/opt/dockerfile/ubuntu_sshd# cat run.sh
#!/bin/bash
/usr/sbin/sshd -D

在docker同目录下build
docker build -t sshd:dockerfile004 .

查看生成的镜像
root@docker:/opt/dockerfile/ubuntu_sshd# docker images |grep dockerfile
sshd                              dockerfile004       c4b27b86c0b8        5 minutes ago       228MB
sshd                              dockerfile003       74213a63a585        18 minutes ago      228MB
sshd                              dockerfile002       4981e935d5df        38 minutes ago      228MB
sshd                              dockerfile001       1a767dd4e3e2        About an hour ago   228MB
root@docker:/opt/dockerfile/ubuntu_sshd#

运行  将容器的22号映射到host的12224端口
docker run -p 12224:22 -d sshd:dockerfile004  /run.sh
 
root@docker:/opt/dockerfile/ubuntu_sshd# docker ps |grep dockerfile
c68fbed21f8b        sshd:dockerfile004                   "/run.sh"                3 minutes ago       Up 3 minutes        0.0.0.0:12224->22/tcp   optimistic_curie

测试是否能ssh   ssh 用户@主机IP ssh端口
ssh jeff001@192.168.10.67 12224

提示如下即成功
Connecting to 192.168.10.67:12224…
Connection established.
To escape to local shell, press ‘Ctrl+Alt+]’.

WARNING! The remote SSH server rejected X11 forwarding request.
Welcome to Ubuntu 18.04.1 LTS (GNU/Linux 4.18.0-16-generic x86_64)

测试成功

推送镜像到 仓库
#标记成仓库相关的  docker tag SOURCE_IMAGE[:TAG] reg.ccie.wang/test/IMAGE[:TAG]
docker tag sshd:dockerfile004 reg.ccie.wang/test/ubuntu:sshd001
#推镜像到仓库    docker push reg.ccie.wang/test/IMAGE[:TAG]
docker push reg.ccie.wang/test/ubuntu:sshd001
The push refers to repository [reg.ccie.wang/test/ubuntu]
e8606e3e9f2b: Preparing

denied: requested access to the resource is denied   #说明没有登陆或没有权限。

先登陆到仓库
root@docker:/opt/dockerfile/ubuntu_sshd# sh +x /root/login.sh
WARNING! Using –password via the CLI is insecure. Use –password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

再次推送到仓库
root@docker:/opt/dockerfile/ubuntu_sshd# docker push reg.ccie.wang/test/ubuntu:sshd001
The push refers to repository [reg.ccie.wang/test/ubuntu]
e8606e3e9f2b: Pushed
638b4a9375b3: Pushed
38f2771441e5: Pushed
d41a6372b93f: Pushed
992b79b09792: Pushed
d76a7595b8f4: Pushed
03e5cb7ca68f: Pushed
23f382505b40: Pushed
4b7d93055d87: Pushed
663e8522d78b: Pushed
283fb404ea94: Pushed
bebe7ce6215a: Pushed
sshd001: digest: sha256:5c60c99a4d59a40dafe5dfe26a37679804e1d2d09ec90bbe975e5abd2a5c0361 size: 3024

以上为推送成功

拉取镜像测试
1.删除现有镜像

docker rmi reg.ccie.wang/test/ubuntu:sshd001
Untagged: reg.ccie.wang/test/ubuntu:sshd001
Untagged: reg.ccie.wang/test/ubuntu@sha256:5c60c99a4d59a40dafe5dfe26a37679804e1d2d09ec90bbe975e5abd2a5c0361
以上为成功

2.从仓库拉取镜像  docker pull reg.ccie.wang/test/ubuntu:sshd001

root@docker:/opt/dockerfile/ubuntu_sshd# docker pull reg.ccie.wang/test/ubuntu:sshd001
sshd001: Pulling from test/ubuntu
Digest: sha256:5c60c99a4d59a40dafe5dfe26a37679804e1d2d09ec90bbe975e5abd2a5c0361
Status: Downloaded newer image for reg.ccie.wang/test/ubuntu:sshd001

验证是否成功
root@docker:/opt/dockerfile/ubuntu_sshd# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
reg.ccie.wang/test/ubuntu         sshd001             c4b27b86c0b8        14 hours ago        228MB

使用该镜像生成容器
#
docker run -p 12225:22 -d reg.ccie.wang/test/ubuntu:sshd001  /run.sh
7ae5fd70960dc933c1f98106a360da3f14c5e93d7cd17bce0f896f69f041fc67
#验证
root@docker:/opt/dockerfile/ubuntu_sshd# docker ps |grep sshd001
7ae5fd70960d        reg.ccie.wang/test/ubuntu:sshd001    "/run.sh"                10 seconds ago      Up 7 seconds        0.0.0.0:12225->22/tcp   happy_albattani
以上信息为成功

也可把当前容器打包一个镜像文件docker commit  容器ID 新TAG

# docker commit 7ae5fd70960d ubuntu:sshd002
sha256:a9c6f22446466cbf920d5e22f95e0d378558d3852a3bc1592a701d78777ba222

验证
root@docker:/opt/dockerfile/ubuntu_sshd# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
ubuntu                            sshd002             a9c6f2244646        5 seconds ago       228MB

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=4082转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点

暂时还木有人评论,坐等沙发!
发表评论

您必须 [ 登录 ] 才能发表留言!