docker-部署Nginx以及Tomcat

一、docker 部署Nginx

1、搜索镜像(nginx)

bash 复制代码
[root@localhost /]# docker search nginx
Error 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镜像

bash 复制代码
[root@localhost /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c353fd29d8c5: Pull complete 
98b095d7d2b4: Pull complete 
af5f0e3644c1: Pull complete 
Digest: sha256:fad8e1cd52e24bce7b72cd7cb674a2efad671647b917055f5bd8a1f7ac9b1af8
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

3、查看镜像 -- 是否有nginx

bash 复制代码
[root@localhost /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    74cc54e27dc4   4 months ago   10.1kB
centos        latest    5d0da3dc9764   3 years ago    231MB
nginx         latest    3f8a4339aadd   7 years ago    108MB

4、启动nginx

bash 复制代码
[root@localhost /]# docker run -d --name naginx01 -p 3344:80 nginx
b8ff70ab44d191d3b03d9843b3f8b9e4333911acab48ee3f3f0a3d529736b27c

参数解释

  • -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、查看运行中的容器

bash 复制代码
[root@localhost /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
b8ff70ab44d1   nginx     "nginx -g 'daemon of..."   5 seconds ago   Up 4 seconds   0.0.0.0:3344->80/tcp, :::3344->80/tcp   naginx01

6、访问服务(本地测试)

bash 复制代码
[root@localhost /]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

7、进入容器

bash 复制代码
[root@localhost /]# docker exec -it naginx01 /bin/bash
root@b8ff70ab44d1:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@b8ff70ab44d1:/# cd /etc/nginx
root@b8ff70ab44d1:/etc/nginx# ls
conf.d		koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params	koi-win  modules     scgi_params  win-utf
root@b8ff70ab44d1:/etc/nginx# 

二、部署Tomcat

前期工作和Nginx一样

1、访问的ip如何查询

Windows 浏览器访问 localhost 指向 Windows 系统本身,而非 Linux 的 Docker

获取Linux注解的IP地址:

bash 复制代码
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中。就可以解决了;

bash 复制代码
root@16977d4a3b43:/usr/local/tomcat# cp -r webapps.dist/* webapps

思考问题 :我们以后要部署项目,如果每次都要进入容器,创建webapps内容,就很麻烦,。我们要可以在容器外部提供一个映射路径,webapps,在外部放置项目就自动同步到内部就好了。-v 数据卷

相关推荐
Scott9999HH8 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
腻害兔8 小时前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 商城模块:从商品管理到交易引擎,50 张表撑起一整套电商系统
java·大数据·vue.js·产品经理·ai编程
码智社9 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海9 小时前
python 列表、元组、集合和字典
开发语言·python
萧瑟余晖10 小时前
JDK 26 新特性详解
java·开发语言
马优晨11 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
人邮异步社区12 小时前
怎么把C语言学到精通?
c语言·开发语言
心平气和量大福大13 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai13 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
维天说13 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json