Dockerfile书写介绍及构建ssh镜像、tomcat镜像、nginx镜像

 

 

 

 

 

 

 

 

 ===================================================================================================

构架SSH镜像

创建sshd目录

[root@localhost sshd]# docker images
REPOSITORY              TAG                IMAG ID                     CREATED                         SIZE
centos                              2                    e06c81931dd5        15 minutes ago                  589MB

[root@localhost ~]# mkdir sshd
[root@localhost ~]# cd sshd/
[root@localhost sshd]# vim run.sh

#!/bin/bash
/usr/sbin/sshd -D

创建密钥对

[root@localhost sshd]# ssh-keygen 

[root@localhost sshd]# cat ~/.ssh/id_rsa.pub > ./authorized_keys
[root@localhost sshd]# ls
authorized_keys         run.sh

[root@localhost sshd]# cp /etc/pam.d/sshd ./                  #将sshd文件移到当前目录便于查找

[root@localhost sshd]# vim sshd

#session required pam_loginuid.so             #注释这条

 

编写Dockfile

[root@localhost sshd]# vim Dockerfile

ROM centos:2
MAINTAINER from crushlinux
RUN mkdir -p /var/run/sshd
RUN mkdir -p /root/.ssh
ADD sshd /etc/pam.d/sshd
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ADD authorized_keys /root/.ssh/authorized_keys
ADD run.sh /run.sh
RUN chmod 775 /run.sh
EXPOSE 22
CMD ["/run.sh"]

构建:

[root@localhost sshd]# docker build -t sshd:1 .

Sending build context to Docker daemon 5.632kB
Step 1/12 : FROM centos:2
---> e06c81931dd5
Step 2/12 : MAINTAINER from crushlinux
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 6e769288fa3f
---

此处省略

--------
Successfully built 86902c3345cf
Successfully tagged sshd:1
有两个
Successfully表示构建成功


如果构建报错检查Dockerfile文件和centos(589M)是否有问题

[root@localhost sshd]# docker images               #此时多了一个sshd的镜像
REPOSITORY           TAG                            IMAGE ID                CREATED                   SIZE
sshd                            1                                  86902c3345cf        14 minutes ago           589MB
centos                         2                                   e06c81931dd5       15 minutes ago          589MB

做端口映射:

[root@localhost docker]# docker run -p 2222:22 -itd sshd:1 /bin/bash                      #这里我做映射因为22端口被占用了
26a9e42b2aa72e5bdc1879cb44c74d5948e3b3067d349d8f4d549e1d2a978836
[root@localhost docker]# docker ps -a
CONTAINER ID       IMAGE         COMMAND          CREATED              STATUS                 PORTS                         NAMES
26a9e42b2aa7        sshd:1           "/bin/bash"           6 seconds ago         Up 4 seconds       0.0.0.0:2222->22/tcp      quizzical_cray

查看sshd服务是否开启

[root@localhost docker]# netstat -lnpt | grep 2222
tcp6 0 0 :::2222 :::* LISTEN 34096/docker-proxy

通过ssh连接2222端口

[root@localhost sshd]# ssh 192.168.200.100 -p 2222
The authenticity of host '[192.168.200.100]:2222 ([192.168.200.100]:2222)' can't be established.
RSA key fingerprint is SHA256:3wIiRcP5B1vB5gDSo4XMGJY/8g0VJO1e1tsZUDIMLDc.
RSA key fingerprint is MD5:8a:69:eb:d3:24:04:bd:c6:42:3e:7b:fb:40:15:dc:2d.
Are you sure you want to continue connecting (yes/no)? yes                  #第一次需要确认连接
Warning: Permanently added '[192.168.200.100]:2222' (RSA) to the list of known hosts.

[root@9d017363bc51 ~]#                                                         #进入到了sshd容器内部

 

构建tomcat镜像

 创建tomcat目录

[root@ns2 ~]# mkdir tomcat_centos
[root@ns2 ~]# cd tomcat_centos/

上传tomcat和jdk软件包到目录中

[root@ns2 tomcat_centos]# ls
apache-tomcat-8.5.40.tar.gz jdk-8u191-linux-x64.tar.gz

编写Dockerfile

[root@ns2 tomcat_centos]# cat Dockerfile

FROM centos:1
MAINTAINER from crushlinux <crushlinux@163.com>
#copy jdk and tomcat into images

ADD ./apache-tomcat-8.5.40.tar.gz /root
ADD ./jdk-8u191-linux-x64.tar.gz /root

#set environment variable
ENV JAVA_HOME /root/jdK1.8.0_191
ENV PATH $JAVA_HOME/bin:$PATH

#default entry point which will be run first when the container starts up
ENTRYPOINT /root/apache-tomcat-8.5.40/bin/startup.sh && tail -F /root/apache-tomcat-8.5.40/logs/catalina.out

准备构建

[root@ns2 tomcat_centos]# docker build -t tomcat:centos2 ./

Sending build context to Docker daemon 201.4MB
Step 1/7 : FROM centos:1
---> d8fd9fa26eab
Step 2/7 : MAINTAINER from crushlinux <crushlinux@163.com>
---> Running in dee5d3687f04
Removing intermediate container dee5d3687f04
---> 28c439ce1086
Step 3/7 : ADD ./apache-tomcat-8.5.40.tar.gz /root
---> 4dead1cf3c6b
Step 4/7 : ADD ./jdk-8u191-linux-x64.tar.gz /root
---> b11627dc3131
Step 5/7 : ENV JAVA_HOME /root/jdK1.8.0_191
---> Running in af2a4bcade37
Removing intermediate container af2a4bcade37
---> 7fdf2aa1903b
Step 6/7 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 84af5b099dba
Removing intermediate container 84af5b099dba
---> 0a6171ce29d3
Step 7/7 : ENTRYPOINT /root/apache-tomcat-8.5.40/bin/startup.sh && tail -F /root/apache-tomcat-8.5.40/logs/catalina.out
---> Running in 26400b458bd7
Removing intermediate container 26400b458bd7
---> 1811d971f63d
Successfully built 1811d971f63d
Successfully tagged tomcat:centos2

[root@ns2 tomcat_centos]# docker images
REPOSITORY             TAG                  IMAGE ID                CREATED                         SIZE
tomcat                     centos2                  1811d971f63d          About a minute ago      le+03MB
nginx centos1 4df942e019eb 6 minutes ago 731MB
centos 1 d8fd9fa26eab 4 hours ago 589MB
nginx 1 231d40e811cd 2 weeks ago 126MB
nginx latest 231d40e811cd 2 weeks ago 126MB

开启tomcat容器并与虚拟机8080端口做映射关系

[root@ns2 mynginx]# docker run -d -p 8090:8080 tomcat:centos2
e6bd06027114c365b66a2bfe90a6442343f01caf90584d2f131afaf0e6a46eb2
[root@ns2 mynginx]# docker ps -a
CONTAINER ID            IMAGE                   COMMAND                     CREATED                 STATUS                   PORTS                                NAMES
e6bd06027114           tomcat:centos2        "/bin/sh -c '/root/a…"          3 seconds ago         Up 2 seconds           0.0.0.0:8090->8080/tcp       confident_chaum

访问tomcat首页

 

构建nginx镜像

 使用Dockerfile构建nginx镜像

 

[root@ns2 mynginx]# docker images
REPOSITORY           TAG               IMAGE ID                        CREATED              SIZE
centos                          1                    d8fd9fa26eab               14 minutes ago       589MB
nginx                            1                    231d40e811cd                 2 weeks ago       126MB
nginx                            latest               231d40e811cd               2 weeks ago       126MB

 

创建一个目录,在该目录里编写dockerfile:

[root@docker ~]# mkdir mynginx

[root@docker ~]# cd mynginx/
[root@docker mynginx]# pwd
/root/mynginx
[root@docker mynginx]#

编写Dockerfile:

[root@docker mynginx]# vi Dockerfile

FROM centos:1               #这里centos:1是镜像及标签,from指定基础镜像

RUN ping -c 1 www.baidu.com@163.com>     #MAINTAINER指定作者和作者联系方式 
RUN yum install pcre-devel -y
RUN rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.10.2-1.el7.ngx.x86_64.rpm         #解压从网上下载的rpm包
ADD nginx.conf /etc/nginx/nginx.conf        #添加当下的叫Nginx.conf文件到/etc/nginx/中叫nginx.conf
ADD run.sh /run.sh        #添加run.sh到/下叫run.sh
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
ADD index.html /usr/share/nginx/html/index.html
RUN chmod 775 /run.sh
EXPOSE 80
CMD ["/run.sh"]

编写用于启动Nginx的脚本

[root@ns2 mynginx]# vim run.sh

#!/bin/bash
/usr/sbin/nginx

编写测试页

[root@ns2 mynginx]# vim index.html

www.crushlinux.com

编写nginx主配置文件:

[root@ns2 mynginx]# vim nginx.conf

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
   worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

运行docker命令构建镜像:

[root@ns2 mynginx]# docker build -t nginx:centos1 ./       #镜像名为nginx标签名为centos1

复制代码

Sending build context to Docker daemon 6.144kB
Step 1/12 : FROM centos:1
---> d8fd9fa26eab
Step 2/12 : RUN ping -c 1 www.baidu.com
---> Using cache
---> d1cc08509c3f
Step 3/12 : MAINTAINER from crushlinux <crushlinux@163.fcom>
---> Using cache
---> c3bf21c54ab1
Step 4/12 : RUN yum install pcre-devel -y
---> Using cache
---> b2edd7b42b8d
Step 5/12 : RUN rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.10.2-1.el7.ngx.x86_64.rpm
---> Using cache
---> 19249407f080
Step 6/12 : ADD nginx.conf /etc/nginx/nginx.conf
---> Using cache
---> db6ee0200428
Step 7/12 : ADD run.sh /run.sh
---> Using cache
---> a096548fbca0
Step 8/12 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf
---> Running in ffd4a87b7135
Removing intermediate container ffd4a87b7135
---> ba1e310ff2de
Step 9/12 : ADD index.html /usr/share/nginx/html/index.html
---> ead86137ed2e
Step 10/12 : RUN chmod 775 /run.sh
---> Running in d87c2a9a17db
Removing intermediate container d87c2a9a17db
---> 77a61f5ce6e4
Step 11/12 : EXPOSE 80
---> Running in 2901ec92c10c
Removing intermediate container 2901ec92c10c
---> 9fef15118ef4
Step 12/12 : CMD ["/run.sh"]
---> Running in 4a549ebb9fdd
Removing intermediate container 4a549ebb9fdd
---> 36b152b2ae9a
Successfully built 36b152b2ae9a
Successfully tagged nginx:centos1

 

输出两个Successfully即为构建成功!

复制代码

[root@ns2 mynginx]# docker images
REPOSITORY        TAG                  IMAGE ID                   CREATED                         SIZE
nginx                     centos1                36b152b2ae9a          About a minute ago           731MB
centos                 1                            d8fd9fa26eab            About an hour ago             589MB
nginx                   1                            231d40e811cd          2 weeks ago                     126MB
nginx                    latest                    231d40e811cd           2 weeks ago                   126MB

运行nginx并查看测试页

[root@ns2 mynginx]# docker run -it -d -p 1080:80 nginx:centos1
5e8a2be256f135cbea2f547c3ec42a948e62307eb378edca35023eed1c86b060

posted @ 2019-12-11 13:01  Room、C  阅读(1023)  评论(0编辑  收藏  举报