Docker部署Nginx

1. 前言:什么是Nginx?

先占个坑位再补博客

2. Docker安装Nginx

2.1 搜索镜像

第一种方式: 使用docker命令:docker search 镜像名(作为复习docker命令啦~)

[root@localhost /]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        14387               [OK]                

第二种方式:Docker hub官网搜索(推荐,可以看到镜像的详细信息)

2.2 拉取镜像

使用docker命令:docker pull 镜像名

[root@localhost /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete 
0732ab25fa22: Pull complete 
d7f36f6fe38f: Pull complete 
f72584a26f32: Pull complete 
7125e4df9063: Pull complete 
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest

# 使用docker images查看是否安装成功
[root@localhost /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              f6d0b4767a6c        3 weeks ago         133MB
centos              latest              300e315adb2f        8 weeks ago         209MB
[root@localhost /]# 

2.3 运行测试

Nginx的默认端口号是80
现在我们以后台的方式启动nginx,并给nginx挂载个名字nginx01,暴露端口3344。
即通过公网的3344访问docker的80端口

# -d 后台运行
# --name 给容器起名字
# -p 暴露端口。宿主机端口:容器内部端口
[root@localhost /]# docker run -d --name nginx01 -p 3344:80 nginx
18f7730e8f8e1bc5b35fad26f0efd5810c87c4c06dbe274f37e58930c123d019
[root@localhost /]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
18f7730e8f8e        nginx               "/docker-entrypoint.…"   About an hour ago   Up About an hour    0.0.0.0:3344->80/tcp   nginx01

# 本机测试
[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>
[root@localhost /]# 

稍微解释一下端口暴露

容器nginx01由镜像通过run启动起来,每个容器相当于一个小的linux。每个容器都是独立的,所以每个容器里也都有一个防火墙。我们启动nginx01开放80端口,但此时是在docker容器里面,nginx01与外面是隔离的。
那么外网想要访问,外网首先请求到linux上,linux开启端口3344。(若使用阿里云,中间还需要拦一道阿里云的配置)此时,外网可以访问到linux了,但linux还不能访问到docker。
因此需要让宿主机和容器打通,使用命令-p 3344:80
在这里插入图片描述

# 进nginx01
[root@localhost /]# docker exec -it nginx01 /bin/bash

# 寻找nginx的配置文件
root@18f7730e8f8e:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@18f7730e8f8e:/# cd etc/nginx

# 查看nginx的配置文件
root@18f7730e8f8e:/etc/nginx# ls
conf.d		koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params	koi-win  modules     scgi_params  win-utf
root@18f7730e8f8e:/etc/nginx# exit
exit

思考题:我们每次改动nginx配置文件,都需要进入容器内部,十分麻烦。若可以在容器外部提供一个映射路径,做到在容器修改文件名,容器内部可以自动同步修改,如何实现?
答:-v 数据卷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值