我尝试使用Docker For Windows在docker hub上使用nginx镜像来创建容器
首先
利用环境的惊人特性
-
- Windows10
-
- Docker For Windows
- Windows PowerShell
本文所述之事/本文中所述的事项/本文要讨论的主题/本文要谈到的内容
- DockerHubのHOW TOに沿ってNginxのイメージからコンテナを作成する
在本文中不提及的事项。
- Docker For Windowsのインストール方法(別の記事を参照)
访问DockerHub网站

如何要点摘要
-
- 什么是nginx?
-
- 如何使用这个映像文件
-
- 托管一些简单的静态内容
-
- 不使用DockerFile的方法
-
- 使用DockerFile的方法
-
- 暴露外部端口的方法
-
- 复杂配置
-
- 在nginx配置中使用环境变量(1.19版本中新增)
-
- 以只读模式运行nginx
-
- 以调试模式运行nginx
-
- 入口点静默日志
-
- 用户和组ID
-
- 以非root用户身份运行nginx
- 图像变体
1. Nginx是什么?
Nginx (「engine-x」と発音) は、HTTP、HTTPS、SMTP、POP3、および IMAP プロトコル用のオープン ソースのリバース プロキシ サーバーであり、ロード バランサー、HTTP キャッシュ、および Web サーバー (オリジン サーバー) も備えています。nginx プロジェクトは、高い同時実行性、高いパフォーマンス、および低いメモリ使用量に重点を置いて開始されました。これは 2 条項の BSD ライク ライセンスに基づいてライセンスされており、Linux、BSD バリアント、Mac OS X、Solaris、AIX、HP-UX、およびその他の *nix フレーバーで動作します。また、Microsoft Windows の概念実証ポートもあります。
这张图片该如何使用?
2-1. 通过托管一些简单的静态内容来生成容器。
2-1-1. 不使用 DockerFile 的方式
我将使用PowerShell执行命令。
$ docker run --name some-nginx -v /some/content:/usr/share/nginx/html:ro -d nginx
[結果]
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
31b3f1ad4ce1: Pull complete
fd42b079d0f8: Pull complete
30585fbbebc6: Pull complete
18f4ffdd25f4: Pull complete
9dc932c8fba2: Pull complete
600c24b8ba39: Pull complete
Digest: sha256:0b970xxxxxxxxxxxxxxxxxxxxxxxxxxx63516b188318682bxxxxxxxxxxx591189ff1
Status: Downloaded newer image for nginx:latest
8dad6526dd42a5738xxxxxxxxxxxxxxxxxxxxxxxxxxxx187f76b2f4dad61bc6944478



2-1-2. 使用 DockerFile 的方法
在C盘根目录下创建一个任意的文件夹(此处命名为docker),
按照以下目录结构创建文件夹。
C:docker
└─static-html-directory
此外,创建文件时使用名为”Dockerfile”(无需指定文件扩展名)。
C:docker
├─Dockerfile
└─static-html-directory
编辑文件的内容按照官方页面上指定的方式进行。
FROM nginx
COPY static-html-directory /usr/share/nginx/html
执行以下命令来构建Dockerfile的内容。
> cd C:\docker
> docker build -t some-content-nginx .
[結果]
[+] Building 2.1s (8/8) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 31B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/nginx:latest 1.9s
=> [auth] library/nginx:pull token for registry-1.docker.io 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 43B 0.0s
=> [1/2] FROM docker.io/library/nginx@sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1 0.0s
=> CACHED [2/2] COPY static-html-directory /usr/share/nginx/html 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:ebec1a79757b1279add2b67063626d1d88b3a6f5fbafa3b08ec63f0624afbb45 0.0s
=> => naming to docker.io/library/some-content-nginx 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

使用追加的图像来创建容器。
> docker run --name some-nginx -d some-content-nginx

接下来,我们将尝试使用指定端口的容器创建方法,请先删除已创建的容器。
2-2. 暴露外部端口(指定端口的方法)
> docker run --name some-nginx -d -p 8080:80 some-content-nginx

由于容器在端口8080上创建,因此尝试访问“localhost:8080”。

确认端口配置没有问题。
接下来,我们尝试另一种容器创建方法,因此请先删除已创建的容器和镜像。
2-3. 复杂配置(生成容器时的复杂配置)
設定ファイルをカスタマイズしてコンテナを生成する方法です。
まずは、設定ファイルをデフォルトのnginxからコピーして取得します。
下記のコマンドで、nginxのコンテナを作成して、設定ファイルをコピーします。
※設定ファイルは「C:\docker」に配置します。このディレクトリは任意です。
> docker run --name tmp-nginx-container -d nginx
> docker cp tmp-nginx-container:/etc/nginx/nginx.conf C:\docker/nginx.conf
> docker rm -f tmp-nginx-container
設定ファイルが作成できました。(今回はデフォルトのまま使用します。)
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
}
作成した設定ファイルを利用して、コンテナを作成します。
> docker run --name my-custom-nginx-container -v C:\docker/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
