一看必會系列:dockerfile 練習一 創建支持sshd 的ubuntu鏡像

來源:本站原創 Docker 超過292 views圍觀 0條評論

dockerfile 創建支持sshd 的ubuntu鏡像

先建目錄

[email protected]:/opt/dockerfile/ubuntu_sshd# !t
tree ../
../
└── ubuntu_sshd
    ├── AZURE-FAT-OPS-PRI
    ├── dockerfile
    └── run.sh

dockfile的內容

[email protected]:/opt/dockerfile/ubuntu_sshd# cat dockerfile
FROM reg.ccie.wang/library/ubuntu:18.04
MAINTAINER [email protected]
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端口

啟動執行腳本的網容
[email protected]:/opt/dockerfile/ubuntu_sshd# cat run.sh
#!/bin/bash
/usr/sbin/sshd -D

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

查看生成的鏡像
[email protected]:/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
[email protected]:/opt/dockerfile/ubuntu_sshd#

運行  將容器的22號映射到host的12224端口
docker run -p 12224:22 -d sshd:dockerfile004  /run.sh
 
[email protected]:/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 [email protected] 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   #說明沒有登陸或沒有權限。

先登陸到倉庫
[email protected]:/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

再次推送到倉庫
[email protected]:/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/[email protected]:5c60c99a4d59a40dafe5dfe26a37679804e1d2d09ec90bbe975e5abd2a5c0361
以上為成功

2.從倉庫拉取鏡像  docker pull reg.ccie.wang/test/ubuntu:sshd001

[email protected]:/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

驗證是否成功
[email protected]:/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
#驗證
[email protected]:/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

驗證
[email protected]:/opt/dockerfile/ubuntu_sshd# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
ubuntu                            sshd002             a9c6f2244646        5 seconds ago       228MB

文章出自:CCIE那點事 http://www.qdxgqk.live/ 版權所有。本站文章除注明出處外,皆為作者原創文章,可自由引用,但請注明來源。 禁止全文轉載。
本文鏈接:http://www.qdxgqk.live/?p=4082轉載請注明轉自CCIE那點事
如果喜歡:點此訂閱本站
  • 相關文章
  • 為您推薦
  • 各種觀點
?
暫時還木有人評論,坐等沙發!
發表評論

您必須 [ 登錄 ] 才能發表留言!

?
?
萌宠夺宝游戏