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

构架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. IDEA 和 Webstorm JAR包方式破解

    IDEA 和 Webstorm JAR包方式破解 IDEA 我是用的IDEA版本是 18.3,更新版本的IDEA很多激活码都不能用了,或者不好激活了. 首先下载相应的 软件版本 和破解 jar包. 下 ...

  2. adworld MISC002 | Linux的挂载文件系统的运用

    EXT3是第三代扩展文件系统(英语:Third extended filesystem,缩写为ext3),是一个日志文件系统,常用于Linux操作系统. Plan 1: 直接将附件使用mount命令挂 ...

  3. laravel框架使用阿里短信接入

    EG: accessKeyid和accessKeySecret还有模板ID.签名名称这几项必要参数自己去阿里云获取一.下载SDK和demo 下载并解压后 在laravel框架的app目录下创建libs ...

  4. codeforces刷题记录

    Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) C. Magic Grid 这种题直接构造 数n是2的n次方的倍数的时候可以这样划分数 ...

  5. vue.js中使用离线检测

    Html5在window.navigator对象上添加了一个属性onLine 返回布尔值 true表示在线.同时新增了两个事件: window.addEventListener('online', f ...

  6. [SDOI2013] 直径 - 树形dp

    对于给定的一棵树,其直径的长度是多少,以及有多少条边满足所有的直径都经过该边. Solution 有点意思 先随便求一条直径(两次DFS即可),不妨设为 \(s,t\),我们知道要求的这些边一定都在这 ...

  7. MariaDB 安装配置记录

    1.集群搭建记录 iptables --append INPUT --protocol tcp \ --source 192.168.126.129 --jump ACCEPT iptables -- ...

  8. vue源码的入口(四)

    我们之前提到过 Vue.js 构建过程,在 web 应用下,我们来分析 Runtime + Compiler 构建出来的 Vue.js,它的入口是 src/platforms/web/entry-ru ...

  9. JavaScript变量的传递方式

    废话不多说,直接上案例: [案例] 1.访问变量 按值: function addM(num) { num += 5; return num; } var cnt = 10; var result = ...

  10. 二分-A - Cable master

    A - Cable master Inhabitants of the Wonderland have decided to hold a regional programming contest. ...