Construction Nginx + Tomcat + MYSQLDocker actual container

Docker before running a container, you need to mirror the environment as a basis, we can say the mirror is the key Docker containers created, create a method to create the most flexible 3 image based Dockerfile file.

Dockerfile can be regarded as a script program to be construed translated Docker lock, is formed by a collection of commands, each command corresponds to an operation command, the specific command is translated into a Docker under Linux. Users can quickly create custom mirrored by its content.

Bowen outline:
First, build Nginx container
Second, build a Tomcat container
Third, build MySQL container

I. Construction of the container Nginx

Nginx is a lightweight WEB server, but also a good reverse proxy server. Nginx service small footprint, high concurrency, by domestic and foreign users.

1. Download the base image

[root@localhost ~]# docker pull docker.io/lvzhenjiang/centos7
//下载一个创建Nginx镜像的基础centos镜像

2. Establish working directory

[root@localhost ~]# mkdir nginx
[root@localhost ~]# cd nginx/

3. Create a file and write Dockerfile

[root@localhost nginx]# vim Dockerfile
FROM lvzhenjiang/centos7              //设置基础镜像
MAINTAINER lzj                              //维护者信息
RUN yum -y install wget prce-devel net-tools gcc zlib zlib-devel make openssl-devel                 
//安装所需依赖包
RUN wget http://nginx.org/download/nginx-1.14.2.tar.gz
//获取nginx源码包
RUN tar zxf nginx-1.14.2.tar.gz
WORKDIR nginx-1.14.2
//解压之后进入解压后的目录
RUN ./configure --prefix=/usr/local/nginx && make && make install
//编译安装
EXPOSE 80                     //开启80端口
EXPOSE 443                   //开启443端口
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
//修改nginx服务配置文件,以非daemon(守护进程)方式启动
ADD run.sh /run.sh            //上传本地编写的脚本到容器中
RUN chmod 775 /run.sh     //赋予脚本执行权限
CMD ["/run.sh"]                 //启动容器时,执行run.sh脚本

4. Write a script execution content

[root@localhost nginx]# vim run.sh
#!/bin/bash                                          //声明使用的shell
/usr/local/nginx/sbin/nginx                  //使用nginx自带的脚本启动nginx

5. Generate Mirror

[root@localhost nginx]# ls                       
//确保Dockerfile文件和启动脚本在同一目录下
Dockerfile  run.sh
[root@localhost nginx]# docker build -t nginx:v1 .
//使用当前目录下的Dockerfile文件生成新的镜像,名为nginx:v1

6. Start a container for testing

[root@localhost nginx]# docker run -dit -p 12345:80 --name nginx nginx:v1
//使用新生成的镜像创建一个名为nginx的容器,并将容器中的80端口映射到宿主机的12345端口
[root@localhost nginx]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                            NAMES
1c73bb591e84        nginx:v1            "/run.sh"           31 minutes ago      Up 31 minutes       443/tcp, 0.0.0.0:12345->80/tcp   nginx
//确认容器已经正常运行并已经映射端口

Test Results:
Construction Nginx + Tomcat + MYSQLDocker actual container

nginx container to build complete!

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.

1. Download the base image

[root@localhost ~]# docker pull docker.io/lvzhenjiang/centos7
//下载一个创建Nginx镜像的基础centos镜像

2. Create a working directory

Because tomcat need java environment, so to download the JDK packages

[root@localhost ~]# mkdir tomcat
[root@localhost ~]# cd tomcat/

3. Write Dockerfile file

[root@localhost tomcat]# vim Dockerfile 
FROM lvzhenjiang/centos7              //基础镜像
MAINTAINER lzj                               //维护者信息
ADD jdk1.8.0_91 /usr/local/jdk-8u91
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
//设置JDK环境变量
RUN yum -y install wget
RUN wget http://us.mirrors.quenda.co/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz
//获取tomcat软件包
RUN tar zxf apache-tomcat-9.0.27.tar.gz
RUN mv apache-tomcat-9.0.27 /usr/local/tomcat
//解压之后,并将解压后的目录移动到指定的位置
EXPOSE 8080
//开启8080端口
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]                       //启动容器时,执行脚本

4. Start writing the script

[root@localhost tomcat]# vim run.sh 
#!/bin/bash
/usr/local/tomcat/bin/startup.sh            //使用tomcat服务自带的脚本启动服务
tailf /run                  //让启动脚本始终运行

The image file generation using Dockerfile

[root@localhost tomcat]# ls
Dockerfile  jdk1.8.0_91  run.sh
//确保Dockerfile文件中所需的东西全在这个目录下
[root@localhost tomcat]# docker build -t tomcat:v1 .
//使用当前目录的Dockerfile文件生成名为tomcat:v1的镜像

6. Run vessel and verification

[root@localhost tomcat]# docker run -dit -p 12344:8080 --name tomcat tomcat:v1
//使用新生成的进行创建一个名为tomcat的容器,并将容器中的8080端口映射到宿主机的12344端口
[root@localhost tomcat]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                            NAMES
1ffc46084d84        tomcat:v1           "/run.sh"           10 minutes ago      Up 10 minutes       0.0.0.0:12344->8080/tcp          tomcat
//确保容器已经启动,并已经映射端口

Access test:
Construction Nginx + Tomcat + MYSQLDocker actual container

tomcat container to build complete!

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, as the database of choice for small and medium enterprises.

1. Download the base image

[root@localhost ~]# docker pull docker.io/lvzhenjiang/centos6
//下载centos6的基础镜像作为构建MySQL的基础镜像

2. Create a working directory

[root@localhost ~]# mkdir mysql
[root@localhost ~]# cd mysql/

3. Create a file Dockerfile

[root@localhost mysql]# vim Dockerfile
FROM lvzhenjiang/centos6           //基础镜像
MAINTAINER lzj                      //维护者信息
RUN yum -y install mysql mysql-server                  //安装mysql
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';"
//启动MySQL服务,并授权用户
EXPOSE 3306                        //开启3306端口
CMD ["mysqld_safe"]              //使用MySQL自带的启动脚本启动服务

4. creating a mirrored

[root@localhost mysql]# ls
Dockerfile
[root@localhost mysql]# docker build -t mysql:v1 .
//使用当前目录下的Dockerfile文件生成名为mysql:v1的镜像

5. Create a container and test

[root@localhost mysql]# docker run -dit -p 12343:3306 --name=mysql mysql:v1
//使用mysql:v1的镜像生成一个名为mysql的容器,并映射容器中的3306端口都宿主机的12343端口
[root@localhost mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                            NAMES
c356d5b35167        mysql:v1            "mysqld_safe"       4 seconds ago       Up 3 seconds        0.0.0.0:12343->3306/tcp   
//确认容器已经正常运行,并已经启动端口映射

Host test:

[root@localhost ~]# yum -y install mysql                    //首先安装mysql命令
[root@localhost ~]# mysql -u root -h 127.0.0.1 -P 12343 -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 
//访问成功

MySQL container to build complete!

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

Guess you like

Origin blog.51cto.com/14157628/2450991