docker部署笔记
安葬docker和相关容器,参考的文章如下:
https://blog.csdn.net/wyc591471297/article/details/122861297
https://blog.csdn.net/Amor_Leo/article/details/85858145
https://blog.csdn.net/weixin_47136046/article/details/110873340
最后安装的容器和镜像如下:
[root@VM-12-17-centos ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
watchkeyadmin v1.0 eae5168d26b5 5 hours ago 643 MB
docker.io/mongo latest d98599fdfd65 34 hours ago 696 MB
docker.io/redis 6 8419619d5d01 42 hours ago 113 MB
docker.io/nginx latest b692a91e4e15 2 days ago 142 MB
docker.io/mongo 4.4.10 275f79a10a88 8 months ago 437 MB
docker.io/portainer/portainer latest 980323c8eb3f 19 months ago 196 MB
docker.io/nginx 1.14.1 5f2d4477b7ea 3 years ago 109 MB
docker.io/java 8 d23bdf5b1b1b 5 years ago 643 MB
[root@VM-12-17-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6071b50e71ba watchkeyadmin:v1.0 "sh -c /data/run.sh" 4 hours ago Up 4 hours watchkeyadmin
efa7bcde8b65 mongo:4.4.10 "docker-entrypoint..." 4 hours ago Up 4 hours 0.0.0.0:19280->27017/tcp mongo
066552197acb redis:6 "docker-entrypoint..." 4 hours ago Up 4 hours 0.0.0.0:17379->6379/tcp redis
aa6f242af9cf docker.io/portainer/portainer "/portainer" 5 hours ago Up 5 hours 8000/tcp, 0.0.0.0:9000->9000/tcp prtainer-test
[root@VM-12-17-centos ~]#
现在域名解析:
现在既有一个java的独立项目,也有docker的容器项目,所以必须要用Nginx来转换端口,实现admin.xxxxx.cn指向业务逻辑端口,目前这个没有去实现,当前只是通过指定端口来访问,测试是可行的!以后可以参考这个文章,以后有机会实现:https://zhuanlan.zhihu.com/p/453181968
caddy安装和配置
本文摘录于:https://zhuanlan.zhihu.com/p/263022775只是做学习备份之用,绝无抄袭之意,有疑惑请联系本人!
因为服务器根据端口来响应app,所以想要响应api.xxx.com的请求就必须要安装反向代理才行,这里使用caddy,按照好后可以看到有如下目录:
目录中的start.sh内容如下:
nohup /app/caddy/caddy2 start >> /app/caddy/log/stdout.log &
可以通过如下命令查看版本号:
root@copilot-os:/app/caddy# ./caddy2 -h
[ERROR] first argument must be a subcommand; see 'caddy help'
root@copilot-os:/app/caddy# ./caddy2 help
Caddy is an extensible server platform.
usage:
caddy <command> [<args...>]
commands:
adapt Adapts a configuration to Caddy's native JSON
build-info Prints information about this build
environ Prints the environment
file-server Spins up a production-ready file server
fmt Formats a Caddyfile
hash-password Hashes a password and writes base64
help Shows help for a Caddy subcommand
list-modules Lists the installed Caddy modules
reload Changes the config of the running Caddy instance
reverse-proxy A quick and production-ready reverse proxy
run Starts the Caddy process and blocks indefinitely
start Starts the Caddy process in the background and then returns
stop Gracefully stops a started Caddy process
trust Installs a CA certificate into local trust stores
untrust Untrusts a locally-trusted CA certificate
upgrade Upgrade Caddy (EXPERIMENTAL)
validate Tests whether a configuration file is valid
version Prints the version
Use 'caddy help <command>' for more information about a command.
Full documentation is available at:
https://caddyserver.com/docs/command-line
root@copilot-os:/app/caddy# ./caddy2 version
v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I=
root@copilot-os:/app/caddy#
安装caddy的教程请看如下文章:
本文摘录于:https://zhuanlan.zhihu.com/p/263022775只是做学习备份之用,绝无抄袭之意,有疑惑请联系本人!
安装命令和日志如下:
Last login: Wed Aug 24 21:56:49 2022 from 127.0.0.1
[root@VM-12-17-centos ~]# mkdir /app/caddy && cd /app/caddy
[root@VM-12-17-centos caddy]# wget -N --no-check-certificate -O "caddy" "https://caddyserver.com/api/download?os=linux&arch=amd64"
WARNING: timestamping does nothing in combination with -O. See the manual
for details.
--2022-08-24 21:58:17-- https://caddyserver.com/api/download?os=linux&arch=amd64
Resolving caddyserver.com (caddyserver.com)... 165.227.20.207, 2604:a880:2:d0::21b0:6001
Connecting to caddyserver.com (caddyserver.com)|165.227.20.207|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/octet-stream]
Saving to: ‘caddy’
[ <=> ] 39,202,816 26.1KB/s in 17m 22s
2022-08-24 22:15:40 (36.8 KB/s) - ‘caddy’ saved [39202816]
[root@VM-12-17-centos caddy]# chmod +x caddy
[root@VM-12-17-centos caddy]# ls
caddy
[root@VM-12-17-centos caddy]# ./caddy version
v2.5.2 h1:eCJdLyEyAGzuQTa5Mh3gETnYWDClo1LjtQm2q9RNZrs=
[root@VM-12-17-centos caddy]#
运行caddy,把start.
sudo /app/caddy/start.sh
[root@VM-12-17-centos ~]# nohup: redirecting stderr to stdout
这里要注意两点(被坑之后血的教训):
1.要进入caddy目录去启动,不要用目录去启动:
cd /app/caddy/
./start.sh
2.端口一定要设置对,一定要开启433端口:
sudo ufw allow 443
我这里最后设置的端口如下:
root@VM-12-17-ubuntu:~# sudo ufw status
Status: active
To Action From
80 ALLOW Anywhere
9000 ALLOW Anywhere
16990 ALLOW Anywhere
27017 DENY Anywhere
19280 DENY Anywhere
22 ALLOW Anywhere
8080 ALLOW Anywhere
8081 ALLOW Anywhere
443 ALLOW Anywhere
80 (v6) ALLOW Anywhere (v6)
9000 (v6) ALLOW Anywhere (v6)
16990 (v6) ALLOW Anywhere (v6)
27017 (v6) DENY Anywhere (v6)
19280 (v6) DENY Anywhere (v6)
22 (v6) ALLOW Anywhere (v6)
8080 (v6) ALLOW Anywhere (v6)
8081 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
Tomcat安装和web应用测试环境搭建
这里参考如下文章:
https://blog.csdn.net/qq_40622253/article/details/108071857 //这篇文章操作是没有问题的,但是在启动Tomcat的时候有个坑,没法连接mysql
https://www.cnblogs.com/yds941268778/p/12776303.html //这篇文章解决了上面说的坑
另外:启动Tomcat的时候注意使用Tomcat的版本号,否则可能运行失败
docker search tomcat
docker pull tomcat:8
docker run -it --name tomcat -p 8080:8080 --link mysql:dbhost -d tomcat:8
下面是测试环境的搭建,直接拷贝到Tomcat中,然后使用域名+端口号+项目名称来访问
删除webapps
rm -r webapps
#重命名
mv webapps.dist webapps
http://xxxxxx.cn:8080
http://xxxxxx.cn:8080/zzzzzz
上面测试网址中x为域名,z为项目名
//进入tomcat
docker exec -it tomcat /bin/bash
rm -r webapps/zzzzzz
docker cp /app/zzzzzz.war tomcat:/usr/local/tomcat/zzzzzz
这种情况下项目中访问的"/"目录就是docker中的目录,而不是服务器的目录:
root@d1eb26eee447:/usr/local/tomcat# ls /
bin data etc lib lib64 media opt root sbin sydtek tmp var
boot dev home lib32 libx32 mnt proc run srv sys usr
root@d1eb26eee447:/usr/local/tomcat# ls /sydtek/
upload
root@d1eb26eee447:/usr/local/tomcat# ls /sydtek/upload/
15994846325 15994846325.zip
root@d1eb26eee447:/usr/local/tomcat# ls /data/
sydtek
root@d1eb26eee447:/usr/local/tomcat# ls /data/sydtek/
log
root@d1eb26eee447:/usr/local/tomcat# ls /data/sydtek/log/
debug.log error.log
日志只能够在这些目录访问!
docker安装mysql
安装说明可以看docker部署的相关文章:https://blog.csdn.net/wyc591471297/article/details/122861297
但是按照这个操作,启动的是有有个巨坑,就是5.7的版本有bug,要用5.7.34的版本,解决办法请看如下文章:https://blog.csdn.net/weixin_45654405/article/details/126375756
启动的时候注意有如下两种方式启动:
//可以从外部访问
docker run --net=host -p 3306:3306 --name mysql
-v /data/mysql/log:/var/log/mysql
-v /data/mysql/data:/var/lib/mysql
-v /data/mysql/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=root
-d mysql:5.7.34
//只能够从内部访问
docker run -p 3306:3306 --name mysql
-v /data/mysql/log:/var/log/mysql
-v /data/mysql/data:/var/lib/mysql
-v /data/mysql/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=root
-d mysql:5.7.34
启动mysql后要做如下的一些配置:
create database sydtek character set utf8mb4 COLLATE = utf8mb4_unicode_ci;
docker cp /data/sydtek.sql mysql:/
use sydtek;
source /sydtek.sql;
#在MySQL 5.7 password字段已从mysql.user表中删除,新的字段名是“authenticalion_string”.
#选择数据库:
use mysql;
#更新root的密码:
update user set authentication_string=password(‘xxxxxx’) where user=‘root’ and Host=‘localhost’;
#刷新权限:
flush privileges;
另外如果要从外网通过Navicat等访问还要做如下配置:
docker exec -it mysql /bin/bash
mysql -u root -p --default-character-set=utf8mb4
docker exec -it a9713a87910a /bin/bash
GRANT ALL PRIVILEGES ON . TO ‘root’@‘%’ IDENTIFIED BY ‘xxxx’ WITH GRANT OPTION;
如果要在Tomcat的内部测试项目中想要访问到mysql,请看<Tomcat安装和web应用测试环境搭建>章节内容