[Docker系列·2] 搭建基于Docker的Nginx服务器

简介: ####docker命令别名 **~/.bashrc** ``` # .bashrc alias d="sudo docker” ``` ####docker的nginx工作目录 /home/erichan/d/nginx ####docker的Dockerfile # Version: 0.0.1 FROM feuyeux/ssd MAINTAINE

docker命令别名

~/.bashrc

# .bashrc

alias d="sudo docker”

docker的nginx工作目录

/home/erichan/d/nginx

docker的Dockerfile

# Version: 0.0.1
FROM feuyeux/ssd
MAINTAINER Eric Han "feuyeux@gmail.com"
RUN apt-get update
RUN apt-get -yq install nginx
RUN mkdir -p /var/www/html
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80

nginx配置文件

nginx/global.conf

[erichan@localhost nginx]$ cat nginx/global.conf

server {
        listen          0.0.0.0:80;
    server_name     _;
    root            /var/www/html/website;
    index           index.html index.htm;
    access_log /var/log/nginx/default_access.log;
    error_log /var/log/nginx/default_error.log;
}

nginx/nginx.conf

[erichan@localhost nginx]$ cat nginx/nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;
events { }
http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
}

测试页面

[erichan@localhost nginx]$ cat website/index.html
    <head>
      <title>Test website</title>
    </head>
    <body>
      <h1>This is a test website</h1>
      <p>learning The docker book.</p>
    </body>

启动nginx服务器

d run -d -p 80 --name website -v /home/erichan/d/nginx/website:/var/www/html/website feuyeux/nginx:1.0 nginx

查看nginx进程

d ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                           NAMES
b92b30ce55b6        feuyeux/nginx:1.0   nginx               4 minutes ago       Up 4 minutes        22/tcp, 0.0.0.0:49153->80/tcp   website    

测试Nginx

[erichan@localhost nginx]$ curl http://localhost:49153
<head>
<title>Test website</title>
</head>
<body>
<h1>This is a test website</h1>    
</body>

修改本地文件

[erichan@localhost nginx]$ nano /home/erichan/d/nginx/website/index.html 
[erichan@localhost nginx]$ cat /home/erichan/d/nginx/website/index.html 
<head>
  <title>Test website</title>
</head>
<body>
  <h1>This is a test website</h1>
  <p>I'm learning The docker book.</p>
</body>

测试Docker·Nginx

[erichan@localhost nginx]$ curl http://localhost:49153
<head>
  <title>Test website</title>
</head>
<body>
  <h1>This is a test website</h1>
  <p>I'm learning The docker book.</p>
</body>

停止进程并删除容器

d kill $(d ps -q) && d rm $(d ps -a -q)

本文是《The Docker Book》的阅读笔记

六翁

目录
相关文章
|
12月前
|
应用服务中间件 网络安全 nginx
手把手教你使用 Docker 部署 Nginx 教程
本文详解Nginx核心功能与Docker部署优势,涵盖镜像拉取、容器化部署(快速、挂载、Compose)、HTTPS配置及常见问题处理,助力高效搭建稳定Web服务。
4582 4
|
缓存 负载均衡 JavaScript
Nginx:高性能Web服务器与反向代理利器
Nginx:高性能Web服务器与反向代理利器
598 110
|
缓存 负载均衡 前端开发
Nginx:高性能的Web服务器与反向代理利器
Nginx:高性能的Web服务器与反向代理利器
735 99
|
缓存 负载均衡 前端开发
Nginx:高性能Web服务器的核心引擎
Nginx:高性能Web服务器的核心引擎
404 47
|
12月前
|
应用服务中间件 Linux nginx
在虚拟机Docker环境下部署Nginx的步骤。
以上就是在Docker环境下部署Nginx的步骤。需要注意,Docker和Nginix都有很多高级用法和细节需要掌握,以上只是一个基础入门级别的教程。如果你想要更深入地学习和使用它们,请参考官方文档或者其他专业书籍。
544 5
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
652 18
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
1070 17
|
弹性计算 安全 应用服务中间件
ECS动手搭建Nginx
在ECS的centos7上搭建一个Nginx
2639 7
ECS动手搭建Nginx
|
12月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
994 10
|
12月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
961 8