docker安装Nginx
查找、安装镜像
查找镜像 docker search nginx
安装镜像 docker pull nginx
指定版本安装 docker pull nginx:1.17
简单的启动Nginx容器
docker run --name nginx1 -p 10000:80 -d nginx
docker run --name nginx2 -p 10001:80 -d nginx
参数 | 说明 |
---|---|
- -name | 设置容器名,要唯一 |
-d | 让容器后台运行 |
-p | 端口映射 本地端口:容器端口 |
会生成一长串的字符串,这个是容器的长 ID,一般可作为日志的文件名。
因为容器的特性,所以你可以启动多个nginx,启动多个确保 --name 的名字不一样,可以通过
docker ps -a
来查看所有容器,运行中的和未运行的,如果需要删除相应的容器,需要先将容器停止docker stop nginx
然后删除docker rm nginx
进入nginx容器
docker exec -it 容器ID名 或 容器名 /bin/bash
然后就跟操作 Linux 基本一样的了。
挂载数据卷
创建数据卷 | 挂载目录主机
# 创建三个数据卷
docker volume create www
docker volume create logs
docker volume create conf
# 创建三个本地目录
mkdir -p nginx1/www nginx1/logs nginx1/conf nginx1/conf/conf.d
-
www
nginx 容器配置的虚拟目录 -
logs
nginx 容器的日志目录 -
conf
nginx 容器的配置文件目录
部署配置文件
-
从容器中复制配置文件,容器要先运行起来才行啊
docker cp nginx:/etc/nginx/nginx.conf ~/nginx/conf
-
从其他地方复制过来配置文件,略
部署
使用数据卷
# 启动容器
docker run -d -p 10000:80 --name nginx1 --mount source=www,target=/usr/share/nginx/html --mount source=logs,target=/var/log/nginx --mount source=conf,target=/etc/nginx/conf.d --mount type=bind,source=/root/nginx1/conf/nginx.conf,target=/etc/nginx/nginx.conf nginx
# 访问
http://192.168.174.130:10000/
查看数据卷信息 docker volume inspect www
可以找到数据卷的位置信息 /var/lib/docker/volumes/www/_data
- 修改首页文件
进入该文件中,修改
index.html
文件内容后,再次访问,查看页面内容是否改变
使用挂载主机目录
和使用数据类似
docker run -d -p 10000:80 --name nginx1 --mount type=bind,source=/root/nginx1/www,target=/usr/share/nginx/html --mount type=bind,source=/root/nginx1/logs,target=/var/log/nginx --mount type=bind,source=/root/nginx1/conf/nginx.conf,target=/etc/nginx/nginx.conf nginx
其他
如果要重新载入 NGINX 可以使用以下命令发送 HUP 信号到容器: docker kill -s HUP nginx
重启 NGINX 容器命令: docker restart nginx