Docker deploys offline Nginx image container and Tomcat image container
1. Preparation
1. Install Docker and load the centos7 base image
The software package needed for the experiment is downloaded from the following link:
https://pan.baidu.com/s/1T_nWUMJAUXaGgawey9V30Q
Extraction code: 0bs3
[root@localhost ~]# rz #上传centos7和docker-18.03.1-ce.tgz
z waiting to receive.**B0100000023be50
[root@localhost ~]# ls
anaconda-ks.cfg docker-18.03.1-ce.tgz 公共 视频 文档 音乐
centos7 initial-setup-ks.cfg 模板 图片 下载 桌面
[root@localhost ~]# tar xf docker-18.03.1-ce.tgz #解压
[root@localhost ~]# cp docker/* /usr/bin/ #优化执行路径
[root@localhost ~]# dockerd & #启动Docker
[root@localhost ~]# docker load < centos7 #载入centos7镜像
f972d139738d: Loading layer 208.8MB/208.8MB
Loaded image: centos:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 75835a67d134 2 years ago 200MB
2. The host releases the software source
Because you need to use yum to install the software package when the image is created, the host uses the http method to publish the software source. The software source is the centos7 CD
1) Install and start http service on the host
挂载光盘,配置本地yum源
[root@localhost ~]# yum -y install httpd && systemctl start httpd
2) Create a shared directory
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mkdir centos7
[root@localhost html]# mount /dev/cdrom centos7/
mount: /dev/sr0 写保护,将以只读方式挂载
3) Use the browser to access the test
Two, deploy Nginx container
Nginx is a lightweight web server and an excellent reverse proxy server. The Nginx service takes up less memory and has strong concurrency. The following uses the Dockerfile file to create a Docker image with Nginx service.
1. Create a working directory
[root@localhost ~]# mkdir nginx
[root@localhost ~]# cd nginx/
[root@localhost nginx]# rz #上传nginx-1.12.0.tar.gz软件包
z waiting to receive.**B0100000023be50
[root@localhost nginx]# ls
nginx-1.12.0.tar.gz
2. Create and write a Dockerfile
[root@localhost nginx]# vim Dockerfile
#基础镜像
FROM centos
#维护该镜像的用户信息
MAINTAINER zhangsan
#设置本地yum源
RUN rm -rf /etc/yum.repos.d/*
ADD CentOS7.repo /etc/yum.repos.d/
#安装相关依赖包
RUN yum -y install proc-devel net-tools gcc zlib zlib-devel make openssl-devel
#传入Nginx软件包并解压
COPY nginx-1.12.0.tar.gz /
RUN tar zxf /nginx-1.12.0.tar.gz
#切换目录
WORKDIR /nginx-1.12.0
#编译安装
RUN ./configure --prefix=/usr/local/nginx && make && make install
#开启端口
EXPOSE 80
EXPOSE 443
#修改Nginx文件,以非daemon方式启动
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
#复制启动脚本
ADD run.sh /
RUN chmod 755 /run.sh
#运行启动脚本
CMD ["/run.sh"]
3. Write and execute script content
[root@localhost nginx]# vim run.sh
#!/bin/bash
/usr/local/nginx/sbin/nginx
4. Write yum source files
[root@localhost nginx]# cp /etc/yum.repos.d/CentOS7.repo .
[root@localhost nginx]# vim CentOS7.repo
[zhangsan]
name=lisi
baseurl=http://192.168.1.10/centos7
enabled=1
gpgcheck=0
5. Generate a mirror
[root@localhost nginx]# docker build -t nginx:v1 .
[root@localhost nginx]# docker images | grep nginx
nginx v1 3ab35fe44bf6 2 minutes ago 349MB
6. Start the container for testing
[root@localhost nginx]# docker run -d --name nginx -P nginx:v1
[root@localhost nginx]# docker ps | grep nginx
7. Access test
Three, deploy the Tomcat container
Tomcat is a free and open source lightweight Web server, commonly used in small and medium-sized enterprises and occasions where concurrent access is not high. It is the first choice for developing and debugging JSP programs. The following uses the Dockerfile file method to create a Docker image with Tomcat service.
1. Create a working directory and drag into related software
[root@localhost ~]# mkdir tomcat
[root@localhost ~]# cd tomcat/
[root@localhost tomcat]# rz #上传apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz软件包
z waiting to receive.**B0100000023be50
[root@localhost tomcat]# ls
apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz
[root@localhost tomcat]# tar zxf jdk-8u91-linux-x64.tar.gz
[root@localhost tomcat]# ls
apache-tomcat-8.5.16.tar.gz jdk1.8.0_91 jdk-8u91-linux-x64.tar.gz
2. Write Dockerfile
[root@localhost tomcat]# vim Dockerfile
FROM centos
MAINTAINER zhangsan
#给JDK设置变量
ADD jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin/:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
#传入Tomcat软件包
COPY apache-tomcat-8.5.16.tar.gz /
RUN tar zxf /apache-tomcat-8.5.16.tar.gz
#将解压后文件移动位置,重命名为tomcat
RUN mv /apache-tomcat-8.5.16 /usr/local/tomcat
#开启80端口
EXPOSE 80
#复制启动脚本
ADD run.sh /
RUN chmod 755 /run.sh
#运行启动脚本
CMD ["/run.sh"]
3. Write and execute script content
[root@localhost tomcat]# vim run.sh
#!/bin/bash
/usr/local/tomcat/bin/startup.sh
tailf /run
[root@localhost tomcat]# ls
apache-tomcat-8.5.16.tar.gz jdk1.8.0_91 run.sh
Dockerfile jdk-8u91-linux-x64.tar.gz
5. Generate image file
[root@localhost tomcat]# docker build -t tomcat:v8 .
Sending build context to Docker daemon 557.4MB
[root@localhost tomcat]# docker images | grep tomcat
tomcat v8 67ec7f712b12 23 seconds ago 601MB
6. Run the container and verify
[root@localhost tomcat]# docker run -d --name tomcat -p 1314:8080 tomcat:v8
[root@localhost tomcat]# docker ps | grep tomcat
Use browser access verification