Docker构建alpine带ssh镜像

117 次查看 0 条评论

1、创建Dockerfile文件

FROM alpine:3.10
MAINTAINER alpine:sshd (649781645@qq.com)
RUN cp -a /etc/apk/repositories /etc/apk/repositories.bak && \
echo 'http://mirrors.aliyun.com/alpine/latest-stable/main/' > /etc/apk/repositories && \
echo 'http://mirrors.aliyun.com/alpine/latest-stable/community/' >> /etc/apk/repositories
RUN sed -i 's/\\h:\\w\\$ /[\\u@\\h \\W]\\$ /g' /etc/profile && \
echo '' > /etc/motd
RUN apk update && \
apk add --no-cache openssh tzdata bash && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config && \
sed -i 's?/root:/bin/ash?/root:/bin/bash?g' /etc/passwd && \
ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key && \
ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key && \
ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key && \
ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key && \
echo "root:123456" | chpasswd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

2、构建镜像

docker build -t alpine:sshd .

3、运行容器

docker run -d -p 2222:22 alpine:sshd

4、登录Alpine容器

ssh -p 2222 root@server-ip

密码设置为123456,可以自己修改Dockerfile中的定义。

5、以服务方式运行(实验性质)

5.1、Dockerfile

FROM alpine:3.10
MAINTAINER alpine:sshd (649781645@qq.com)
RUN cp -a /etc/apk/repositories /etc/apk/repositories.bak && \
echo 'http://mirrors.aliyun.com/alpine/latest-stable/main/' > /etc/apk/repositories && \
echo 'http://mirrors.aliyun.com/alpine/latest-stable/community/' >> /etc/apk/repositories
RUN sed -i 's/\\h:\\w\\$ /[\\u@\\h \\W]\\$ /g' /etc/profile && \
echo '' > /etc/motd
RUN apk update && \
apk add --no-cache openssh openrc tzdata bash&& \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config && \
sed -i 's?/root:/bin/ash?/root:/bin/bash?g' /etc/passwd && \
ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key && \
ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key && \
ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key && \
ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key && \
rc-update add sshd default && \
echo "root:123456" | chpasswd
EXPOSE 22
ENTRYPOINT ["/sbin/init"]

5.2、Build image

docker build -t alpine:test .

5.3、Run Container

## 权限不足可能有未知问题
docker run -d -p 2222:22 alpine:test
## 特权运行(服务hwdrivers还是启动失败,可能有未知问题)
docker run -d -p 2222:22 --privileged  alpine:test
## 直接使用本镜像
docker run -d -p 2222:22 k649781645/alpine:test
docker run -d -p 2222:22 --privileged k649781645/alpine:test

6、删除镜像

## 首先得删除使用该镜像的容器
docker rm -f xxx
docker rmi alpine:sshd
## 查看并删除none镜像
docker images | grep "none" | awk '{print $3}'
docker rmi $(docker images | grep "none" | awk '{print $3}')

本文镜像已Push都Docker hub上:https://hub.docker.com/u/k649781645

Linux Docker , Alpine

暂无评论