当前位置:   article > 正文

Docker Dockerfile Docker容器对外的22端口的监听_dockerfile 22端口

dockerfile 22端口

什么是Dockerfile


由于Docker官网公共仓库镜像大多不完整,无法真正满足企业的生产环境系统,此时需要我们自行定制镜像或者重新打包镜像。

Docker镜像制作是管理员的必备工作之一,Docker镜像制作的方法主要有两种,制作方法如下:

  1. Docker commit|export将新容器提交至Images列表
  2. 编写Dockerfile,bulid新的镜像至镜像列表

Dockerfile正是用来构建Docker镜像的构建文件,是由一系列的命令和参数构成的脚本,Dokcerfle的构建步骤

(1)编写Dokcerfle文件(2)docker buld 生成新的镜像(3)docker run 运行镜像      https://hub.docker.com/

 

Docker file制作过程解析


基础知识

1,每条保留字指令影必须为大写字母后面要眼随至少一个参数

2,指令从上到下顺序执行

3,#表示注释

4,每条指令都会创建一个新的镜像层,并对镜像提交

大致流程

1,docker从基础镜像运行一个容器

2,执行一条指令并对容器进行修改

3,执行类似于docker commit的操作提文一个新的镜像,

4,docker再基于刚提交的新的镜像运行一个新的容器

5,执行Dockerfile的下一个指令再从执行第2点直到没有指令

 

基于Centos7镜像制作开放22端口开ssh服务登入Centos7


  1. [root@localhost ssh]# chmod 600 ssh_host_ed25519_key
  2. [root@localhost ssh]# cp ssh_host_ed25519_key /test/
  3. [root@localhost ssh]# chmod 600 ssh_host_ecdsa_key
  4. [root@localhost ssh]# cp ssh_host_ecdsa_key /test/
  5. [root@localhost ssh]# chmod 600 ssh_host_rsa_key
  6. [root@localhost ssh]# cp ssh_host_rsa_key /test/
  7. [root@localhost docker]# tar -czf ssh.tar ssh*
  8. [root@localhost docker]# ll
  9. total 20
  10. -rw-r--r-- 1 root root 394 Jul 23 20:58 Dockerfile
  11. -rw------- 1 root root 227 Jul 23 15:29 ssh_host_ecdsa_key
  12. -rw------- 1 root root 387 Jul 23 15:29 ssh_host_ed25519_key
  13. -rw------- 1 root root 1675 Jul 23 15:29 ssh_host_rsa_key
  14. -rw-r--r-- 1 root root 1829 Jul 23 15:31 ssh.tar
  15. [root@localhost docker]# cat Dockerfile
  16. FROM ansible/centos7-ansible
  17. MAINTAINER complicated
  18. RUN echo 12345 |passwd --stdin root &&\
  19. yum install net-tools openssh-server -y &&\
  20. rm -rf /etc/yum.repos.d/epel*
  21. ADD ssh.tar /etc/ssh
  22. RUN sed -i '/DNS/s/#//g' /etc/ssh/sshd_config &&\
  23. sed -i '/DNS/s/yes/no/g' /etc/ssh/sshd_config &&\
  24. /usr/sbin/sshd
  25. EXPOSE 22
  26. WORKDIR /root
  27. CMD /usr/sbin/sshd;/bin/bash
  28. [root@localhost docker]# docker build -t coten7-ssh-v1 .
  29. [root@localhost docker]# docker images
  30. REPOSITORY TAG IMAGE ID CREATED SIZE
  31. coten7-ssh-v1 latest 3c72505e3d2a About a minute ago 573MB
  32. ansible/centos7-ansible latest 688353a31fde 3 years ago 447MB
  33. [root@localhost docker]# docker run -itd coten7-ssh-v1
  34. 23d2beae2b00d326c8baab44a5806260cf78db63ffd1662b0816b77d0852ad80
  35. [root@localhost docker]# docker ps
  36. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  37. 23d2beae2b00 coten7-ssh-v1 "/bin/sh -c /usr/sbi…" 8 seconds ago Up 8 seconds 22/tcp wizardly_hugle
  38. [root@localhost docker]# docker exec -it 23d2beae2b00 /bin/bash
  39. [root@23d2beae2b00 ~]# netstat -tpln
  40. Active Internet connections (only servers)
  41. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  42. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7/sshd
  43. tcp6 0 0 :::22 :::* LISTEN 7/sshd
  44. [root@localhost docker]# docker inspect 23d2beae2b00 | grep -i ipaddr | tail -1
  45. "IPAddress": "192.168.179.1",
  46. #使用shell去登入
  47. Connecting to 192.168.179.1:22...
  48. Connection established.
  49. To escape to local shell, press 'Ctrl+Alt+]'.
  50. WARNING! The remote SSH server rejected X11 forwarding request.
  51. Last failed login: Thu Jul 23 13:18:21 UTC 2020 from 192.168.179.4 on ssh:notty
  52. There were 2 failed login attempts since the last successful login.
  53. [root@23d2beae2b00 ~]#

 

开放22端口并且配置容器和宿主机免密登入


  1. [root@www ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:t16JQoHMQNlQapgpQFfRkNHttXzRM+rZzouWh4pbBr0 root@localhost.localdomain
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. |o. o=XO . . |
  13. |. .+.*.+ . . . + |
  14. |. + o + o o . o o|
  15. | . . o.o o |
  16. | S...o o |
  17. | . ..o.+ . |
  18. | . oEo = |
  19. | o+. +.+ |
  20. | oo.o....|
  21. +----[SHA256]-----+
  22. [root@www ~]# ll .ssh/
  23. total 12
  24. -rw------- 1 root root 1679 Jul 25 19:35 id_rsa
  25. -rw-r--r-- 1 root root 402 Jul 25 19:35 id_rsa.pub --私钥
  26. -rw-r--r-- 1 root root 172 Jul 25 21:25 known_hosts --公钥
  27. #公钥要传给容器,或者制作dockerfile,这样宿主机ssh登入容器才能免密码登入
  28. [root@www ~]# cp .ssh/id_rsa.pub /docker/
  29. [root@www docker]# ll
  30. total 24
  31. -rw-r--r-- 1 root root 385 Jul 25 21:21 Dockerfile
  32. -rw-r--r-- 1 root root 402 Jul 25 19:36 id_rsa.pub
  33. -rw------- 1 root root 227 Jul 25 21:14 ssh_host_ecdsa_key
  34. -rw------- 1 root root 387 Jul 25 21:13 ssh_host_ed25519_key
  35. -rw------- 1 root root 1679 Jul 25 21:14 ssh_host_rsa_key
  36. -rw-r--r-- 1 root root 1833 Jul 25 21:15 ssh.tar
  37. [root@www docker]# cat Dockerfile
  38. FROM ansible/centos7-ansible
  39. MAINTAINER Complicated
  40. RUN echo 123456 | passwd --stdin root &&\
  41. yum install net-tools openssh-server -y
  42. ADD ssh.tar /etc/ssh
  43. RUN sed -i '/DNS/s/#//g' /etc/ssh/sshd_config &&\
  44. sed -i '/DNS/s/yes/no/g' /etc/ssh/sshd_config &&\
  45. mkdir -p /root/.ssh
  46. COPY id_rsa.pub /root/.ssh/authorized_keys
  47. WORKDIR /root
  48. CMD /usr/sbin/sshd;/bin/bash
  1. [root@www ~]# docker run -itd --name os1 --privileged -p 6522:22 centos7-ssh:v2
  2. 8b047b9db23701025f10f8364894d1170f065fe7398fd0f4b331451725f5d0d2
  3. [root@www ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 8b047b9db237 centos7-ssh:v2 "/bin/sh -c /usr/sbi?? 2 seconds ago Up 2 seconds 0.0.0.0:6522->22/tcp os1
  6. [root@www ~]# docker exec 8b047 netstat -tpln
  7. Active Internet connections (only servers)
  8. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  9. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7/sshd
  10. tcp6 0 0 :::22 :::* LISTEN 7/sshd
  11. [root@www ~]# docker exec 8b047 ifconfig
  12. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  13. inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
  14. ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
  15. RX packets 8 bytes 648 (648.0 B)
  16. RX errors 0 dropped 0 overruns 0 frame 0
  17. TX packets 0 bytes 0 (0.0 B)
  18. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  19. [root@www ~]# ssh -l root 172.17.0.2
  20. The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
  21. ECDSA key fingerprint is SHA256:f+ZyP4Ec4dPDSYUXXrhZPdSIkrNyh2M+ALTi77N+vdY.
  22. ECDSA key fingerprint is MD5:fd:3a:e0:d3:6a:37:01:44:19:8e:ff:cf:29:c0:c4:9b.
  23. Are you sure you want to continue connecting (yes/no)? yes
  24. Warning: Permanently added '172.17.0.2' (ECDSA) to the list of known hosts.
  25. [root@8b047b9db237 ~]# exit
  26. logout
  27. Connection to 172.17.0.2 closed.
  28. [root@www ~]# ssh -l root 172.17.0.2
  29. Last login: Sat Jul 25 13:25:04 2020 from 172.17.0.1

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/587397
推荐阅读
相关标签
  

闽ICP备14008679号