docker-部署Nginx以及Tomcat - 详解
一、docker 部署Nginx
1、搜索镜像(nginx)
[root@localhost /]# docker search nginxError response from daemon: Get "https://index.docker.io/v1/search?q=nginx&n=25": dial tcp 192.133.77.133:443: connect: connection refused
简单说一下可能导致该报错的原因:(这里search失败不影响pull)
1、不同服务端点
- docker search: 访问 index.docker.io/v1/search (旧版API,IP为199.16.156.71)
docker pull:访问 registry-1.docker.io(新版V2 API,使用不同的IP/CDN)
- search 和 pull命令 可能位于不同的服务器集群,受网络策略影响不同。
2、网络限制
- 防火墙或安全组拦截了目标IP (199.16.156.71:443)
- 公司网络/代理可能仅允许部分 Docker 服务(如拉取镜像),但禁止搜索服务。
- DNS 污染或本地路由问题导致特定 IP 无法访问。
3、Docker 配置问题:
- 代理配置未正确应用到所有服务(如只对
registry-1.docker.io生效)。- Docker 守护进程的 DNS 配置错误。
2、拉取nginx镜像
[root@localhost /]# docker pull nginxUsing default tag: latestlatest: Pulling from library/nginxc353fd29d8c5: Pull complete 98b095d7d2b4: Pull complete af5f0e3644c1: Pull complete Digest: sha256:fad8e1cd52e24bce7b72cd7cb674a2efad671647b917055f5bd8a1f7ac9b1af8Status: Downloaded newer image for nginx:latestdocker.io/library/nginx:latest
3、查看镜像 -- 是否有nginx
[root@localhost /]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhello-world latest 74cc54e27dc4 4 months ago 10.1kBcentos latest 5d0da3dc9764 3 years ago 231MBnginx latest 3f8a4339aadd 7 years ago 108MB
4、启动nginx
[root@localhost /]# docker run -d --name naginx01 -p 3344:80 nginxb8ff70ab44d191d3b03d9843b3f8b9e4333911acab48ee3f3f0a3d529736b27c
参数解释
- -d(detach)
- 作用:后台运行容器(守护进程模式)
- ✅ 不阻塞当前终端
- ✅ 容器在后台持续运行
- ❌ 不加此参数:容器会占用当前终端(需用
Ctrl+P+Q退出)- -- name naginx01 (这里为啥是naginx01?因为敲代码的时候误触了)
- 作用:为容器指定唯一名称
- ✅ 自定义标识:
naginx01(可替换为其他名称)- ✅ 替代 Docker 生成的随机名称(如
festive_mcclintock)- ✅ 后续操作可直接用名称(无需容器ID):
- docker stop naginx01 # 停止容器
docker logs naginx01 # 查看日志
-p 3344:80(publish)作用:端口映射(主机端口:容器端口)
部分 说明 3344主机端口(可自定义) 80容器内部端口 ✅ 效果:
访问http://主机IP:3344→ 流量转发 → 容器的 80 端口
❗ 常见问题:若主机端口被占用,需改为空闲端口(如
-p 8080:80)安全组/防火墙需放行主机端口
端口暴露的概念

5、查看运行中的容器
[root@localhost /]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb8ff70ab44d1 nginx "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp naginx01
6、访问服务(本地测试)
[root@localhost /]# curl localhost:3344Welcome to nginx! body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }Welcome to nginx!If you see this page, the nginx web server is successfully installed andworking. Further configuration is required. For online documentation and support please refer tonginx.org.Commercial support is available atnginx.com. Thank you for using nginx.
7、进入容器
[root@localhost /]# docker exec -it naginx01 /bin/bashroot@b8ff70ab44d1:/# whereis nginxnginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginxroot@b8ff70ab44d1:/# cd /etc/nginxroot@b8ff70ab44d1:/etc/nginx# lsconf.d koi-utf mime.types nginx.conf uwsgi_paramsfastcgi_params koi-win modules scgi_params win-utfroot@b8ff70ab44d1:/etc/nginx#
二、部署Tomcat
前期工作和Nginx一样
1、访问的ip如何查询
Windows 浏览器访问 localhost 指向 Windows 系统本身,而非 Linux 的 Docker
获取Linux注解的IP地址:
root@localhost /]# ip addr show | grep "inet " | grep -v 127.0.0.1 inet 192.168.43.129/24 brd 192.168.43.255 scope global noprefixroute dynamic ens33 inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
在Windows浏览器访问:http://<Linux的IP地址>:3355
2、第一次访问产生的问题
第一次部署好访问: http://192.168.43.129:3355/

这个是正常的,但是为什么呢?
webapps里面是空的。(没有webapp)
阿里云镜像的原因。默认是最小的镜像,所以不必要的都剔除掉了。
保证最小可运行的环境!
webapps.dist中是有root的。
所以我们把webapps.dist中的文件cp到webapps中。就可以解决了;
root@16977d4a3b43:/usr/local/tomcat# cp -r webapps.dist/* webapps
思考问题 :我们以后要部署项目,如果每次都要进入容器,创建webapps内容,就很麻烦,。我们要可以在容器外部提供一个映射路径,webapps,在外部放置项目就自动同步到内部就好了。-v 数据卷



浙公网安备 33010602011771号