使用Dockerfile构建sshd+systemctl+nginx+tomcat

使用Dockerfile构建sshd镜像

创建sshd目录,进入并编辑Dockerfile

[root@server1 ~]# mkdir sshd && cd sshd
[root@server1 sshd]# vi Dockerfile 
FROM centos:7 
MAINTAINER new
RUN yum -y update # 更新yum源
RUN yum -y install openssh* net-tools lsof telnet passwd   #安装所需服务和工具
RUN echo "123123" | passwd --stdin root  #设置容器内的root用户密码
RUN sed -i 's/UsePAM yes/UsePAM no/g'   /etc/ssh/sshd_config  #关闭PAM认证
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key  #生成rsa密钥对
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd  #注释pam登录功能
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]  #启动容器时,启动服务

创建镜像,并运行容器

[root@server1 sshd]# docker build -t sshd:new .
[root@server1 sshd]# docker run -d -P sshd:new  #-P随机指定端口,默认从32768开始
[root@server1 sshd]# docker ps -a   
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
33ffbd91efa7         sshd:new            "/usr/sbin/sshd -D"      4 hours ago         Up 4 hours          0.0.0.0:32768->22/tcp                           eager_lichterman

验证

[root@localhost ~]# ssh localhost -p 32768
The authenticity of host '[localhost]:32768 ([::1]:32768)' can't be established.
RSA key fingerprint is SHA256:dzpdTgXSGu0cHH15x8C5xf+ZWuBtgXTmVcvN19z53r8.
RSA key fingerprint is MD5:4a:b0:ec:8f:49:c3:82:02:e9:a9:7e:48:ca:6d:16:9b.
Are you sure you want to continue connecting (yes/no)?  yes
Warning: Permanently added '[localhost]:32768' (RSA) to the list of known hosts.
root@localhost's password:
[root@33ffbd91efa7 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 50  bytes 6005 (5.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 44  bytes 6043 (5.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

systemctl服务

[root@localhost ~]# mkdir systemctl && cd systemctl
[root@localhost systemctl]# vi Dockerfile
FROM sshd:new  #基于之前的ssh容器
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"]

[root@localhost systemctl]# docker build -t systemctl:new .

创建并运行容器

[root@localhost systemctl]# docker build -t systemctl:new .
[root@localhost systemctl]# docker run --privileged -it -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemctl:new /sbin/init &

进入容器,验证

[root@localhost systemctl]# docker exec -it 60565bc2d9e2 bash
[root@60565bc2d9e2 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:sshd(8)
           man:sshd_config(5)

构建nginx容器

[root@localhost nginx]# mkdir nginx && cd nginx
[root@localhost nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <kkun>
RUN yum -y update
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.2 nginx-1.12.2
RUN cd nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make -j2 && make install
RUN ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 run.sh
EXPOSE 80
EXPOSE 443
CMD ["./run.sh"]
# 创建启动脚本
[root@localhost nginx]# vi run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx

创建镜像,运行容器,查看容器状态,随机指定端口为32776

[root@localhost nginx]# docker build -t nginx:new .
[root@localhost nginx]# docker run -dit -P nginx:new1 bash
a3b83fbd82bf5bff858f103f6d66f9076fe162d3ccb944e759af58480f0a4f74
[root@localhost nginx]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS                        PORTS                                           NAMES
a3b83fbd82bf        nginx:new1          "bash"                4 seconds ago       Up 3 seconds                  0.0.0.0:32773->80/tcp, 0.0.0.0:32772->443/tcp   romantic_hoover

构建TOMCAT镜像

[root@localhost ~]# mkdir tomcat && cd tomcat
[root@localhost ~]# vi Dockerfile
FROM centos:7
MAINTAINER TOMCATlatest
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
RUN mv /usr/local/jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/jre/bin:/usr/local/java/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
RUN mv /usr/local/apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
#启动容器时,启动服务,用ENTRTPOINT,不使用CMD
#外部CMD 可以覆盖dokerfile中的cmd 而不会覆盖ENTRTPOINT
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"]

创建镜像,运行容器


[root@localhost tomcat]# docker build -t tomcat:new .

[root@localhost tomcat]# docker run -dit -P tomcat:new bash
533b448f5b13846af5b6f52f665070edd899bc7d541ba7cdc3f0165ff5a80b2d
[root@localhost tomcat]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS                                           NAMES
533b448f5b13        tomcat:new          "/usr/local/tomcat/b…"   4 seconds ago       Up 3 seconds                     0.0.0.0:32770->8080/tcp                         exciting_cohen

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值