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

构架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 .632kB
Step / : FROM centos:
---> e06c81931dd5
Step / : 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:
MAINTAINER from crushlinux <crushlinux@.com>
#copy jdk and tomcat into images ADD ./apache-tomcat-8.5..tar.gz /root
ADD ./jdk-8u191-linux-x64.tar.gz /root #set environment variable
ENV JAVA_HOME /root/jdK1..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./bin/startup.sh && tail -F /root/apache-tomcat-8.5./logs/catalina.out

准备构建

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

Sending build context to Docker daemon .4MB
Step / : FROM centos:
---> d8fd9fa26eab
Step / : MAINTAINER from crushlinux <crushlinux@.com>
---> Running in dee5d3687f04
Removing intermediate container dee5d3687f04
---> 28c439ce1086
Step / : ADD ./apache-tomcat-8.5..tar.gz /root
---> 4dead1cf3c6b
Step / : ADD ./jdk-8u191-linux-x64.tar.gz /root
---> b11627dc3131
Step / : ENV JAVA_HOME /root/jdK1..0_191
---> Running in af2a4bcade37
Removing intermediate container af2a4bcade37
---> 7fdf2aa1903b
Step / : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 84af5b099dba
Removing intermediate container 84af5b099dba
---> 0a6171ce29d3
Step / : ENTRYPOINT /root/apache-tomcat-8.5./bin/startup.sh && tail -F /root/apache-tomcat-8.5./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

Dockerfile书写介绍及构建ssh镜像、tomcat镜像、nginx镜像的更多相关文章

  1. Dockerfile 自动制作 Docker 镜像(三)—— 镜像的分层与 Dockerfile 的优化

    Dockerfile 自动制作 Docker 镜像(三)-- 镜像的分层与 Dockerfile 的优化 前言 a. 本文主要为 Docker的视频教程 笔记. b. 环境为 CentOS 7.0 云 ...

  2. docker学习之路-nginx镜像(翻译)

    本篇来自https://hub.docker.com/_/nginx/?tab=description 它是docker hub上nginx的官方网站,上面有关于nginx的使用描述等.从这里你可以找 ...

  3. Docker 拉取Nginx镜像 和运行

    Docker 镜像拉取 docker pull [OPTIONS] NAME[:TAG|@DIGEST] 镜像拉取命令 OPTIONS说明: -a :拉取所有 tagged 镜像 --disable- ...

  4. Docker在Centos下使用Dockerfile构建远程Tomcat和Jenkins镜像

    镜像构建准备环境原料 构建CentOS Docker tomcat镜像 Dockerfile文件内容: FROM centos:latest MAINTAINER boonya <boonya@ ...

  5. 构建自己的Tomcat镜像

    在很多情况下,我们会不满足于官方提供的Tomcat镜像.比如官方镜像的时区为UTC时间,并不是北京时间:再比如在特定硬件环境下,jdk的随机数生成器初始化过慢问题.此时,我们就会考虑构建自己的Tomc ...

  6. Docker镜像实战(ssh、systemctl、nginx、tomcat、mysql)

    Docker镜像实战 1.构建ssh镜像 2.构建systemctl 镜像 3.构建nginx镜像 4.构建tomcat镜像 5.构建mysql镜像 1.构建ssh镜像: 创建镜像目录 mkdir / ...

  7. Docker使用Dockerfile创建支持ssh服务自启动的容器镜像

    原文链接:Docker使用Dockerfile创建支持ssh服务自启动的容器镜像 1. 首先创建一个Dockerfile文件.文件内容例如以下 # 选择一个已有的os镜像作为基础 FROM cento ...

  8. 使用dockerfile构建nginx镜像

    使用dockerfile构建nginx镜像 docker构建镜像的方法:   commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方 ...

  9. Dockerfile构建nginx镜像

    Dockerfile构建nginx镜像 [root@bogon ~]# mkdir /opt/docker-file [root@bogon ~]# cd /opt/docker-file/ [roo ...

随机推荐

  1. IntelliJ IDEA提示URI is not registered几种解决方法

    IntelliJ IDEA提示URI is not registered几种解决方法使用IntelliJ IDEA (以下简称IDEA)导入项目或是在maven生成  archetype时候,如果提示 ...

  2. Ueditor1.4.3.3 asp UTF-8版文件缺失修改方法

    前几天在做一个项目中需要用富文本编辑器,最终选定用Ueditor编辑器,从官网下载源码(http://ueditor.baidu.com/website/download.html),按照文档创建de ...

  3. CSS隐藏元素的五种方法

    1.opacity:0 2.visibility:hidden 3.diaplay:none 4.position:absolute display display属性依照词义真正隐藏元素.将disp ...

  4. SendMessage模拟按键所需的虚拟键码

    Virtual-Key Codes The following table shows the symbolic constant names, hexadecimal values, and mou ...

  5. Educational Codeforces Round 65 (Rated for Div. 2)B. Lost Numbers(交互)

    This is an interactive problem. Remember to flush your output while communicating with the testing p ...

  6. centos7搭建天兔

    如果新系统尚未安装工具pip,可通过以下三步快速安装pip              1.  yum -y install epel-release               2.  yum -y ...

  7. [AT3867] Digit Sum 2

    给出N,求小于等于N的正整数中用十进制表示各数位数字之和的最大值. Solution 如果是X999的形式,那么就是自己 否则,就是(X-1)999 #include <bits/stdc++. ...

  8. Oracle忘记用户名和密码

    Microsoft Windows [版本 10.0.16299.192](c) 2017 Microsoft Corporation.保留所有权利. C:\WINDOWS\system32>e ...

  9. C语言链表的中间结点

    给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5]输出:此列表中的结点 3 (序列化形式:[3,4, ...

  10. 自定义Nginx返回页面

    1.403返回页面 #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log noti ...