通过Dockerfile部署TomcatApacheNginxMySQL服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过Dockerfile部署TomcatApacheNginxMySQL服务相关的知识,希望对你有一定的参考价值。

博文目录

一、编写Dockerfile部署Tomcat服务

二、编写Dockerfile部署Apache服务

1、编写Dockerfile文件

2、docker宿主机或者客户端访问httpd01容器中的apache服务

3、登录httpd01容器,查看访问日志

三、编写Dockerfile部署nginx服务

1、编写Dockerfile文件

2、使用docker宿主机或者客户端访问nginx01容器中的nginx服务

3、登录nginx01容器,查看访问日志

四、编写Dockerfile部署mysql服务

一、编写Dockerfile部署Tomcat服务

[root@centos01 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf   <!--开启路由功能-->
[root@centos01 ~]# sysctl -p   <!--刷新配置-->
net.ipv4.ip_forward = 1
[root@centos01 ~]# mkdir tomcat           <!--创建tomcat目录--> 
<!--拷贝tomcat和jdk压缩包到tomcat目录--> 
[root@centos01 ~]# cp /mnt/apache-tomcat-7.0.54.tar.gz ./tomcat/   
[root@centos01 ~]# cp /mnt/jdk-7u65-linux-x64.gz ./tomcat/ 
<!--解压缩tomcat和jdk(测试用,一会还会将其删除)--> 
[root@centos01 tomcat]# tar zxvf apache-tomcat-7.0.54.tar.gz  
[root@centos01 tomcat]# tar zxvf jdk-7u65-linux-x64.gz
[root@centos01 tomcat]# vim Dockerfile   <!--编写Dockerfile文件--> 
<!--基础镜像centos6.7系统--> 
FROM    hub.c.163.com/public/centos:6.7-tools
<!--作者联系方式--> 
MAINTAINER      18232013151@163.com 
<!--解压缩tomcat和解压缩JDK--> 
ADD     apache-tomcat-7.0.54.tar.gz /usr/src/ 
ADD     jdk-7u65-linux-x64.gz /usr/src/ 
<!--设置环境变量--> 
ENV     JAVA_HOME /usr/src/jdk1.7.0_65
ENV     PATH $JAVA_HOME/bin/$PATH
EXPOSE  8080     <!--监听8080端口--> 
<!--启动tomcat服务,保持docker容器运行--> 
ENTRYPOINT      /usr/src/apache-tomcat-7.0.54/bin/startup.sh && tailf /usr/src/apache-tomcat-7.0.54/logs/catalina.out 
[root@centos01 tomcat]# docker images |grep hub<!--验证docker宿主机是否有基础镜像--> 
hub.c.163.com/public/centos   6.7-tools           b2ab0ed558bb        3 years ago         602 MB
[root@centos01 tomcat]# docker build -t tomcat:v1.0 .   
        <!--通过dockerfile生成镜像,镜像的名字是tomcat:v1.0-->
Sending build context to Docker daemon 459.7 MB
Step 1/8 : FROM hub.c.163.com/public/centos:6.7-tools
……………<!--此处省略部分内容-->
Removing intermediate container aa619596db96
Successfully built 66675674563d
[root@centos01 tomcat]# docker images <!--查看docker宿主机是否生成tomcat:v1.0镜像-->
REPOSITORY                    TAG                 IMAGE ID            CREATED              SIZE
tomcat                        v1.0                66675674563d        About a minute ago   908 MB
hub.c.163.com/public/centos   6.7-tools           b2ab0ed558bb        3 years ago          602 MB
[root@centos01 tomcat]# docker run -it -d --name tomcat01 -p 192.168.100.10:8080:8080/tcp tomcat:v1.0   
   <!--通过tomcat:v1.0镜像运行容器并发布服务,容器名字为tomcat01-->
74dd48e73260de092f882ac76e8a052f2fb5469b67e01fc3fcf653e31dd0462d
[root@centos01 tomcat]# docker ps  <!--查看正在运行的容器-->
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                   NAMES
74dd48e73260        tomcat:v1.0         "/bin/sh -c ‘/usr/..."   21 seconds ago      Up 20 seconds       22/tcp, 192.168.100.10:8080->8080/tcp   tomcat01

通过客户端或者docker宿主机访问tomcat01容器中的tomcat服务
技术图片

二、编写Dockerfile部署Apache服务

1、编写Dockerfile文件

[root@centos01 ~]# docker images         <!--查看docker宿主机镜像-->
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
hub.c.163.com/public/centos   6.7-tools           b2ab0ed558bb        3 years ago         602 MB
[root@centos01 ~]# mkdir apache   <!--创建apache目录-->
[root@centos01 ~]# cd apache/
[root@centos01 apache]# vim Dockerfile        <!--创建Dockerfile-->
FROM hub.c.163.com/public/centos:6.7-tools
MAINTAINER lyx.163.com
RUN rm -rf /etc/yum.repos.d/CentOS-*
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum clean all
RUN yum -y install httpd
RUN echo "www.dockerfile.com" > /var/www/html/index.html
CMD ["httpd","-DFOREGROUND"]
[root@centos01 apache]# docker build -t httpd:v1.0 . <!--通过dockerfile生成apache镜像-->
Sending build context to Docker daemon 2.048 kB
Step 1/8 : FROM hub.c.163.com/public/centos:6.7-tools
 ---> b2ab0ed558bb
Step 2/8 : MAINTAINER lyx.163.com
……………… <!--此处省略部分内容-->
Removing intermediate container cef73e68e7b1
Step 8/8 : CMD httpd -DFOREGROUND
 ---> Running in de7c11e41b1e
 ---> 33bf9e8f5992
Removing intermediate container de7c11e41b1e
Successfully built 33bf9e8f5992
[root@centos01 apache]# docker images     <!--查看docker宿主机镜像-->
REPOSITORY                    TAG                 IMAGE ID            CREATED              SIZE
httpd                         v1.0                33bf9e8f5992        About a minute ago   769 MB
hub.c.163.com/public/centos   6.7-tools           b2ab0ed558bb        3 years ago          602 MB
[root@centos01 apache]# docker run -it -d --name httpd01 -p 192.168.100.10:80:80/tcp httpd:v1.0         
     <!--通过httpd镜像运行一个容器,容器名字为httpd01,并发布容器中的apache服务-->
33de1fc52130b9109b7746568c85cf1a12d3067baa6b42135cb389040315f20c
[root@centos01 apache]# docker ps         <!--查看运行的容器-->
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                               NAMES
33de1fc52130        httpd:v1.0          "httpd -DFOREGROUND"   17 seconds ago      Up 16 seconds       22/tcp, 192.168.100.10:80->80/tcp   httpd01

2、docker宿主机或者客户端访问httpd01容器中的apache服务

[root@centos01 apache]# curl http://192.168.100.10   
         <!--docker宿主机访问httpd01容器中的apache服务-->
www.dockerfile.com

技术图片

3、登录httpd01容器,查看访问日志

[root@centos01 apache]# docker exec -it httpd01 /bin/bash      <!--登录httpd01容器-->
[root@33de1fc52130 /]# cat /var/log/httpd/access_log        <!--查看访问日志-->
192.168.100.10 - - [16/May/2020:17:24:30 +0800] "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0"
192.168.100.252 - - [16/May/2020:17:24:51 +0800] "GET / HTTP/1.1" 200 19 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0

三、编写Dockerfile部署Nginx服务

1、编写Dockerfile文件

[root@centos01 ~]# mkdir nginx   <!--创建nginx目录-->
[root@centos01 ~]# cp /mnt/nginx-1.6.0.tar.gz ./nginx/ <!--复制nginx压缩包到nginx目录-->
[root@centos01 ~]# cd nginx/
[root@centos01 nginx]# vim Dockerfile       <!--创建Dockerfile-->
FROM hub.c.163.com/public/centos:6.7-tools
MAINTAINER lyx.163.com
RUN rm -rf /etc/yum.repos.d/CentOS-*
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum clean all
RUN yum -y install epel-release pcre-devel zlib-devel gcc
ADD nginx-1.6.0.tar.gz /usr/src/
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/src/nginx-1.6.0
RUN ./configure --prefix=/usr/local/nginx --user=nginx --with-http_stub_status_module && make && make install
RUN echo "www.dockerfile.nginx.com" > /usr/local/nginx/html/index.html
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
CMD ["nginx"]
[root@centos01 nginx]# docker build -t nginx:v1.0 .  <!--使用dockerfile生成nginx镜像-->
Sending build context to Docker daemon 806.4 kB
Step 1/15 : FROM hub.c.163.com/public/centos:6.7-tools
 ---> b2ab0ed558bb
Step 2/15 : MAINTAINER lyx.163.com
……………… <!--此处省略部分内容-->
Removing intermediate container f4c88ae2748a
Step 15/15 : CMD nginx
 ---> Running in a8a00e8ae169
 ---> 03f7a7450ab9
Removing intermediate container a8a00e8ae169
Successfully built 03f7a7450ab9
[root@centos01 nginx]# docker images |grep nginx        <!--docker宿主机查看nginx镜像-->
nginx                         v1.0                03f7a7450ab9        About a minute ago   929 MB
[root@centos01 nginx]# docker run -it -d --name nginx01 -p 192.168.100.10:8080:80/tcp nginx:v1.0          
   <!--基于nginx镜像运行一个容器,并发布容器中的nginx服务-->
8f249f41662f991cc8b4b457e80a90404a0d5140994aeb10cd62d37a5ab10a21
[root@centos01 nginx]# docker ps        <!--查看运行的容器-->
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                 NAMES
8f249f41662f        nginx:v1.0          "nginx"                14 seconds ago      Up 13 seconds       22/tcp, 192.168.100.10:8080->80/tcp   nginx01
33de1fc52130        httpd:v1.0          "httpd -DFOREGROUND"   10 minutes ago      Up 10 minutes       22/tcp, 192.168.100.10:80->80/tcp     httpd01

2、使用docker宿主机或者客户端访问nginx01容器中的nginx服务

[root@centos01 nginx]# curl http://192.168.100.10:8080        
           <!--访问Nginx01容器中的nginx服务-->
www.dockerfile.nginx.com

技术图片

3、登录nginx01容器,查看访问日志

[root@centos01 nginx]# docker exec -it nginx01 /bin/bash         <!--登录Nginx01容器-->
[root@8f249f41662f nginx-1.6.0]# cat /usr/local/nginx/logs/access.log         <!--查看访问日志-->
192.168.100.10 - - [16/May/2020:09:34:33 +0000] "GET / HTTP/1.1" 200 25 "-" "curl/7.29.0"
192.168.100.252 - - [16/May/2020:09:35:21 +0000] "GET / HTTP/1.1" 200 25 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0"

四、编写Dockerfile部署MySQL服务

[root@centos01 ~]# mkdir mysql       <!--创建mysql目录-->
[root@centos01 ~]# cp mysql-5.5.22.tar.gz cmake-2.8.6.tar.gz ./mysql   
         <!--复制mysql和cmake压缩包到mysql目录-->
[root@centos01 ~]# cd mysql/
[root@centos01 mysql]# vim Dockerfile        <!--创建Dockerfile-->
FROM hub.c.163.com/public/centos:6.7-tools
MAINTAINER lyx.163.com
RUN rm -rf /etc/yum.repos.d/CentOS-*
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-6.repo
RUN yum clean all
RUN yum -y install ncurses-devel libstdc++-devel gcc-c++ 
RUN groupadd mysql
RUN useradd -M -s /sbin/nologin -g mysql mysql
ADD cmake-2.8.6.tar.gz /usr/src
ADD mysql-5.5.22.tar.gz /usr/src
WORKDIR /usr/src/cmake-2.8.6
RUN ./configure && gmake && gmake install 
WORKDIR /usr/src/mysql-5.5.22
RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make install 
RUN cp support-files/my-medium.cnf /etc/my.cnf
RUN chown -R mysql:mysql /usr/local/mysql/
RUN cp support-files/mysql.server /etc/init.d/mysqld
RUN chmod +x /etc/init.d/mysqld
RUN chkconfig --add mysqld
RUN chkconfig --level 35 mysqld on
RUN ln -s /usr/local/mysql/bin/* /usr/local/bin/
RUN  /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 
ADD mysqld.sh /usr/local/mysql
RUN chmod +x /usr/local/mysql/mysqld.sh
EXPOSE 3306
CMD "/usr/local/mysql/mysqld.sh" && tail -f /dev/null
[root@centos01 mysql]# vim mysqld.sh    <!--创建启动mysql脚本-->
#!/bin/bash
/etc/init.d/mysqld start          <!--启动mysql命令-->
[root@centos01 mysql]# docker build -t mysql:v1.0 . <!--通过dockerfile生成mysql镜像-->
……………… <!--此处省略部分内容-->
Removing intermediate container 1362670945c1
Step 26/26 : CMD "/usr/local/mysql/mysqld.sh" && tail -f /dev/null
 ---> Running in 2a9bcf3ed523
 ---> 25e2ae633bcf
Removing intermediate container 2a9bcf3ed523
Successfully built 25e2ae633bcf
[root@centos01 mysql]# docker images |grep mysql  <!--docker宿主机查看mysql镜像-->
mysql                         v1.0                25e2ae633bcf        About a minute ago   2.41 GB
[root@centos01 mysql]# docker run -it -d --name mysql01 -p 192.168.100.10:3306:3306/tcp mysql:v1.0            
       <!--基于mysql镜像运行一个容器,并发布容器中的mysql服务-->
b4c8d6b4ca204420706934777a178a3f1bb4e067e93edf388609dc95140b0b31
[root@centos01 mysql]# docker ps              <!--查看运行的容器-->
CONTAINER ID        IMAGE               COMMAND                   CREATED             STATUS              PORTS                                   NAMES
b4c8d6b4ca20        mysql:v1.0          "/bin/sh -c ‘"/usr..."   8 seconds ago       Up 7 seconds        22/tcp, 192.168.100.10:3306->3306/tcp   mysql01
8f249f41662f        nginx:v1.0          "nginx"                   15 minutes ago      Up 15 minutes       22/tcp, 192.168.100.10:8080->80/tcp     nginx01
33de1fc52130        httpd:v1.0          "httpd -DFOREGROUND"      25 minutes ago      Up 25 minutes       22/tcp, 192.168.100.10:80->80/tcp       httpd01
[root@centos01 mysql]# docker exec -it mysql01 /bin/bash    <!--登录刚刚创建的容器-->
[root@b4c8d6b4ca20 mysql-5.5.22]# mysqladmin -uroot password "pwd@123"   
         <!--初始化mysql登录密码-->
mysql> grant all on *.* to ‘tom‘@‘192.168.100.%‘ identified by ‘pwd@123‘;   
            <!--在容器中登录mysql,授权tom拥有完全控制权限-->
Query OK, 0 rows affected (0.00 sec)
mysql> create database liyanxin;         <!--创建一个数据库-->
Query OK, 1 row affected (0.00 sec)

mysql> show databases;        <!--查看所有数据库-->
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liyanxin           |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
[root@centos01 ~]# yum -y install mysql           <!--安装mysql客户端-->
[root@centos01 ~]# mysql -utom -ppwd@123 -h 192.168.100.10 -P3306   
          <!--docker宿主机访问mysql01容器中的mysql数据库-->
MySQL [(none)]> show databases;        <!--查看所有数据库-->
+--------------------+ 
| Database           |
+--------------------+
| information_schema |
| liyanxin           |
| mysql              |
| performance_schema |
| test               |
+--------------------+

————————————本文到此结束,感谢观看——————————————

以上是关于通过Dockerfile部署TomcatApacheNginxMySQL服务的主要内容,如果未能解决你的问题,请参考以下文章

Dockerfile编写,容器部署项目,用nginx做负载均衡,私有仓库搭建

Dockerfile构建镜像与部署

使用 Dockerfile 部署镜像实例

使用Dockerfile定制自己的docker镜像

使用Dockerfile部署TOMCAT

实战docker,编写Dockerfile定制tomcat镜像,实现web应用在线部署