博文目录
一、编写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
[root@centos01 ~]# cp /mnt/apache-tomcat-7.0.54.tar.gz ./tomcat/
[root@centos01 ~]# cp /mnt/jdk-7u65-linux-x64.gz ./tomcat/
[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
FROM hub.c.163.com/public/centos:6.7-tools
MAINTAINER 18232013151@163.com
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
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
hub.c.163.com/public/centos 6.7-tools b2ab0ed558bb 3 years ago 602 MB
[root@centos01 tomcat]# docker build -t 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
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
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
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
[root@centos01 ~]# cd apache/
[root@centos01 apache]# vim 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 .
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
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
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
www.dockerfile.com
3、登录httpd01容器,查看访问日志
[root@centos01 apache]# docker exec -it httpd01 /bin/bash
[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
[root@centos01 ~]# cp /mnt/nginx-1.6.0.tar.gz ./nginx/
[root@centos01 ~]# cd nginx/
[root@centos01 nginx]# vim 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 .
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
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
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
www.dockerfile.nginx.com
3、登录nginx01容器,查看访问日志
[root@centos01 nginx]# docker exec -it nginx01 /bin/bash
[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
[root@centos01 ~]# cp mysql-5.5.22.tar.gz cmake-2.8.6.tar.gz ./mysql
[root@centos01 ~]# cd mysql/
[root@centos01 mysql]# vim 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
#!/bin/bash
/etc/init.d/mysqld start
[root@centos01 mysql]# docker build -t mysql:v1.0 .
………………
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
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
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> grant all on *.* to 'tom'@'192.168.100.%' identified by 'pwd@123';
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
[root@centos01 ~]# mysql -utom -ppwd@123 -h 192.168.100.10 -P3306
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| liyanxin |
| mysql |
| performance_schema |
| test |
+--------------------+
————————————本文到此结束,感谢观看——————————————