Nginx部署
1.获取镜像
# 拉取镜像 此处我们拉取的是官方最新镜像,其它版本可以去DockerHub查询
[root@VM-24-9-centos ~]# docker pull nginx
2.创建容器
# 创建容器 第一个nginx是容器名,第二个nginx是镜像名
[root@VM-24-9-centos ~]# docker run -d -p 80:80 --name nginx nginx
3.把容器内的配置文件等复制到容器外用于挂载
# nginx的配置文件日志文件及默认的页面分别放于容器内的 /etc/nginx /usr/share/nginx/html /var/log/nginx 中,我们需要将其挂载到容器外部
# 创建三个文件夹 conf html logs
[root@VM-24-9-centos data]# mkdir -p /data/nginx/{conf.d,html,logs}
# 将容器内的 nginx.conf配置文件和default.conf配置文件复制出来
[root@VM-24-9-centos data]# docker cp nginx:/usr/share/nginx/html /data/nginx
[root@VM-24-9-centos data]# docker cp nginx:/etc/nginx/nginx.conf /data/nginx
[root@VM-24-9-centos data]# docker cp nginx:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/default.conf
# 查看目录结构
[root@VM-24-9-centos nginx]# cd /data/nginx
[root@VM-24-9-centos nginx]# ll
total 16
drwxr-xr-x 2 root root 4096 Nov 16 10:48 conf.d
drwxr-xr-x 2 root root 4096 Nov 16 10:48 html
drwxr-xr-x 2 root root 4096 Nov 16 10:48 logs
-rw-r--r-- 1 root root 648 Nov 2 23:01 nginx.conf
4.删除之前的容器,然后创建新的容器把目录挂载上去
# 停止容器
[root@VM-24-9-centos nginx]# docker stop nginx
nginx
# 删除容器
[root@VM-24-9-centos nginx]# docker rm nginx
nginx
# 创建新的容器 --privileged=true 容器内部对挂载的目录拥有读写等特权
docker run -d -p 9200:9200 --name nginx_9200 \
-v /data/nginx/html:/usr/share/nginx/html \
-v /data/nginx/logs:/var/log/nginx \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
--privileged=true \
nginx
5.访问nginx查看是否创建成功
[root@VM-24-9-centos ~]# curl http://x.x.x.x
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>