在查看Docker的nginx镜像的配置文件时,展示自定义页面

例如,当阅读Docker for Mac的文档时,您很快会遇到nginx的示例。

docker run -d -p 80:80 --name webserver nginx

这样一来,只要访问本地主机,就会显示“Welcome to nginx”页面。

顺便问一下,nginx 的配置文件在哪里,里面写了些什么呢?对于初学者来说,他们会想知道。

因此,在审核配置文件的同时,我们将提供一个名为”docker + nginx简单示例”的示例页面,例如piyo.html。

请确认容器内的设置文件。

# コンテナを立ち上げます
$ docker run -d -p 80:80 --name webserver nginx

# コンテナ内に入ります。
$ docker exec -it webserver /bin/bash

之后,您可以通过猫等方式来观看。

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;
}
server {
    listen       80;
    server_name  localhost;

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

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

展示自己的页面

根据上述的配置文件,可以得知 location 被设置为 /usr/share/nginx/html,所以我想把 html 文件放在这里。

请参考以下链接:https://hub.docker.com/_/nginx/

准备文件

# 作業
$ mkdir workspace; cd $_
$ touch Dockerfile
$ mkdir html; cd $_
$ touch index.html piyo.html
$ cd ..


# 確認
mochizuki ~/workspace
$ tree
.
├── Dockerfile
└── html
    ├── index.html
    └── piyo.html

1 directory, 3 files
FROM nginx
COPY ./html /usr/share/nginx/html
<html>
<body>
<h1>Piyo</h1>
</body>
</html>

只是在 Dockerfile 中添加了 COPY 行。

执行命令

$ cd ~/workspace

# ------------------------ 
# まずは、イメージの作成
# ------------------------ 

$ docker build --tag simple-nginx .
# Sending build context to Docker daemon 4.096 kB
# Step 1/2 : FROM nginx
#  ---> cc1b61406712
# Step 2/2 : COPY ./html /usr/share/nginx/html
#  ---> 68e7ceff0e0a
# Removing intermediate container 82d84fb20caf
# Successfully built 68e7ceff0e0a

# ------------------------ 
# イメージの確認
# ------------------------ 

$ docker images -a simple-nginx
# REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
# simple-nginx        latest              68e7ceff0e0a        56 seconds ago      182 MB

# ------------------------ 
# コンテナ立ち上げ & 確認
# ------------------------ 

$ docker run -d --name simple-nginx-container -p 80:80 simple-nginx
# d2f9613a55cf8c39628f319f0e23777ecd1e1a69554c094bf765a1a7c0c5015a

$ docker ps
# CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
# d2f9613a55cf        simple-nginx        "nginx -g 'daemon ..."   5 seconds ago       Up 4 seconds         0.0.0.0:80->80/tcp, 443/tcp   simple-nginx-container

当您访问 localhost/piyo.html 时,页面将会显示。

スクリーンショット 2017-02-05 13.32.24.png

这就是全部了。虽然在实现相同效果方面还有其他方法,比如在容器启动时使用 -v 进行处理,但我只提供了一个示例供参考。

非常感谢您。

赠品

我尝试使用docker-compose来做同样的事情。

$ tree
.
├── containers
│   └── nginx
│       └── Dockerfile
├── docker-compose.yml
└── public
    ├── index.html
    └── piyo.html
version: '3'
services:
  nginx:
    build: ./containers/nginx
    ports:
      - "80:80"
    volumes:
      - ./public:/usr/share/nginx/html
FROM nginx
$ docker-compose up

接下来,我们可能会使用docker-compose来安装php-fpm和mysql以组装起来。

bannerAds