Docker实战:基于centos7镜像创建ssh容器

基于Dockerfile创建

mkdir sshdocker
cd sshdocker
vi Dockerfile
vi run.sh
chmod +x run.sh

DOCKERFILE

# cat Dockerfile

FROM centos:latest
MAINTAINER [email protected]
RUN set -ex \
    && yum install -y openssh-server \
    && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' \
    && ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key -N '' \
    && ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key -N '' \
    && echo "123123" | passwd --stdin root

ADD run.sh /run.sh
EXPOSE 22
CMD ["/run.sh"]

run.sh

# cat run.sh
#! /bin/bash
/usr/sbin/sshd -D

文件可以从我的git拉

git clone https://github.com/106140873/sshdocker.git
正克隆到 'sshdocker'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 7 (delta 0), reused 7 (delta 0), pack-reused 0
Unpacking objects: 100% (7/7), done.
# cd sshdocker/

建立镜像

docker build -t huangshumao/sshdocker ./

查看镜像,看到刚创建的镜像大小是309MB,有三个layer

]# docker images huangshumao/sshdocker
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
huangshumao/sshdocker   latest              4ba91b1e5c6c        8 minutes ago       309MB
# docker inspect huangshumao/sshdocker:latest |grep -A 4 layers
            "Type": "layers",
            "Layers": [
                "sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854",
                "sha256:56f5559cbd625c61d3da4a8689126a3fa2f18e2107777938d756a29bb5e04c48",
                "sha256:0dc315fbb8f5342ee3e95930ee82445a6aa46741554a18a213b9b61136204d57"

创建容器

docker run --name sshdocker -d -p 22222:22 huangshumao/sshdocker

 

试下ssh到容器,密码是123123

#ssh [email protected] -p 22222
The authenticity of host '[127.0.0.1]:22222 ([127.0.0.1]:22222)' can't be established.
RSA key fingerprint is SHA256:BGOa73R1c8tXt8NWJmzPJtlLLsDyuRTF37Jf0APqVPk.
RSA key fingerprint is MD5:52:ae:c9:1f:3a:17:f8:17:f8:a6:a8:bc:64:ac:07:85.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:22222' (RSA) to the list of known hosts.
[email protected]'s password:
[root@862cdd0338fa ~]# exit

原理参考:

https://blog.51cto.com/13735335/2288319

你可能感兴趣的:(DOCKER)