Docker(十一):Docker实战部署HTTPS的Tomcat站点

1、选择基础镜像

   docker pull tomcat:7.0-jre8

2、生成HTTPS证书

  keytool -genkey -alias tomcat -keyalg RSA -keystore tomcat.keystore

  root@HX-StrMedia:~/ssl# ls
  tomcat.keystore

3、启动tomcat容器并导入证书

  root@7632c9d5c82b:/usr/local/tomcat# ls /tmp/
  tomcat.keystore
  root@7632c9d5c82b:/usr/local/tomcat# mkdir keys
  root@7632c9d5c82b:/usr/local/tomcat# cp /tmp/tomcat.keystore keys/
  root@7632c9d5c82b:/usr/local/tomcat# ls keys/
  tomcat.keystore

  修改配置文件如下:

         maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
    clientAuth="false" sslProtocol="TLS"
    keystoreFile="/usr/local/tomcat/keys/tomcat.keystore"
    keystorePass="dukuan" />

  也可配置8080 端口

         connectionTimeout="20000"
    redirectPort="8443"
    SSLEnabled="true" scheme="https" secure="true"
    clientAuth="false" sslProtocol="TLS"
    keystoreFile="/usr/local/tomcat/keys/tomcat.keystore"
    keystorePass="dukuan"
    />

4、Commit

  不退出容器在宿主机commit

  root@HX-StrMedia:~/ssl# docker ps -a
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  d3a3c7964a89 tomcat:7.0-jre8 "bash" 28 seconds ago Up 26 seconds 8080/tcp determined_mahavira

  root@HX-StrMedia:~/ssl# docker commit d3a3c7964a89 tomcat:https

  root@HX-StrMedia:~/ssl# docker images;
  REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  tomcat https 5308378959ab About a minute ago 560.8 MB

5、启动并访问

  root@HX-StrMedia:~/ssl# docker run --rm -ti -p 9099:8080 tomcat:https

  root@HX-StrMedia:~/ssl# docker ps
  CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  532a4ef7efcc tomcat:https "bash" 29 seconds ago Up 29 seconds 0.0.0.0:9099->8080/tcp thirsty_bhaskara

   访问宿主机9099端口即可

6、将web源码部署到Tomcat镜像中

  6.1 静态导入

    创建Dockerfile如下,将websrc下的源码复制到webapps下

      # cat Dockerfile
      # This is Dockerfile
      # Version 1.0
      # Author is Dukuan

      # 第一行必须指定基于的基础镜像
      FROM centos

      # 维护者信息
      MAINTAINER Du Kuan

      FROM tomcat:https

      COPY ./websrc /usr/local/tomcat/webapps/myproj/  

      # docker build -t myweb:v1 .
      Sending build context to Docker daemon 3.072 kB
      Step 1 : FROM centos
      ---> d4350798c2ee
      Step 2 : MAINTAINER Du Kuan
      ---> Running in cd053280ea4f
      ---> 687ac7bd642f
      Removing intermediate container cd053280ea4f
      Step 3 : FROM tomcat:https
      ---> 71400c2da356
      Step 4 : COPY ./websrc /usr/local/tomcat/webapps/myproj/
      ---> f704dc9f0466
      Removing intermediate container 4fd43be2f210
      Successfully built f704dc9f0466

      # docker images
      REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
      myweb v1 f704dc9f0466 7 minutes ago 560.8 MB
      tomcat https 71400c2da356 4 hours ago 560.8 MB

  6.2 静态挂载

    创建Dockerfile

    # This is Dockerfile
    # Version 1.0
    # Author is Dukuan

    # 第一行必须指定基于的基础镜像
    FROM tomcat:https

    # 维护者信息
    MAINTAINER Du Kuan

    RUN mkdir -p /usr/local/tomcat/webapps/myproj

    # 表示在镜像中创建一个挂载点
    VOLUME /usr/local/tomcat/webapps/myproj

    创建镜像

    docker build -t myweb:v2 .

    启动镜像

    docker run -ti -v $(pwd)/../websrc:/usr/local/tomcat/webapps/myproj myweb:v2

    主机修改文件

    echo "This is page" >> index.jsp

    查看容器是否修改  

    root@4899f6c92448:/usr/local/tomcat# cat webapps/myproj/index.jsp
    Test Page
    This is page

    

转载于:https://www.cnblogs.com/dukuan/p/7667807.html

你可能感兴趣的:(Docker(十一):Docker实战部署HTTPS的Tomcat站点)