前面已經對安裝Tomcat進行了詳解:docker入門學習(一) html
直接上代碼java
//啓動一個Tomcat 命名爲mytomcat [root@localhost ~]# docker run -d --name mytomcat -p 9999:8080 tomcat cecb37db8134f1b1fe6d13c43b0b22d1832a9937b9d2bbfa9958eb180bd81eb6 //上傳war包到linux主機 我上傳到home文件夾下 [root@localhost ~]# cd /home [root@localhost home]# ls qrcode.war [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cecb37db8134 tomcat "catalina.sh run" 9 minutes ago Up 9 minutes 0.0.0.0:9999->8080/tcp mytomcat 1226f307b5ba tomcat "catalina.sh run" About an hour ago Up 28 minutes 0.0.0.0:8888->8080/tcp agitated_leavitt ec9d0d788590 mysql "docker-entrypoint.s…" 16 hours ago Up 16 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql02 //進入一個容器 sudo docker exec -it 容器id /bin/bash [root@localhost ~]# sudo docker exec -it cecb37db8134 /bin/bash root@cecb37db8134:/usr/local/tomcat# ls BUILDING.txt LICENSE README.md RUNNING.txt conf lib native-jni-lib webapps CONTRIBUTING.md NOTICE RELEASE-NOTES bin include logs temp work root@cecb37db8134:/usr/local/tomcat# //Ctrl+D 退出容器 root@cecb37db8134:/usr/local/tomcat# exit //複製war到Tomcat的webapps目錄下 [root@localhost ~]# docker cp /home/qrcode.war cecb37db8134:/usr/local/tomcat/webapps //從新進入容器 qrcode.war已經在webapps目錄下了 [root@localhost ~]# sudo docker exec -it cecb37db8134 /bin/bash root@cecb37db8134:/usr/local/tomcat# ls BUILDING.txt LICENSE README.md RUNNING.txt conf lib native-jni-lib webapps CONTRIBUTING.md NOTICE RELEASE-NOTES bin include logs temp work root@cecb37db8134:/usr/local/tomcat# ls ./webapps/ ROOT docs examples host-manager manager qrcode qrcode.war root@cecb37db8134:/usr/local/tomcat#
經常使用命令:mysql
//經過鏡像產生一個容器 docker run -p port1:port2 containerName:tag //啓動一個已有的容器 docker start containerID/containerName //中止一個容器 docker stop containerID/containerName //重啓一個容器 docker restart containerID/containerName //與一個已經啓動的容器進行交互式操做 docker exec -i -t containnerID/containerName /bin/bash //複製文件到容器中 docker cp src_path containerId:dest_path
訪問結果:成功linux
常見問題:git
若是訪問項目啓動頁成功,但沒法登陸 能夠使用 docker logs 容器id 命令查看日誌web
日誌若是是關於mysql、jdbc相關的問題 緣由多是你安裝了8以上版本的mysql,而項目的驅動包版本太低sql
方法1、下載低版本的mysqldocker
//下載你原來項目使用的mysql版本 [root@localhost ~]# docker pull mysql:5.7.22 //從新啓動mysql [root@localhost ~]# docker run -p 3306:3306 --name mysql03 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.22 fbd6e687b1a27202ef6408462317d4cf149646eaef3e801da04e81d7a187b271
方法2、更新mysql驅動包tomcat
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency>