首页 运维 nginx Docker如何运行nginx并挂载本地目录到镜像中

Docker如何运行nginx并挂载本地目录到镜像中

May 15, 2023 pm 08:25 PM
docker nginx

1 从hup上pull镜像

docker pull nginx
登录后复制

2 创建将要挂载的目录

mkdir -p /data/nginx/{conf,conf.d,html,logs}
登录后复制

3 先要有配置文件才能启动容器

3.1 vim /data/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;

  include /etc/nginx/conf.d/*.conf;
}
登录后复制

3.2 vim /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/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; 
  #} 
}
登录后复制

4 启动容器

#将容器中nginx的80端口映射到本地的81端口
docker run --name nginx81 -d -p 81: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
登录后复制

5 查看启动的容器

[root@dc01 ~]# docker ps
container id image command created status ports names
fa56f865bd26 nginx:latest "nginx -g 'daemon of…" 4 weeks ago up 3 seconds 0.0.0.0:80->80/tcp vigilant_swirles
[root@dc01 ~]#
登录后复制

6 网页访问nginx

Docker如何运行nginx并挂载本地目录到镜像中

以上是Docker如何运行nginx并挂载本地目录到镜像中的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

docker desktop怎么用 docker desktop怎么用 Apr 15, 2025 am 11:45 AM

如何使用 Docker Desktop?Docker Desktop 是一款工具,用于在本地机器上运行 Docker 容器。其使用步骤包括:1. 安装 Docker Desktop;2. 启动 Docker Desktop;3. 创建 Docker 镜像(使用 Dockerfile);4. 构建 Docker 镜像(使用 docker build);5. 运行 Docker 容器(使用 docker run)。

docker镜像源怎么换国内 docker镜像源怎么换国内 Apr 15, 2025 am 11:30 AM

可切换到国内镜像源,步骤如下:1. 编辑配置文件 /etc/docker/daemon.json,添加镜像源地址;2. 保存退出后,重启 Docker 服务 sudo systemctl restart docker,即可提升镜像下载速度和稳定性。

docker版本怎么看 docker版本怎么看 Apr 15, 2025 am 11:51 AM

要获取 Docker 版本,您可以执行以下步骤:运行 Docker 命令“docker --version”来查看客户端和服务器版本。对于 Mac 或 Windows,还可以通过 Docker Desktop GUI 的“版本”选项卡或“关于 Docker Desktop”菜单查看版本信息。

docker怎么创建镜像 docker怎么创建镜像 Apr 15, 2025 am 11:27 AM

创建 Docker 镜像步骤:编写包含构建指令的 Dockerfile。在终端中构建镜像,使用 docker build 命令。标记镜像,使用 docker tag 命令分配名称和标签。

docker怎么更新镜像 docker怎么更新镜像 Apr 15, 2025 pm 12:03 PM

更新 Docker 镜像的步骤如下:拉取最新镜像标记新镜像为特定标签删除旧镜像(可选)重新启动容器(如果需要)

docker镜像怎么保存 docker镜像怎么保存 Apr 15, 2025 am 11:54 AM

在 Docker 中保存镜像,可以使用 docker commit 命令创建新的镜像,包含指定容器的当前状态,语法为:docker commit [选项] 容器ID 镜像名称。要保存镜像到仓库,可以使用 docker push 命令,语法为:docker push 镜像名称[:标签]。要导入已保存的镜像,可以使用 docker pull 命令,语法为:docker pull 镜像名称[:标签]。

docker容器名称怎么查 docker容器名称怎么查 Apr 15, 2025 pm 12:21 PM

可以通过以下步骤查询 Docker 容器名称:列出所有容器(docker ps)。筛选容器列表(使用 grep 命令)。获取容器名称(位于 "NAMES" 列中)。

docker怎么查看日志 docker怎么查看日志 Apr 15, 2025 pm 12:24 PM

查看 Docker 日志的方法包括:使用 docker logs 命令,例如:docker logs CONTAINER_NAME使用 docker exec 命令运行 /bin/sh 并查看日志文件,例如:docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log使用 Docker Compose 的 docker-compose logs 命令,例如:docker-compose -f docker-com

See all articles