目 录CONTENT

文章目录

docker 使用数据卷部署nginx容器

ZiChen D
2021-12-08 / 0 评论 / 0 点赞 / 288 阅读 / 4,396 字 / 正在检测是否收录...

使用数据卷部署nginx

创建目录

分别创建两个目录用来存放配置文件和网页内容

[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# mkdir -p /config

上传下载好的html文件包

[root@localhost ~]# ls
38345  anaconda-ks.cfg

//将文件包下的文件移动到/var/www/html/下
[root@localhost ~]# cd 38345/
[root@localhost 38345]# ls
audio  css  images  index.html  js  vendor
[root@localhost 38345]# mv * /var/www/html/
[root@localhost 38345]# cd /var/www/html/
[root@localhost html]# ls
audio  css  images  index.html  js  vendor

安装nginx

[root@localhost ~]# yum -y install nginx

//将配置文件复制到/config目录下
[root@localhost nginx]# pwd
/etc/nginx
[root@localhost nginx]# ls
conf.d        fastcgi.conf.default    koi-utf     mime.types.default  scgi_params          uwsgi_params.default
default.d     fastcgi_params          koi-win     nginx.conf          scgi_params.default  win-utf
fastcgi.conf  fastcgi_params.default  mime.types  nginx.conf.default  uwsgi_params

[root@localhost nginx]# cp -r * /config/
[root@localhost nginx]# cd /config/
[root@localhost config]# ls
conf.d        fastcgi.conf.default    koi-utf     mime.types.default  scgi_params          uwsgi_params.default
default.d     fastcgi_params          koi-win     nginx.conf          scgi_params.default  win-utf
fastcgi.conf  fastcgi_params.default  mime.types  nginx.conf.default  uwsgi_params

创建数据卷容器

[root@localhost ~]# docker run --name nginx1 -v /var/www/html/:/usr/share/nginx/html busybox

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
e4f6f0030688   busybox    "sh"          42 seconds ago   Exited (0) 40 seconds ago               nginx1
bb9283a9d50b   centos:8   "/bin/bash"   11 hours ago     Exited (255) 24 minutes ago             httpd

再创建一个数据卷存放配置文件

[root@localhost ~]# docker run --volumes-from nginx1 -v /config/:/etc/nginx --name nginx_conf  busybox

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS                        PORTS     NAMES
182d7377105b   busybox    "sh"          14 seconds ago   Exited (0) 13 seconds ago               nginx_conf
e4f6f0030688   busybox    "sh"          3 minutes ago    Exited (0) 3 minutes ago                nginx1
bb9283a9d50b   centos:8   "/bin/bash"   11 hours ago     Exited (255) 27 minutes ago             httpd

创建容器测试

查看nginx_conf这太主机是否有配置文件和网页内容

[root@localhost ~]# docker run -it --rm --volumes-from nginx_conf  busybox 
/ # ls /etc/nginx/
conf.d                  fastcgi_params          mime.types              scgi_params             win-utf
default.d               fastcgi_params.default  mime.types.default      scgi_params.default
fastcgi.conf            koi-utf                 nginx.conf              uwsgi_params
fastcgi.conf.default    koi-win                 nginx.conf.default      uwsgi_params.default
/ # ls /usr/share/nginx/html/
audio       css         images      index.html  js          vendor

创建nginx镜像

[root@localhost ~]# docker run -itd --name web --volumes-from nginx_conf -P nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e5ae68f74026: Pull complete 
21e0df283cd6: Pull complete 
ed835de16acd: Pull complete 
881ff011f1c9: Pull complete 
77700c52c969: Pull complete 
44be98c0fab6: Pull complete 
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest
0495bd956b9aa3cb977fa27875c34cae8164cbac07f638ec2f1c40b57ad967c7

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                     NAMES
0495bd956b9a   nginx     "/docker-entrypoint.…"   9 minutes ago   Up 9 minutes   0.0.0.0:49153->80/tcp, :::49153->80/tcp   web

[root@localhost ~]# docker exec -it web /bin/bash
root@0495bd956b9a:/# ls /etc/nginx/
conf.d	      fastcgi.conf.default    koi-utf	  mime.types.default  scgi_params	   uwsgi_params.default
default.d     fastcgi_params	      koi-win	  nginx.conf	      scgi_params.default  win-utf
fastcgi.conf  fastcgi_params.default  mime.types  nginx.conf.default  uwsgi_params

root@0495bd956b9a:/# ls /usr/share/nginx/html/
audio  css  images  index.html	js  vendor

在浏览器上用IP加映射过来的49153端口访问试试:

访问成功!

0

评论区