Dockerfile构建服务镜像

构建ssh镜像

[root@localhost ~]# mkdir sshd
[root@localhost ~]# cd sshd/
[root@localhost sshd]# vim dockerfile

FROM centos:7
MAINTAINER The CentOS Project <cloud-centos>
RUN yum -y update
RUN yum -y install openssh* net-tools lsof telnet passwd
RUN echo '123456' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

[root@localhost sshd]# docker build -t sshd:new .  生成镜像
[root@localhost sshd]# docker run -d -P sshd:new                 //启动容器4f584d2acbdbff268583faaa4b4d3be5fd03ad8300afd2d803b1f25956381e29

[root@localhost sshd]# ssh localhost -p 32770                      //远程登录
The authenticity of host '[localhost]:32770 ([::1]:32770)' can't be established.
RSA key fingerprint is SHA256:l1wGwfYmIXS1DwIW60oxuBtwHy6iEVZ8R64sSWBja0o.
RSA key fingerprint is MD5:9a:93:e6:6a:f0:eb:06:06:ce:47:21:00:75:0f:24:e3.Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '[localhost]:32770' (RSA) to the list of known hosts.root@localhost's password:
 [root@4f584d2acbdb ~]#             //远程登录成功

构建systemctl镜像

[root@localhost ~]# mkdir systemctl
[root@localhost ~]# cd systemctl/

[root@localhost systemctl]# vim Dockerfile
FROM sshd:new
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/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 images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
systemd             new                 d544f11cf063        17 seconds ago      540MB
nginx               new                 549da65f308d        2 hours ago         476MB
sshd                new                 d587e3500668        4 hours ago         540MB
centos              7                   8652b9f0cb4c        13 days ago         204MB

[root@localhost systemctl]# docker run --privileged -ti -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd:new /sbin/init &
[1] 99192
[root@localhost systemctl]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS                   NAMES
99e5ca52d4f9        systemd:new         "/sbin/init"             14 seconds ago      Up 13 seconds                22/tcp                  gracious_sanderson
4644b27b3fdd        f57557405f9d        "/bin/sh -c '(cd /li…"   7 minutes ago       Exited (126) 7 minutes ago                           infallible_northcutt
a086429864f0        nginx:new           "/run.sh"                2 hours ago         Up 2 hours                   0.0.0.0:32768->80/tcp   gifted_kapitsa
4f584d2acbdb        sshd:new            "/usr/sbin/sshd -D"      4 hours ago         Up 2 hours                   0.0.0.0:32769->22/tcp   brave_brown
[root@localhost systemctl]# docker exec -it 99e5ca52d4f9 bash


[root@99e5ca52d4f9 /]# 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)
           root@99e5ca52d4f9 /]# systemctl start sshd
[root@99e5ca52d4f9 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-11-27 05:59:13 UTC; 2s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 56 (sshd)
   CGroup: /docker/99e5ca52d4f9e1212baee52dfdf05fe953d8c80bf98c95f595c01abf4bbc56e9/system.slice/sshd.service
           └─56 /usr/sbin/sshd -D

构建Nginx镜像

在这里插入图片描述

[root@localhost ~]# mkdir nginx[root@localhost ~]# cd nginx/
[root@localhost nginx]# vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image
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.tar.gz /usr/local/src
WORKDIR /usr/local/srcW
WORKDIR nginx-1.12.2
RUN ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
RUN chmod 755 /run.sh
CMD ["/run.sh"]

[root@localhost nginx]# cd
[root@localhost ~]# vim run.sh 
#!/bin/bash/usr/local/nginx/sbin/nginx

在这里插入图片描述

[root@localhost ~]# docker build -t nginx:new .[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               new                 0cb903d54b6e        8 minutes ago       537MB
centos              7                   8652b9f0cb4c        13 days ago         204MB

[root@localhost ~]# docker run -d -P nginx:new
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                           NAMES
7b56d594813c        nginx:new           "/run.sh"           8 minutes ago       Up 8 minutes        0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   determined_wu

在这里插入图片描述

构建TOMCAT镜像

[root@localhost ~]# mkdir /opt/tomcat
[root@localhost ~]# cd /opt/tomcat/
拷贝apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz到/opt/tomcat下

在这里插入图片描述

[root@localhost tomcat]# vim Dockerfile
[root@localhost tomcat]# rm Docker
FROM centos:7
MAINTAINER this is tomcat image
ADD jdk-8u91-linux-x64.tar.gz /usr/local
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JRE_BIN /uar/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/lib/charsets.jar
ADD apache-tomcat-8.5.16.tar.gz /usr/local
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat8
EXPOSE 8080
#CMD ["/usr/local/tomcat8/bin/catalina.sh","run"]
ENTRYPOINT ["/usr/local/tomcat8/bin/catalina.sh","run"]

[root@localhost tomcat]# docker build -t tomcat:new .
[root@localhost tomcat]# docker run -d --name tomcat09 -p 1218:8080 tomcat:new
[root@localhost tomcat]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d858497636d6        tomcat:new          "/usr/local/tomcat8/…"   5 minutes ago       Up 5 minutes        0.0.0.0:1218->8080/tcp   tomcat09

检验在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页