使用 docker 部署 Nginx

1、部署nginx

# 1. 搜索镜像
docker search nginx

# 2. 下载镜像
docker pull nginx 

# 3. 查看镜像是否下载成功
docker image

# 4. 启动容器(-d:后台运行 --name:容器名 -p:暴露端口 宿主机端口3344:容器端口80)
dockerrun -d --name nginx01 -p 3344:80 nginx

# 5. 查看容器是否启动
docker ps 

# 6. 本机测试
curl localhost:3344

完整代码:

 #1、 搜索镜像

[root@iZ2vcfsvipso9tjpqp6fuyZ /]# docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                                             Official build of Nginx.                        18636     [OK]       
unit                                              Official build of NGINX Unit: Universal Web …   6         [OK]       
nginxproxy/nginx-proxy                            Automated Nginx reverse proxy for docker con…   90                   
nginxproxy/acme-companion                         Automated ACME SSL certificate generation fo…   116                  
bitnami/nginx                                     Bitnami nginx Docker Image                      165                  [OK]
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   29                   [OK]
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   96                   
nginxproxy/docker-gen                             Generate files from docker container meta-da…   12                   
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in…   6                    
rancher/nginx-ingress-controller                                                                  11                   
rancher/nginx-ingress-controller-defaultbackend                                                   2                    
bitnami/nginx-exporter                                                                            3                    
rancher/nginx                                                                                     2                    
rapidfort/nginx-ib                                RapidFort optimized, hardened image for NGIN…   10                   
rapidfort/nginx                                   RapidFort optimized, hardened image for NGINX   14                   
vmware/nginx-photon                                                                               1                    
bitnami/nginx-ldap-auth-daemon                                                                    3                    
rapidfort/nginx-official                          RapidFort optimized, hardened image for NGIN…   10                   
vmware/nginx                                                                                      2                    
rancher/nginx-conf                                                                                0                    
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   203                  
nginxproxy/forego                                 Foreman in Go                                   0                    
bitnamicharts/nginx                                                                               0                    
privatebin/nginx-fpm-alpine                       PrivateBin running on an Nginx, php-fpm & Al…   75                   [OK]
bitnami/nginx-intel    

# 2、下载镜像                                                                          1                    
[root@iZ2vcfsvipso9tjpqp6fuyZ /]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

# 3、查看镜像是否下载成功
[root@iZ2vcfsvipso9tjpqp6fuyZ /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
nginx         latest    605c77e624dd   17 months ago   141MB

# 4、启动容器 (-d: 后台启动   --name: 容器的别名  -p: 暴露端口  宿主主机端口号3344::容器端口号80 )
[root@iZ2vcfsvipso9tjpqp6fuyZ /]# docker run -d --name nginx01 -p 3344:80 nginx
550741303b309f0b3e108b122b58de56102a78adb2129fede58823cdfee33006

# 5.、查看容器是否启动
[root@iZ2vcfsvipso9tjpqp6fuyZ /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
550741303b30   nginx     "/docker-entrypoint.…"   8 seconds ago   Up 8 seconds   0.0.0.0:3344->80/tcp, :::3344->80/tcp   nginx01

# 6.、本机测试
[root@iZ2vcfsvipso9tjpqp6fuyZ /]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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、进入容器
[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# docker exec -it nginx01 /bin/bash
root@1f9dbdf7f36d:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@1f9dbdf7f36d:/# cd /etc/nginx/
root@1f9dbdf7f36d:/etc/nginx# ls
conf.d    fastcgi_params    koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params    uwsgi_params  win-utf

# 8、停止容器

[root@iZ2vcfsvipso9tjpqp6fuyZ /]# docker ps -aq
550741303b30
[root@iZ2vcfsvipso9tjpqp6fuyZ /]# docker stop 550741303b30
550741303b30
 

参考狂神说Docker14、作业一:部署Nginx_哔哩哔哩_bilibili

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是使用 Docker 部署 Nginx使用 Nginx 部署项目的步骤: 1. 首先,需要安装 Docker,可以参考官网的安装教程进行安装。 2. 创建一个用于存放 Nginx 配置文件和项目文件的目录,比如 /home/nginx。 3. 在该目录下创建一个名为 nginx.conf 的 Nginx 配置文件,可以参考以下示例进行编写: ``` worker_processes 1; events { worker_connections 1024; } http { server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; } location /api/ { proxy_pass http://backend:8000/; } } } ``` 上述配置文件中,Nginx 监听本地 80 端口,将访问根目录的请求转发到 /usr/share/nginx/html 目录下的 index.html 文件,将以 /api/ 开头的请求转发到名为 backend 的 Docker 容器中的 8000 端口。 4. 在该目录下创建一个名为 Dockerfile 的文件,用于构建自定义的 Nginx 镜像,可以参考以下示例进行编写: ``` FROM nginx COPY nginx.conf /etc/nginx/nginx.conf COPY dist/ /usr/share/nginx/html/ ``` 上述 Dockerfile 文件中,使用 nginx 作为基础镜像,将本地的 nginx.conf 文件复制到容器中的 /etc/nginx/nginx.conf 目录下,将本地的项目文件复制到容器中的 /usr/share/nginx/html/ 目录下。 5. 使用以下命令构建并运行 Nginx 容器: ``` docker build -t my-nginx . docker run --name my-nginx -p 80:80 --link backend my-nginx ``` 上述命令中,首先使用 docker build 命令构建名为 my-nginx 的自定义 Nginx 镜像,然后使用 docker run 命令启动该镜像对应的容器,将本地的 80 端口映射到容器的 80 端口,并连接名为 backend 的后端容器。 6. 至此,使用 Docker 部署 Nginx使用 Nginx 部署项目的过程已完成。可以通过浏览器访问 http://localhost 来查看部署的项目。如果该项目中有需要通过 /api/ 访问的后端服务,可以在后端容器中启动该服务,并将其命名为 backend,以便 Nginx 容器能够连接到该服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值