-
查找Docker Hub 上的 nginx 镜像
[root@docker ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 11601 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1619 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 723 [OK]
docker.io docker.io/bitnami/nginx Bitnami nginx Docker Image 67 [OK]
docker.io docker.io/linuxserver/nginx An Nginx container, brought to you by Linu... 64
docker.io docker.io/tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rt... 47 [OK]
docker.io docker.io/nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 20
docker.io docker.io/schmunk42/nginx-redirect A very simple container to redirect HTTP t... 17 [OK]
- 拉去官方的镜像
[root@docker ~]# docker pull nginx
[root@docker ~]# docker images | grep nginx
docker.io/nginx latest 719cd2e3ed04 2 weeks ago 109.2 MB
- 使用默认配置来启动 Nginx 容器实例
[root@docker ~]# docker run --name zh-nginx-test -p 8081:80 -d nginx
34334551cd6e696785810c77c37f3a1e4a6c21d427d6b559b9b9384add60144c
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34334551cd6e nginx "nginx -g 'daemon off" 8 seconds ago Up 5 seconds 0.0.0.0:8081->80/tcp zh-nginx-test
zh-nginx-test
容器名称。-d
设置容器在在后台一直运行。-p
端口进行映射,将本地 8081 端口映射到容器内部的 80 端口。- 执行以上出现一长串字符,表示容器的ID,一般作为日志的文件名。
- 通过地址访问查看是否安装成功
[root@docker ~]# elinks 127.0.0.1:8081 --dump
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.
Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
Nginx 部署
- 创建目录 nginx, 用于存放后面的相关东西
[root@docker ~]# mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
- 拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录
[root@docker ~]# docker cp 34334551cd6e:/etc/nginx/nginx.conf ~/nginx/conf
- www: 目录将映射为 nginx 容器配置的虚拟目录。
- logs: 目录将映射为 nginx 容器的日志目录。
- conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
部署命令
[root@docker ~]# docker run -d -p 8082:80 --name nginx-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
0f17a5a182e68639945d252467ddbb3902dec1d167ba041d7e655c81ad3a0238
命令说明:
-
-p 8082:80: 将容器的 80 端口映射到主机的 8082 端口。
-
--name nginx-web:将容器命名为 nginx-web。
-
-v ~/nginx/www:/usr/share/nginx/html:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。
-
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
-
-v ~/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。
- 启动以上命令后进入 ~/nginx/www 目录:
[root@docker ~]# cd nginx/www/
- 创建 index.html 文件,内容如下:
[root@docker www]# vim index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试</title>
</head>
<body>
<h1> Welcome you ! </h1>
<p> 测试成功 !</p>
</body>
</html>
相关命令
- 如果要重新载入 NGINX 可以使用以下命令发送 HUP 信号到容器:
[root@docker ~]# docker kill -s HUP container-name
- 重启 NGINX 容器命令:
[root@docker ~]# docker restart container-name