Docker containers set up Nginx, Tomcat, MySQL

 博文大纲:
  • I. Construction of the container Nginx
  • Second, the container constructed Tomcat
  • Third, build MySQL container

Note: Be sure to configure the host can be connected to Internet, because in the process of building container, a lot of resources need to go to the Internet to find.

I. Construction of the container Nginx

Nginx is a lightweight web server, is also an excellent reverse proxy server. Nginx service small footprint, high concurrency, the following operations are specifically constructed Nginx containers:

[root@localhost ~]# docker pull centos      #下载centos 7的基础镜像,该镜像也将会被后面构建的Tomcat和MySQL容器所使用。
[root@localhost ~]# mkdir nginx       #创建工作目录
[root@localhost ~]# cd nginx/
[root@localhost nginx]# vim Dockerfile          #编写dockerfile文件,用来实现Nginx安装过程
#写入以下内容
FROM centos            #设置基础镜像
MAINTAINER the centos project     #维护该镜像的用户信息
RUN yum -y install wget proc-devel net-tools gcc zlib zlib-devel make openssl-devel  #安装相关依赖包
RUN wget http://nginx.org/download/nginx-1.9.7.tar.gz     #下载Nginx源码包
RUN tar zxf nginx-1.9.7.tar.gz     #解压下载的源码包
WORKDIR nginx-1.9.7     #指定容器中的工作路径
RUN ./configure --prefix=/usr/local/nginx && make && make install     #配置及编译安装
#开启容器的80/443端口
EXPOSE 80     
EXPOSE 443
RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf      #修改配置文件,以非daemon方式启动
ADD run.sh /run.sh         #上传运行脚本
RUN chmod 775 /run.sh    #赋予脚本权限
CMD ["/run.sh"]      #启动容器时执行脚本,编辑完成后,保存退出即可
[root@localhost nginx]# vim run.sh      #编辑运行脚本,写入以下内容

#!/bin/bash
/usr/local/nginx/sbin/nginx
[root@localhost nginx]# ls           #确定当前目录下有以下文件
Dockerfile  run.sh
[root@localhost nginx]# docker build -t nginx:ljz .       #生成镜像,千万不要忽略命令末尾的点“.”号,否则会报错
                      .............................#省略部分内容
Successfully built cd6ac93f3680              #出现这行提示信息,则表示创建成功
[root@localhost nginx]# docker run -d -P nginx:ljz      #启动该容器,“-d”表示持久化运行,“-P”表示将容器的端口映射到宿主机
895c19da98f3256acb20939dcc7abb4d26273287ddfc0810efc0940a55d04c10
[root@localhost nginx]# docker ps -a    #查看容器,其中32769、32768就是容器映射到宿主机的端口
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                           NAMES
895c19da98f3        nginx:ljz           "/run.sh"           6 seconds ago       Up 5 seconds        0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp   nervous_brahmagupta

Access to the container for testing whether Nginx running success! Follows (32769 to access the host port, access port 80 is equivalent container):
Docker containers set up Nginx, Tomcat, MySQL

Second, the container constructed Tomcat

Tomcat is a free open source lightweight web servers, commonly used in small and medium enterprises and concurrency is not high occasions, is the preferred development and debugging JSP program. Dockerfile file using the following ways to create docker mirror with Tomcat services.

Preparation: Download JDK source package, and place the Tomcat work directory of Linux host.

[root@localhost ~]# mkdir tomcat                 #创建Tomcat的工作目录
[root@localhost ~]# cd tomcat/              #切换至Tomcat目录
[root@localhost tomcat]# rz            #我这里将本地的JDK源码包上传了上来(我这里使用的是xshell连接的)
[root@localhost tomcat]# ls                    #查看
jdk-8u91-linux-x64.tar.gz
[root@localhost tomcat]# tar zxf jdk-8u91-linux-x64.tar.gz     #解包
[root@localhost tomcat]# vim Dockerfile           #编写dockerfile文件

FROM centos     #基础镜像centos
MAINTAINER The centos project   [email protected]   #维护该镜像的用户信息
ADD jdk1.8.0_91 /usr/local/jdk-8u91             #将本地的JDK文件上传至容器
#以下是设置JDK环境变量
ENV JAVA_HOME /usr/local/jdk-8u91        
ENV JAVA_BIN /usr/local/jdk-8u91/bin
ENV JRE_HOME /usr/local/jdk-8u91/jre
ENV PATH $PATH:/usr/local/jdk-8u91/bin:/usr/local/jdk-8u91/jre/bin
ENV CLASSPATH /usr/local/jdk-8u91/jre/bin:/usr/local/jdk-8u91/lib:/usr/local/jdk-8u91/jre/lib/charsets.jar
RUN yum -y install wget     #安装wget工具
RUN wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.38/bin/apache-tomcat-8.5.38.tar.gz
#下载Tomcat源码包
RUN tar zxf apache-tomcat-8.5.38.tar.gz    #解压Tomcat源码包
RUN mv apache-tomcat-8.5.38 /usr/local/tomcat         #移至安装目录
EXPOSE 8080     #开启8080端口
ADD run.sh /run.sh          #添加运行脚本
RUN chmod 775 /run.sh        #赋予脚本执行权限
CMD ["/run.sh"]           #运行脚本,编写完毕后,保存退出即可。
[root@localhost tomcat]# vim run.sh          #编写执行脚本内容

#!/bin/bash
/usr/local/tomcat/bin/startup.sh
tailf /run
[root@localhost tomcat]# ls       #确保当前目录下有以下文件
Dockerfile  jdk1.8.0_91  jdk-8u91-linux-x64.tar.gz  run.sh
[root@localhost tomcat]# docker build -t tomcat:ljz .        #生成镜像,千万不要忽略命令末尾的点“.”号,否则会报错
                      .............................#省略部分内容
Successfully built  94c59c9239ec         #出现这行提示信息,则表示创建成功
[root@localhost tomcat]# docker run -d --name tomcat -p 8080:8080 tomcat:ljz
#运行刚刚创建的Tomcat容器,并指定容器的名字为Tomcat,映射到宿主机的8080端口。
5d8a5714166ef63208511bb1123c5568d0562b1b3734db4eec2aed733d8a733d
[root@localhost tomcat]# docker ps -a | grep tomcat        #查看Tomcat容器的状态是否正常
5d8a5714166e        tomcat:ljz          "/run.sh"           20 seconds ago      Up 19 seconds       0.0.0.0:8080->8080/tcp 

Client Access Linux host the 8080 port, test whether access to the Tomcat web container services provided, as follows:
Docker containers set up Nginx, Tomcat, MySQL
see above page, then Tomcat container constructed successfully.

Here briefly about the difference between Tomcat and apache: they are Aache project for handling HTTP service of open source development. Both are free, it can also be run as a standalone web server. Apache is the C language, the main analytical static text, high concurrency, focusing on the HTTP server, Tomcat is a JSP server line with JavaEES of java development, part of the Apache expansion, mainly used to resolve JSP / Servlet, focusing on Servlet engine

Third, build MySQL container

MySQL is currently most popular relational databases, SQL language used is the most common standardized language used to access databases, MySQL has a small size, high speed and low cost advantages of small and medium enterprises preferred database.

Construction of the vessel began to MySQL:

[root@localhost ~]# docker pull docker.io/guyton/centos6      #下载centos 6作为基础镜像
[root@localhost ~]# mkdir mysql        #创建工作目录
[root@localhost ~]# cd mysql/        #切入工作目录
[root@localhost mysql]# vim Dockerfile          #编辑dockerfile文件

FROM guyton/centos6
MAINTAINER the centos project-mysql
RUN yum -y install mysql mysql-server
RUN /etc/init.d/mysqld start && \
mysql -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"&&\
mysql -e "grant all privileges on *.* to 'root'@'localhost' identified by '123456';"
EXPOSE 3306
CMD ["mysqld_safe"]
[root@localhost mysql]# docker build -t mysql:ljz .     #生成镜像,千万不要忽略命令末尾的点“.”号,否则会报错
                      .............................#省略部分内容
    Successfully built   30414dc7bc02         #出现这行提示信息,则表示创建成功
[root@localhost ~]# docker run -d --name mysql -P mysql:ljz   #运行容器,并定义容器名称为mysql
ea1a8aa2d1f18e0d06c4bdf47a6b743f3763247f13c6335b3fc7b785baf7332f
[root@localhost ~]# docker ps -a | grep mysql        #确认容器处于运行状态
ea1a8aa2d1f1        mysql:ljz           "mysqld_safe"            41 seconds ago      Up 40 seconds                 0.0.0.0:32770->3306/tcp                         mysql
[root@localhost ~]# mysql -h 192.168.1.1 -u root -P 32770 -p123456
#对容器中的数据库进行访问测试,若无法使用mysql命令,请自行执行“yum -y install mysql”进行安装
Welcome to the MariaDB monitor.  Commands end with ; or \g.
                       .............................#省略部分内容        

MySQL [(none)]>           #命令提示符发生变化,登录到了容器中的数据库
MySQL [(none)]> show databases;               #查看数据库的数据
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

MySQL [(none)]> exit              #退出
Bye

-------- end of this article so far, thanks for reading

Guess you like

Origin blog.51cto.com/14223370/2455535