1.拉取tomcat镜像
docker pull tomcat # 获取tomcat 镜像
2.启动tomcat 容器 (仅用来复制配置文件,复制完即删除此容器)
docker run --name tomcat9 -d -p 8080:8080 tomcat #--name 别名
3.复制tomcat 配置文件到宿主机
docker cp tomcat9 ://usr/local/docker/tomcat9/conf /usr/local/tomcat/conf
docker cp tomcat9 : /usr/local/docker/tomcat9/webapps /usr/local/tomcat/webapps
docker cp tomcat9 :/usr/local/docker/tomcat9/logs /usr/local/tomcat/logs
docker cp:/opt/upload:/opt/upload #这个是把容器的目录复制到宿主机用于文件上传
4、移除容器
docker rm -f tomcat9
5、挂载tomcat9文件到宿主机
docker run -d --name tomcat9 -p 8080:8080 -v /usr/local/docker/tomcat9/conf:/usr/local/tomcat/conf -v /usr/local/docker/tomcat9/webapps:/usr/local/tomcat/webapps -v /usr/local/docker/tomcat9/logs/:/usr/local/tomcat/logs -v /opt/upload:/opt/upload --privileged=true --restart=always tomcat:9
#--name 给你启动的容器起个名字,以后可以使用这个名字启动或者停止容器
#-p 映射端口,将docker宿主机的8080端口和容器的8080口进行绑定
#-v 挂载文件用的,第一个-v 表示将你本地的nginx.conf覆盖你要起启动的容器的nginx.conf文件
#第二个表示将日志文件进行挂载,就是把nginx服务器的日志写到你docker宿主机的/root/docker/nginx/log/下面
#第三个-v 表示的和第一个-v意思一样的。
#-d 表示启动的是哪个镜像
6、查看docker 容器是否启动成功 有出现端口说明成功
docker ps -a
7、测试
使用curl测试端口号,通过ps 可以查出运行状态以及端口号,测试即可。可以发现出现页面404,说明可以访问了,只是路径出现问题
由于404,肯定是页面出现问题,所以就进去webapps下找
进入正在运行的tomcat9.0容器
docker exec -it tomcat9.0 /bin/bash
进入后可以发现两个重要的webapps以及webapps.dist
在自己tomcat可以发现,下面的东西时全部存在webapps文件夹下的,
点开webapps时发现为空
原来这个tomcat是被docker给阉割了,就是将原本属于webapps下面的全部东西放入到webapps.dist文件下面了
接下来就好办了,可以cp赋值过去,也可以rm移动过去,当然还可以直接删除webapps文件,再将webapps.dist重命名为webapps。这里选择复制
再切换到webapps,就能发现里面有东西了
完成以上步骤,就可以进行最终的测试了
可能需要30s左右,我们就能在浏览器上访问了。一定要用这个外网ip,而不是宿主机ip
部署web 应用
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!-- APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the APR/native implementation which always uses
OpenSSL for TLS.
Either JSSE or OpenSSL style configuration may be used. OpenSSL style
configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!--
<Connector protocol="AJP/1.3"
address="::1"
port="8009"
redirectPort="8443" />
-->
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/upload/" docBase="/opt/upload" debug="0"/>
<!-- /upload 是访问路径 /opt/upload 是存放图片的物理路径 -->
<!-- <Context path="/upload/" docBase="/opt/upload" debug="0"/>
<Context path="/upload/" docBase="/opt/upload" debug="0"/>
<Context path="upload" docBase="/opt/upload" reloadable="false" ></Context>
<Context path="" docBase="material" debug="0"/>
SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
上传方法
import com.alibaba.fastjson.JSONObject;
import com.asiainfo.adshare.aop.LoginControllerChecking;
import com.asiainfo.adshare.enums.IndexType;
import com.asiainfo.adshare.util.FileUtils;
import com.asiainfo.adshare.utils.Result;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/***
* 图片上传到阿里云
*/+
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
if (file != null) {
String s = uploadFileALIYUN(file);
return Result.ok(s);
}
return Result.ok("文件不存在");
}
public static String uploadFileALIYUN(MultipartFile file) throws IOException {
// 获取文件名
String fileName = file.getOriginalFilename();
// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 解决中文问题,liunx下中文路径,图片显示问题
fileName = UUID.randomUUID().toString().replace("-", "") + suffixName;
// 返回客户端 文件地址 URL
String url = "/upload/" + fileName;
// String url = "localhost:8090"+"/upload/" + fileName;
// File dest = new File("E:\\upload\\" + fileName);
//阿里云部署
File dest = new File("/opt/upload/" + fileName);
// File dest = new File(ApiConstant.DEV_UPLOAD_PATH + fileName);
// 检测是否存在目录
// log.info("sss===>{}",dest.getParentFile().exists());
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
file.transferTo(dest);
// return new ResultBuilder<>().setData(MapUtil.returnMap("url",url)).build();
log.info(url);
return url;
}
下载图片配置
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyPicConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/upload/**").addResourceLocations("file:/opt/upload/");
//registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/upload/");
}
}
注意 注意 注意
1.阿里云的端口对应的端口一定要放开
2.宿主机可以现创建挂载路径