四、Docker镜像制作

1.基于容器制作镜像

ssh例子:
 [root@docker ~]# docker rm -f `docker ps -qa`
 [root@docker ~]# docker run -it --name "oldguo_sshv1" centos:6.9 /bin/bash
[root@df8386465269 /]# passwd
 mv /etc/yum.repos.d/*.repo /tmp
 echo -e "[ftp]\nname=ftp\nbaseurl=ftp://10.0.0.110/pub/centos6\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
yum makecache fast && yum install openssh-server -y
 /etc/init.d/sshd start     ----->重要:ssh第一次启动时,需要生成秘钥,生成pam验证配置文件
 /etc/init.d/sshd stop
 "hang" 运行sshd,并丢到后台
 /usr/sbin/sshd -D
docker commit oldguo_sshv1 oldguo/sshd:v1

2.基于Dockerfile构建简易镜像

 1)

例子: centos 6.9+SSH 
[root@docker test]# cat dockerfile 
FROM centos:6.9
RUN mv /etc/yum.repos.d/*.repo /tmp
RUN echo -e "[ftp]\nname=ftp\nbaseurl=ftp://10.0.0.100/pub\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
RUN yum makecache fast && yum install openssh-server -y
RUN echo "123456"|passwd --stdin root
RUN /etc/init.d/sshd start
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

 2)双服务 sshd+httpd +volumns

mkdir  -p /opt/Dockerfile/app/ssh_httpd_vol

[root@docker ssh_httpd]# vim Dockerfile 
FROM centos:6.9
RUN mv /etc/yum.repos.d/*.repo /tmp
RUN echo -e "[ftp]\nname=ftp\nbaseurl=ftp://10.0.0.110/pub/centos6\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
RUN yum makecache fast && yum install openssh-server -y
RUN yum install -y httpd
RUN echo "123456"|passwd --stdin root
RUN /etc/init.d/sshd start
EXPOSE 22
EXPOSE 80
ADD ssh_httpd.sh /opt/ssh_httpd.sh
VOLUME /var/www/html
CMD ["/bin/bash","/opt/ssh_httpd.sh"]

[root@docker ssh_httpd]# cat ssh_httpd.sh 
#!/bin/bash
/etc/init.d/httpd start
/usr/sbin/sshd -D

docker build -t "oldguo/ssh_httpd_vol:v1" .

 3)centos7.5+sshd

 

方法1:
 ## Centos7.5
[root@docker sshd]# cat dockerfile 
FROM centos:7.5.1804
FROM centos:7.5.1804
RUN mv /etc/yum.repos.d/*.repo /tmp
RUN echo -e "[ftp]\nname=ftp\nbaseurl=ftp://10.0.0.100/pub\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
RUN yum install -y openssh-server
RUN yum install -y openssh-clients
RUN yum install -y sed
RUN yum install net-tools* -y
RUN yum install iproute-* -y
RUN yum install NetworkManager -y
RUN mkdir /var/run/sshd
RUN echo 'UseDNS no' >> /etc/ssh/sshd_config
RUN sed -i -e '/pam_loginuid.so/d' /etc/pam.d/sshd
RUN echo 'root:123456' | chpasswd
RUN /usr/bin/ssh-keygen -A
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

方法二:
##制作支持systemd的7.5镜像
https://forums.docker.com/t/any-simple-and-safe-way-to-start-services-on-centos7-systemd/5695/10
https://access.redhat.com/articles/754933

FROM centos:7.5.1804
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

FROM oldguo/centos7.5:v1
RUN mv /etc/yum.repos.d/*.repo /tmp
RUN echo -e "[ftp]\nname=ftp\nbaseurl=ftp://10.0.0.100/pub\ngpgcheck=0">/etc/yum.repos.d/ftp.repo
RUN yum makecache fast
RUN yum install -y openssh-server
RUN yum install -y openssh-clients
RUN yum install -y sed
RUN yum install net-tools* -y
RUN yum install iproute-* -y
RUN yum install NetworkManager -y
RUN echo 'root:123456' | chpasswd
RUN systemctl enable sshd
EXPOSE 22
CMD ["/usr/sbin/init"]

测试
docker run -tid --privileged=true --name testssh -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 2022:22 test:v1

  

 

posted @ 2020-05-14 17:36  zhuguoqing  阅读(200)  评论(0编辑  收藏  举报