容器内运行nginx其实很简单,但是一开始还是浪费了我很多时间。这里写下来给大家省点时间。

1、创建nginx文件夹,放置各种配置及日志等。

mkdir /docker/nginx

docker 文件夹是我存放所有基础设施容器的地方。

2、创建nginx配置文件

cd /docker/nginx
vim nginx.conf

nginx.conf为主配置文件

user  nginx;
worker_processes ; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; events {
worker_connections ;
} 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 ; #gzip on; include /etc/nginx/conf.d/*.conf;
}

最后一句include /etc/nginx/conf.d/*.conf;

这里的*.conf就是子配置。我只创建了一个default.conf

mkdir /docker/nginx/conf.d
cd /docker/nginx/conf.d
vim default.conf

在default.conf里面加入你的server级别的配置。

我这里只是监听了80端口,并反向代理到了5000端口

server {
listen ;
server_name localhost; #charset koi8-r;
#access_log /var/log/nginx/host.access.log main; location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:5000; # 这里设置你要代理的ip+端口
}
}

3、创建docker-compose.yml容器编排

vim /docker/nginx/docker-compose.yml

docker-compose对格式要求很严格注意里面的空格。

解释下我的设置

restart  永远重启

image 从nginx镜像拉取

ports 输出使用80,443端口

volumes 挂载外部卷到docker内部。这样就可以使用我们刚才创建好的配置了。

(这里面:ro的意思是只读的意思,表示第一第二个卷只能被容器读取不能写入)

docker-compose文件内容如下方便大家copy

version: '3.0'

services:
nginx:
restart: always
image: nginx
ports:
- :
- :
volumes:
- /docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- /docker/nginx/conf.d:/etc/nginx/conf.d:ro
- /docker/nginx/log:/var/log/nginx

4、运行

要保证在我们刚才创建的目录里

cd /docker/nginx

docker-compose up -d

加-d的意思是后台运行。大家可以试试不加 -d

5、其他

  • 如果你的nginx版本跟你的配置不一致,你可以进入到容器内看看。
docker exec -it 容器id /bin/bash
  • 停止镜像
docker-compose down

容器(docker)内运行Nginx的更多相关文章

  1. Docker中运行nginx

    Docker中运行nginx 1.Docker中运行nginx 2.配置文件 2.1 nginx.conf 2.2 default.conf 3.docker的镜像可以挂什么卷 部分内容原文地址: C ...

  2. Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

    首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...

  3. Docker 自动运行Nginx容器

    Dockerfile文件如下: FROM ubuntu #基础镜像 RUN apt-get update #更新apt RUN apt-get -y install nginx #安装nginx VO ...

  4. CentOS7安装Docker,运行Nginx镜像、Centos镜像

    摘要 总体思路:yum命令直接安装Docker,下载想要的镜像并启动 1.环境,CentOS7 Minimal 64位,Docker必须要64位的系统 2.通过yum命令直接安装,yum instal ...

  5. 阿里云服务器用Docker配置运行nginx并访问

    一.Docker拉取nginx镜像 docker pull nginx:1.12.2 这里是下载的是nginx的1.12.2版本,其他版本的镜像请访问https://hub.docker.com/r/ ...

  6. [Docker] Win10中安装Docker并运行Nginx镜像

    一.安装Docker 进入官网:https://www.docker.com/products/docker-desktop 可能需要先注册登录,很简单的. 点击 Download Desktop f ...

  7. Docker中运行nginx并挂载本地目录到镜像中

    1.1 从hup上pull镜像1.2 创建将要挂载的目录1.3 先要有配置文件才能启动容器1.3.1 vim /data/nginx/conf/nginx.conf1.3.2 vim /data/ng ...

  8. centos7 搭建docker内运行rabbitmq,然后再镜像ha方案的完全教程,暂时一个宿主机只能运行一个docker的rabbitmq,但是集群 ha都正常

    1.安装centos7.x,配置好网络2.因为docker需要比较高版本的内核,比如使用overlayfs作为默认docker文件系统要3.18,所以先升级内核到3.18以上版本,能直接过4是最佳了检 ...

  9. 最简单的docker教程:在docker里运行nginx服务器

    命令行docker search nginx搜索名为nginx的docker image,返回结果的第一个,github上有10293个star,这就是我们想要搜索的结果: 使用命令docker pu ...

随机推荐

  1. 下载python包

    修改pip下载源 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider 更新pip版本 python -m pip inst ...

  2. requests 爬虫

    爬虫 常用爬虫爬取网页,但如果一直爬取会被ban掉,因此需要对爬虫进行一些改进反反爬 使用requests和beautifulsoup4构建爬虫,1.随机user-agent:2.ip代理:4.coo ...

  3. Linux第十节课学习笔记

    部署LVM三步: 1.pv:使设备支持LVM: 2.vg:对支持LVM的设备进行整合: 3.lv:将整合的空间进行切割. 每个基本单元PE的大小为4M,分配空间必须是4M的整数倍.可以容量或基本单元个 ...

  4. python中防止字符串转义

    有学生问了个问题,试了好多办法都不行,搜也搜不到,只能自己尝试了,顺利解决. 问题描述: 如果一个字符串包含转义字符,如“adfdfasd\tfdsadf\t”,正常print会将\t看作转义字符ta ...

  5. MySQL 5.6 以上版本支持三种sql_mode模式:ANSI、TRADITIONAL和STRICT_TRANS_TABLES。

    Field 'id' doesn't have a default value问题解决方法 运维的名义关注0人评论3323人阅读2018-01-23 17:37:42   MySQL 5.0 以上版本 ...

  6. pycrypto安装出错的问题 intmax_t C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.

    前言: 需要安装 Microsoft Visual Studio2017 Community 或者buildtools 解决方案一(解决了python3.7上安装pycrypto-2.6.1的错误问题 ...

  7. vue 路由配置

    1.不带参数的路由配置 及 跳转 //路由配置: { name: "a", path: "/a", component: a }   页面跳转: this.$r ...

  8. Ubuntu16.04更新源

    首先说说为什么要更新源,我是在docker容器中修改配置文件时有所需要,要用到vim,但是会报错.找不到需要的包. 网上都会说要先更新:apt-get update 但是超级慢有没有,我更新了4小时, ...

  9. 【C++】类中this指针的理解

    转自 苦涩的茶https://www.cnblogs.com/liushui-sky/p/5802981.html C++类中this指针的理解 先要理解class的意思.class应该理解为一种类型 ...

  10. Virtualbox扩展硬盘

    动态调整 1.VBoxManage showhdinfo win10.vdi 2.VBoxManage modifyhd win10.vdi --resize 61200 3.磁盘管理器中分配空间并格 ...