当前位置: 首页 > 编程笔记 >

Docker安装Nginx教程实现图例讲解

骆嘉石
2023-03-14
本文向大家介绍Docker安装Nginx教程实现图例讲解,包括了Docker安装Nginx教程实现图例讲解的使用技巧和注意事项,需要的朋友参考一下

这里来安装下Nginx试下。

注意要明确一点,镜像是类,容器是对象。

查看当前的镜像

看到只有一个测试的镜像。

拉取镜像:

下载成功后查看,镜像已经被下载下来了:

使用 nginx 镜像

运行容器:

查看容器运行情况:

然后在浏览器输入网址:

修改文件:

[root@VM_0_4_centos bin]# docker ps
CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS        NAMES
 
8bf811453641    nginx        "nginx -g 'daemon of…"  4 minutes ago    Up 4 minutes    0.0.0.0:80->80/tcp  nginx_test

记住这里的 CONTAINER ID ,这是容器的ID

进入容器,修改:

[root@VM_0_4_centos bin]# docker exec -it 8bf811453641 /bin/bash
root@8bf811453641:/# cd /usr/share/nginx/html
root@8bf811453641:/usr/share/nginx/html# echo "hello docker">index.html
root@8bf811453641:/usr/share/nginx/html# exit

这是查看,修改的已经生效了。

如果想停止容器:

docker stop containerId // containerId 是容器的ID
[root@VM_0_4_centos bin]# docker stop 8bf811453641

然后用docker ps 看容器运行状态就行。

到此,容器运行完毕,总体来说非常的简单。

下面追加挂载方法先创建目录

mkdir -p /data/nginx/{conf,conf.d,html,logs}

nginx配置文件

/data/nginx/conf/nginx.conf

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  keepalive_timeout 65;

  #gzip on;

  server {
    listen    80;
    server_name localhost;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location / {
      root  /usr/share/nginx/html;
      index index.html index.htm;
    }

    #error_page 404       /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }

  }
  
  include /etc/nginx/conf.d/*.conf;
}

/data/nginx/conf.d/default.conf

server { 
  listen    80; 
  server_name localhost; 
 
  #charset koi8-r; 
  #access_log /var/log/nginx/log/host.access.log main; 
 
  location / { 
    #root  /data/nginx/html; 
    root  /usr/share/nginx/html; 
    index index.html index.htm; 
    #autoindex on; 
    #try_files $uri /index/index/page.html; 
    #try_files $uri /index/map/page.html; 
  } 
 
  #error_page 404       /404.html; 
 
  # redirect server error pages to the static page /50x.html 
  # 
  error_page  500 502 503 504 /50x.html; 
  location = /50x.html { 
    root  /usr/share/nginx/html; 
  } 
 
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
  # 
  #location ~ \.php$ { 
  #  proxy_pass  http://127.0.0.1; 
  #} 
 
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  # 
  #location ~ \.php$ { 
  #  root      html; 
  #  fastcgi_pass  127.0.0.1:9000; 
  #  fastcgi_index index.php; 
  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
  #  include    fastcgi_params; 
  #} 
 
  # deny access to .htaccess files, if Apache's document root 
  # concurs with nginx's one 
  # 
  #location ~ /\.ht { 
  #  deny all; 
  #} 
}

/data/nginx/html/index.html

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>系统时间</title>
</head>
<body>
<h1 id="datetime">
  <script>
    setInterval("document.getElementById('datetime').innerHTML=new Date().toLocaleString();", 1000);
  </script>
</h1>
</body>

删除容器

docker rm -f nginx-test

重新映射启动容器

docker run --name nginx-test -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html
-v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
-v /data/nginx/logs:/var/log/nginx
-v /data/nginx/conf.d:/etc/nginx/conf.d -d nginx:latest

再次运行

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍Docker 安装 PHP并与Nginx的部署实例讲解,包括了Docker 安装 PHP并与Nginx的部署实例讲解的使用技巧和注意事项,需要的朋友参考一下 安装 PHP 镜像 查找 Docker Hub 上的 php 镜像: 此外,我们还可以用 docker search php 命令来查看可用版本: 这里我们拉取官方的镜像,标签为7.3.24-fpm-stretch 等待下载完成

  • 本文向大家介绍linux下安装nginx(图文教程),包括了linux下安装nginx(图文教程)的使用技巧和注意事项,需要的朋友参考一下 Nginx是一款轻量级的Web 服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。以下主要介绍linux下安装nginx。 linux系统为Centos 64位 简介 一、Linux安装软件常用方法 1、rp

  • 本文向大家介绍Docker 安装 MySQL 并实现远程连接教程,包括了Docker 安装 MySQL 并实现远程连接教程的使用技巧和注意事项,需要的朋友参考一下 拉取镜像 查看拉取完成的镜像 通过镜像创建并启动一个MySQL容器 –name:给新创建的容器命名,此处命名为 mysql_dev -e:配置信息,此处配置mysql的root用户的登陆密码 -p:端口映射,此处映射主机3333端口到容

  • Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务 。 1、查看可用的 Nginx 版本 访问 Nginx 镜像库地址: https://hub.docker.com/_/nginx?tab=tags。 可以通过 Sort by 查看其他版本的 Nginx,默认是最新版本 nginx:latest。 你也可以在下拉列表中找到其他你想要

  • 本文向大家介绍Ubuntu Docker 安装教程,包括了Ubuntu Docker 安装教程的使用技巧和注意事项,需要的朋友参考一下 Ubuntu Docker 安装 Docker 支持以下的 Ubuntu 版本: Ubuntu Precise 12.04 (LTS) Ubuntu Trusty 14.04 (LTS) Ubuntu Wily 15.10 其他更新的版本……  前提条件 Dock

  • 本文向大家介绍docker在linux上的安装部署实例讲解,包括了docker在linux上的安装部署实例讲解的使用技巧和注意事项,需要的朋友参考一下 以下文章了解之后你可以在服务器上部署项目,本地不需要安装 tomcat,jdk,mysql等服务器,通过docker可以一键解决 安装docker 若找不到资源,需要更新资源库,命令为: 查看docker版本 更多的docker相关知识请关注:ht