Dockerfile创建LinuxCentOS6.x Nginx MySQL Tomcat PHP(LNMTP)镜像

Dockerfile创建LinuxCentOS6.x Nginx MySQL Tomcat PHP(LNMTP)镜像,第1张

Dockerfile创建Linux/CentOS6.x Nginx MySQL Tomcat PHP(LNMTP)镜像

Dockerfile

    Dockerfile是实现自动构造镜像的工具,用户可以按照自己的需求定制私有镜像

环境介绍

CentOS6.x镜像

    此为基础镜像,我们通过Dockerfile来创建一个包含自定义软件Nginx/MySQL/Tomcat/PHP的镜像,可以作为自己的私有镜像;下载地址:

https://download.openvz.org/template/precreated/centos-6-x86_64-minimal.tar.gz

LNMTP软件包(源码包)

Nginx:http://nginx.org/download/nginx-1.8.1.tar.gz
MySQL:http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.29.tar.gz
Tomcat:http://apache.opencas.org/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz
JDK(RPM):http://download.oracle.com/otn-pub/java/jdk/8u77-b03/jdk-8u77-linux-x64.rpm
PHP:http://cn2.php.net/distributions/php-5.6.20.tar.bz2

epel扩展源

RHEL7:  http://mirrors.neusoft.edu.cn/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
RHEL6:  http://mirrors.opencas.cn/epel/6/i386/epel-release-6-8.noarch.rpm

基于CentOS7安装Docker

System:        CentOS7.2

Hostname:   dime.huangming.org

IPADDR:       192.168.1.15

    安装Docker的主机我们称为宿主机,而通过镜像(共有或私有p_w_picpaths)创建的虚拟机称为容器,Docker的容器其实就好比是KVM里单独运行的一个或多个虚拟机。


Step1:安装Docker

1、installation docker

[root@dime ~]# yum install docker -y

[root@dime ~]# systemctl start docker.service

[root@dime ~]# systemctl enable docker.service 

 启动Docker服务,并设置开机启动


2、上传一个CentOS镜像到Docker镜像仓库

将centos-6-x86_64-minimal.tar.gz镜像导入

# cat centos-6-x86_64-minimal.tar.gz | docker import - centos-6-x86_64

[root@dime source]# cat centos-6-x86_64-minimal.tar.gz | docker import - centos-6-x86_64
7996b1e6f7eaac8034f414c3c9c232dcefe515262056847f68da51252be50cde

[root@dime source]# docker p_w_picpaths    ;查看docker镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos-6-x86_64     latest              7996b1e6f7ea        41 seconds ago      326.4 MB


Step2:在Docker主机上创建本地密钥

1、创建一个RSA类型,长度为2048的密钥

[root@dime ~]# ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
2e:8b:d0:af:38:83:ba:b2:39:a8:2c:e3:04:48:e7:02 root@dime.huangming.org
The key's randomart p_w_picpath is:
+--[ RSA 2048]----+
|                 |
|                 |
|E. .             |
|+ o              |
|o. .    S        |
|. ..   .         |
|.o. . . .        |
|X.oo o o         |
|%B.oo.o          |
+-----------------+

[root@dime ~]# ls ~/.ssh/
id_rsa  id_rsa.pub  known_hosts


id_rsa为私钥  id_rsa.pub 为公钥,将authorized_keys权限修改为600

[root@dime ~]# cat ~/.ssh/id_rsa.pub > /root/dockerdir/authorized_keys

[root@dime ~]# chmod 600 /root/dockerdir/authorized_keys 


Step3:Dockerfile文件

1、软件包的准备

[root@dime dockerdir]# ls
apache-tomcat-8.0.33.tar.gz     Dockerfile              libiconv-1.14.tar.gz
authorized_keys                 jdk-8u65-linux-x64.rpm  mysql-5.6.29.tar.gz
centos-6-x86_64-minimal.tar.gz  jpegsrc.v6b.tar.gz      nginx-1.8.1.tar.gz
cmake-3.4.3.tar.gz              libgd-2.1.1.tar.bz2     php-5.6.20.tar.bz2


2、编写Dockerfile文件

#######################################################################

FROM centos-6-x86_64

MAINTAINER huangming <741616710@qq.com>

#Install openssh

RUN yum install -y openssh-server

RUN mkdir /root/.ssh

COPY ./authorized_keys /root/.ssh/authorized_keys

RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config

#Install MySQL

RUN yum install -y gcc gcc-c++ make automake

COPY ./cmake-3.4.3.tar.gz .

RUN mkdir -p /usr/local/cmake

RUN tar zxf cmake-3.4.3.tar.gz

RUN cd cmake-3.4.3 && ./bootstrap && make && make install

RUN groupadd mysql; useradd -r -g mysql mysql

RUN mkdir /usr/local/mysql; mkdir /data/mysql/db -p

RUN yum install gcc gcc-c++ ncurses-devel bison bison-devel -y

COPY ./mysql-5.6.29.tar.gz .

RUN tar zxf mysql-5.6.29.tar.gz

RUN cd mysql-5.6.29 && cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/db -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DWITH-MYSQLD-LDFLAGS=-all-static -DWITH-CLIENT-LD-FLAGS=-all-static -DWITH_DEBUG=0 && gmake && gmake install

RUN chown -R root:mysql /usr/local/mysql/ && chown -R mysql:mysql /data/mysql/db/

RUN cd /mysql-5.6.29/scripts && chmod 755 mysql_install_db.sh

RUN /mysql-5.6.29/scripts/mysql_install_db.sh --basedir=/usr/local/mysql --datadir=/data/mysql/db --no-defaults --user=mysql

RUN cd /mysql-5.6.29/support-files/ && cp my-default.cnf /etc/my.cnf && cp mysql.server /etc/init.d/mysqld

RUN chmod 755 /etc/init.d/mysqld && chkconfig mysqld on

RUN echo -e '#!/bin/bash\nexport PATH=$PATH:/usr/local/mysql/bin' >/etc/profile.d/mysql.sh

RUN source /etc/profile

#Install Nginx

RUN yum install zlib pcre pcre-devel openssl openssl-devel -y

RUN useradd -s /sbin/nologin nginx

COPY ./nginx-1.8.1.tar.gz .

RUN mkdir /usr/local/nginx

RUN tar zxf nginx-1.8.1.tar.gz

RUN cd /nginx-1.8.1/ && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-http_sub_module && make && make install

RUN nginx -t

#Install php package dependency

RUN rpm -ivh http://mirrors.opencas.cn/epel/6/i386/epel-release-6-8.noarch.rpm

RUN yum -y install libpng-devel libtool libxslt-devel png bzip2 bzip2-devel libxml2-devel libXpm-devel curl-devel libmcrypt expat libxslt freetype freetype-devel libmcrypt-devel autoconf libpng zlib-devel zlib


COPY ./libiconv-1.14.tar.gz .

RUN mkdir /usr/local/libiconv  && tar zxf libiconv-1.14.tar.gz

RUN cd /libiconv-1.14 && ./configure --prefix=/usr/local/libiconv && make && make install && cd /

COPY ./jpegsrc.v6b.tar.gz .

RUN mkdir /usr/local/jpeg6 && mkdir /usr/local/jpeg6/{bin,lib,include,man} && mkdir /usr/local/jpeg6/man/man1

RUN tar zxf jpegsrc.v6b.tar.gz

RUN cp -r /usr/share/libtool/config/config.sub /jpeg-6b && cp /usr/share/libtool/config/config.guess /jpeg-6b

RUN cd /jpeg-6b && ./configure --prefix=/usr/local/jpeg6 --enable-shared --enable-static && make && make install && cd /

COPY ./libgd-2.1.1.tar.bz2 .

RUN mkdir /usr/local/libgd2 && tar jxf libgd-2.1.1.tar.bz2

RUN cd /libgd-2.1.1 && ./configure --prefix=/usr/local/libgd2 --with-zlib --with-jpeg=/usr/local/jpeg6 --with-png --with-freetype && make && make install && cd /

#Install php

RUN useradd -s /sbin/nologin php-fpm

COPY ./php-5.6.20.tar.bz2 .

RUN tar xjf php-5.6.20.tar.bz2

RUN cd /php-5.6.20 && ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-freetype-dir --with-jpeg-dir=/usr/local/jpeg6 --with-mcrypt --with-gd=/usr/local/libgd2 --with-iconv-dir=/usr/local/libiconv --with-png-dir --with-zlib --with-libxml-dir --with-curl --with-mhash --with-openssl --with-pear --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-exif --enable-sockets --with-gettext --enable-ftp --disable-ipv6 --enable-bcmath --enable-shmop --enable-sysvsem --with-pcre-dir && make && make install

RUN cp /php-5.6.20/php.ini-production /usr/local/php/etc/php.ini

RUN cp /php-5.6.20/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod 755 /etc/init.d/php-fpm

RUN cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

RUN /usr/local/php/sbin/php-fpm -t

#Install JDK

COPY ./jdk-8u65-linux-x64.rpm .

RUN rpm -ivh jdk-8u65-linux-x64.rpm

RUN echo -e 'export JAVA_HOME=/usr/java/latest\nexport PATH=$JAVA_HOME/bin:$PATH' > /etc/profile.d/java.sh

RUN . /etc/profile.d/java.sh

#Install Tomcat

COPY ./apache-tomcat-8.0.33.tar.gz .

RUN tar zxf apache-tomcat-8.0.33.tar.gz -C /usr/local/

RUN cd /usr/local/ && ln -sv apache-tomcat-8.0.33 tomcat

RUN echo -e 'export CATALNA_HOME=/usr/local/tomcat\nexport PATH=$CATALNA_HOME/bin:$PATH' > /etc/profile.d/tomcat.sh

RUN source /etc/profile

RUN cd / && rm -rf jdk-8u65-linux-x64.rpm mysql-5.6.29* jpeg* libgd* php-5.6.20* nginx-1.8.1* libiconv-1.14* cmake-3.4.3* apache-tomcat-8.0.33*


EXPOSE 80 8080 3306 22


3、使用Dockerfile文件创建镜像

# docker build -t centos_lnmpt .    #“.”表示Dockerfile文件,镜像名字为centos_nmpt

[root@dime dockerdir]# docker build -t centos_lnmpt .

......(过程)
Removing intermediate container 9365447c603d
Successfully built 24eb1588647f

4、查看生成的镜像docker p_w_picpaths

[root@dime ~]# docker p_w_picpaths
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos_lnmpt        latest              24eb1588647f        21 minutes ago      6.089 GB
centos_nmpt         latest              d166614dc0f5        About an hour ago   6.089 GB
centos-6-x86_64     latest              7996b1e6f7ea        10 hours ago        326.4 MB

Step4:使用centos_lnmpt镜像运行一个容器

1、开启一个Docker容器docker run -itd -P --name=name p_w_picpath

-i :   表示让容器的标准输入打开

-t :   表示分配一个伪终端

-d :  表示让容器在后台运行

-P :  表示宿主机以随机分配的端口逐一映射容器暴露的端口

-p :  表示自定义宿主机一个或多个端口映射容器暴露的端口

--name : 表示指定容器的名字,容器的名字具有与ID一样的特性


 #docker run -itd -P --name=dnode1 centos_lnmpt bash

[root@dime dockerdir]# docker run -itd -P --name=dnode1 centos_lnmpt bash

3f63e6ba4d83fa7eb1e877edc630e15b64e60e2bc373714fc753c1abb8274b0b



[root@dime dockerdir]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                                                                            NAMES

3f63e6ba4d83        centos_lnmpt        "bash"              2 minutes ago       Up 2 minutes        0.0.0.0:32775->22/tcp, 0.0.0.0:32774->80/tcp, 0.0.0.0:32773->3306/tcp, 0.0.0.0:32772->8080/tcp   dnode1

#docker run -itd -p 33322:22 -p 33380:80 -p 33306:3306 -p 38080:8080 --name=dnode2 centos_lnmpt bash

[root@dime ~]# docker run -itd -p 33322:22 -p 33380:80 -p 33306:3306 -p 38080:8080 --name=dnode2 centos_lnmpt bash

499c9a0bc965677a2ae2a238fc05dc4ec81920661bb1ba12b6933ce0176f3b55



[root@dime ~]# docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                                                                            NAMES

499c9a0bc965        centos_lnmpt        "bash"              4 minutes ago       Up 4 minutes        0.0.0.0:33322->22/tcp, 0.0.0.0:33380->80/tcp, 0.0.0.0:33306->3306/tcp, 0.0.0.0:38080->8080/tcp   dnode2

3f63e6ba4d83        centos_lnmpt        "bash"              About an hour ago   Up About an hour    0.0.0.0:32775->22/tcp, 0.0.0.0:32774->80/tcp, 0.0.0.0:32773->3306/tcp, 0.0.0.0:32772->8080/tcp   dnode1


2、进入已开启的容器

#进入dnode1容器
[root@dime dockerdir]# docker exec -it dnode1 bash
[root@3f63e6ba4d83 /]# ls /usr/local/
apache-tomcat-8.0.33  doc    include  lib64    libiconv  php    src
bin                   etc    jpeg6    libexec  mysql     sbin   tomcat
cmake                 games  lib      libgd2   nginx     share


#启动Nginx
[root@3f63e6ba4d83 /]# nginx 
[root@3f63e6ba4d83 /]# ss -ntl
State       Recv-Q Send-Q           Local Address:Port             Peer Address:Port 
LISTEN      0      128                          *:80                          *:*


#进入dnode2容器
[root@dime ~]# docker exec -it dnode2 bash

#启动Tomcat
[root@a405702fbe84 /]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.


[root@a405702fbe84 /]# ss -ntl
State       Recv-Q Send-Q           Local Address:Port             Peer Address:Port 
LISTEN      0      1             ::ffff:127.0.0.1:8005                       :::*     
LISTEN      0      100                         :::8009                       :::*     
LISTEN      0      100                         :::8080                       :::*


3、测试访问dnode1和dnode2的web服务

在客户机上访问http://192.168.1.15:32774,此为dnode1通过与宿主机端口映射32774-->80,对外提供web服务


在客户机上访问http://192.168.1.15:38080,此为dnode2通过与宿主机端口映射38080-->8080,对外提供服务



Step5:SSH远程管理容器主机

前面在构造Dockerfile时,已经将宿主机的ssh公钥发送到容器镜像里,因此,只需要将容器的sshd服务启动,即可以通过ssh远程登陆容器进行管理

#dnode1
[root@3f63e6ba4d83 /]# service sshd start 
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
[root@3f63e6ba4d83 /]# exit
exit

#dnode2
[root@a405702fbe84 /]# service sshd start 
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]
[root@a405702fbe84 /]# exit
exit


远程登陆dnode1和dnode2

[root@dime ~]# ssh root@192.168.1.15 -p 32775
The authenticity of host '[192.168.1.15]:32775 ([192.168.1.15]:32775)' can't be established.
RSA key fingerprint is 36:34:32:82:25:ba:6b:e4:b9:40:7e:98:a2:6d:de:3c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.15]:32775' (RSA) to the list of known hosts.
[root@3f63e6ba4d83 ~]# 


[root@dime ~]# ssh root@192.168.1.15 -p 33322
The authenticity of host '[192.168.1.15]:33322 ([192.168.1.15]:33322)' can't be established.
RSA key fingerprint is 2e:18:d1:0c:26:cb:d2:2d:37:9a:0d:a7:6d:0d:1e:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.15]:33322' (RSA) to the list of known hosts.
[root@499c9a0bc965 ~]#


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/778766.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-03
下一篇 2022-05-03

发表评论

登录后才能评论

评论列表(0条)

保存