# 用 Docker 调试 Nginx

- Link: https://www.ixiqin.com/2021/01/29/with-the-docker-debugging-nginx/
- Published: 2021-01-29T00:46:29+08:00
- Author: 白宦成

文章目录

 [Toggle](https://www.ixiqin.com/2021/01/29/with-the-docker-debugging-nginx/#)

 * [Requirements](https://www.ixiqin.com/2021/01/29/with-the-docker-debugging-nginx/#requirements)
 * [实现思路](https://www.ixiqin.com/2021/01/29/with-the-docker-debugging-nginx/#%e5%ae%9e%e7%8e%b0%e6%80%9d%e8%b7%af)
 * [具体操作](https://www.ixiqin.com/2021/01/29/with-the-docker-debugging-nginx/#%e5%85%b7%e4%bd%93%e6%93%8d%e4%bd%9c)
    - [启动容器并测试效果](https://www.ixiqin.com/2021/01/29/with-the-docker-debugging-nginx/#%e5%90%af%e5%8a%a8%e5%ae%b9%e5%99%a8%e5%b9%b6%e6%b5%8b%e8%af%95%e6%95%88%e6%9e%9c)

容器技术被广泛应用在各种场景，在实际的应用过程中，我们也可以根据自己的需要，进行
各种配置。我最近因为在调试 Nginx ，因此，也使用 Docker 来调试 Nginx。

## Requirements

 * 已经安装 Docker
 * 安装了 docker-compose

## 实现思路

docker-compose 可以帮助我们把一些 Docker 启动的配置给简化，通过编写配置文件，简化
启动容器的命令。

## 具体操作

创建一个项目，并在项目的根目录中放置 `docker-compose.yml` 以及 `nginx.conf`。其中，`
nginx.conf` 是你需要测试的 nginx 文件，`docker-compose.yml` 则是用来记录你的 Docker
容器启动配置。

![文件结构](http://static.zybuluo.com/xiqingongzi/socao1p3eexp3hpln8zmug03/image.
png)

### 启动容器并测试效果

将你需要测试的配置文件内容放置在 nginx.conf 中，并在dockcer-compose.yml 中添加如下
内容

    ```javascript
    web:
      image: nginx
      ports:
        - "8080:80"
      volumes:
        - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
      command: [nginx-debug, '-g', 'daemon off;']
    Code language: JavaScript (javascript)
    ```

随后，执行 `docker-compose up` 就可以启动容器，并展示出容器的运行效果

![执行效果](https://postimg.aliavv.com/mbp2021/yz92m.png)

此时，你就可以访问 `localhost:8080` 来查看你自己的配置了。
