• [Docker]三.Docker 部署nginx,以及映射端口,挂载数据卷


    一.Docker 部署 Nginx 以及端口映射

    Docker 部署 Nginx,首先需要下载nginx镜像,然后启动这个镜像,就运行了一个nginx的容器了

    1.下载 nginx 镜像并启动容器

    1. #查看是否存在nginx镜像:发现没有nginx镜像
    2. [root@localhost zph]# docker images | grep nginx
    3. #下载nginx镜像
    4. [root@localhost zph]# docker pull nginx
    5. Using default tag: latest
    6. latest: Pulling from library/nginx
    7. a378f10b3218: Pull complete
    8. 5b5e4b85559a: Pull complete
    9. 508092f60780: Pull complete
    10. 59c24706ed13: Pull complete
    11. 1a8747e4a8f8: Pull complete
    12. ad85f053b4ed: Pull complete
    13. 3000e3c97745: Pull complete
    14. Digest: sha256:add4792d930c25dd2abf2ef9ea79de578097a1c175a16ab25814332fe33622de
    15. Status: Downloaded newer image for nginx:latest
    16. docker.io/library/nginx:latest
    17. #再次查看是否存在nginx镜像:发现下载成功了nginx镜像
    18. [root@localhost zph]# docker images
    19. REPOSITORY TAG IMAGE ID CREATED SIZE
    20. nginx latest 593aee2afb64 22 hours ago 187MB
    21. #启动nginx容器,在后台运行,不用加/bin/bash命令,因为nignx底层会执行相关命令
    22. [root@localhost zph]# docker run -it -d --name mynginx 593aee2afb64
    23. e0773deea60048d82729038535fad45308204e181da6c7a35fb44090cc552077
    24. #查看启动的容器:发现nginx已经启动
    25. [root@localhost zph]# docker ps
    26. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    27. e0773deea600 593aee2afb64 "/docker-entrypoint.…" 5 seconds ago Up 2 seconds 80/tcp mynginx
    28. [root@localhost zph]#

    启动nginx容器成功后,这样就创建了一个web服务了,验证这个容器是否成功: 进入nginx容器访问url,验证nginx

    1. #进入nginx容器访问url,验证nginx
    2. [root@localhost zph]# docker exec -it e0773deea600 /bin/bash
    3. #curl 验证是否可以打印出nginx相关html:发现可以打印,说明nginx容器创建成功
    4. root@e0773deea600:/# curl 127.0.0.1
    5. <head>
    6. Welcome to nginx!
    7. Welcome to nginx!

    8. If you see this page, the nginx web server is successfully installed and

    9. working. Further configuration is required.

    10. For online documentation and support please refer to

    11. Commercial support is available at
    12. Thank you for using nginx.

    13. root@e0773deea600:/#

    nginx容器创建成功后,那么怎么在外部访问这个容器的url呢?

    2. -p 映射端口

    想在外部访问容器里面的 nginx 这个时候就需要映射端口

    可以通过 -p 参数来映射端口,语法:

            docker run -p 容器外端口:容器内端口 容器名/容器ID

    1. #查看运行的容器
    2. [root@localhost zph]# docker ps
    3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    4. e0773deea600 593aee2afb64 "/docker-entrypoint.…" 15 minutes ago Up 15 minutes 80/tcp mynginx
    5. #查看镜像
    6. [root@localhost zph]# docker images | grep nginx
    7. nginx latest 593aee2afb64 23 hours ago 187MB
    8. # 再运行一个nginx容器(一个镜像可以运行多个容器),并进行端口映射,让外部也可以访问nginx容器中的html
    9. [root@localhost zph]# docker run -it -d --name mynginx_port -p 81:80 593aee2afb64
    10. 36bf218c9754446319299c61f919d54aeebf0d3734ecc0de56183972ea7d6614
    11. #查看运行的容器:发现mynginx_port容器已在运行了
    12. [root@localhost zph]# docker ps
    13. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    14. 36bf218c9754 593aee2afb64 "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx_port
    15. e0773deea600 593aee2afb64 "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 80/tcp mynginx
    16. #在linux系统中访问nginx中的html:发现成功访问了
    17. [root@localhost zph]# curl 127.0.0.1:81
    18. <head>
    19. Welcome to nginx!
    20. Welcome to nginx!

    21. If you see this page, the nginx web server is successfully installed and

    22. working. Further configuration is required.

    23. For online documentation and support please refer to

    24. Commercial support is available at
    25. Thank you for using nginx.

    26. #查看当前linux对外的ip:发现是ens33中的:192.168.0.6
    27. [root@localhost zph]# ip addr
    28. 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    29. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    30. inet 127.0.0.1/8 scope host lo
    31. valid_lft forever preferred_lft forever
    32. inet6 ::1/128 scope host
    33. valid_lft forever preferred_lft forever
    34. 2: ens33: mtu 1500 qdisc fq_codel state UP group default qlen 1000
    35. link/ether 00:0c:29:c3:3d:27 brd ff:ff:ff:ff:ff:ff
    36. inet 192.168.0.6/24 brd 192.168.0.255 scope global dynamic noprefixroute ens33
    37. valid_lft 171516sec preferred_lft 171516sec
    38. inet6 fe80::20c:29ff:fec3:3d27/64 scope link noprefixroute
    39. valid_lft forever preferred_lft forever
    40. #在linux中,通过192.168.0.6本地ip访问发现访问成功
    41. [root@localhost zph]# curl 192.168.0.6:81
    42. <head>
    43. Welcome to nginx!
    44. Welcome to nginx!

    45. If you see this page, the nginx web server is successfully installed and

    46. working. Further configuration is required.

    47. For online documentation and support please refer to

    48. Commercial support is available at
    49. Thank you for using nginx.

    50. [root@localhost zph]#

    二.Docker 部署 Nginx 挂载数据卷

    1.-v 指定路径挂载数据卷

    挂载数据卷后,当容器外的数据发生变化,容器内的数据也会跟随变化,当容器内的数据发生变化后,也会同步到容器外
    基本语法:
            docker run -v 容器外目录 : 容器内目录 容器 ID
    1. #查看镜像
    2. [root@localhost www]# docker images
    3. REPOSITORY TAG IMAGE ID CREATED SIZE
    4. nginx latest 593aee2afb64 37 hours ago 187MB
    5. #端口映射以及挂载数据
    6. [root@localhost www]# docker run -it -d --name mynginx_pv -p 82:80 -v /var/www/:/usr/local/nginx/html 593aee2afb64
    7. ce47906a57afb7c24a1847211f60e7d778e86a2e0b3c65bbf2bed7cb48123ad8
    8. #查看var/www下文件
    9. root@localhost www]# ll
    10. 总用量 12
    11. -rw-r--r-- 1 root root 5 10月 26 07:37 index.html
    12. -rw-r--r-- 1 root root 10 10月 25 07:54 test2.txt
    13. -rw-r--r-- 1 root root 12 10月 25 07:51 test.txt
    14. #查看运行的容器
    15. [root@localhost www]# docker ps
    16. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    17. ce47906a57af 593aee2afb64 "/docker-entrypoint.…" 45 seconds ago Up 42 seconds 0.0.0.0:82->80/tcp, :::82->80/tcp mynginx_pv
    18. #进入容器
    19. [root@localhost www]# docker exec -it ce47906a57af /bin/bash
    20. #进入挂载的数据目录
    21. root@ce47906a57af:/# cd /usr/local/nginx/html
    22. #查看文件
    23. root@ce47906a57af:/usr/local/nginx/html# ls
    24. index.html test.txt test2.txt
    25. #删除一个文件
    26. root@ce47906a57af:/usr/local/nginx/html# rm -rf test.txt
    27. root@ce47906a57af:/usr/local/nginx/html# ls
    28. index.html test2.txt
    29. #退出
    30. root@ce47906a57af:/usr/local/nginx/html# exit
    31. #查看外面数据文件是否被删除:发现已经被删除了,操作成功
    32. [root@localhost www]# ll
    33. 总用量 8
    34. -rw-r--r-- 1 root root 5 10月 26 07:37 index.html
    35. -rw-r--r-- 1 root root 10 10月 25 07:54 test2.txt

    2.docker inspect 命令查看容器运行的细节

    查看详细的映射关系

    [root@localhost wwwroot]# docker inspect 3408e7d7a430

    3.-v 匿名挂载数据卷

    基本语法:

            docker run -v 容器内目录 容器 ID

    -P 表示随机映射端口

    1. docker run -d -P --name nginx01 -v /etc/nginx nginx
    2. [root@localhost ~]# docker run -d -P --name nginx01 -v /etc/nginx nginx
    3. e6aea0a9c58cf4f31ce0ebdd43b071845ba5b1ee772949a5cf02da5f40e2245c
    4. [root@localhost ~]# docker ps -a
    5. CONTAINER ID IMAGE COMMAND CREATED STATU
    6. S PORTS NAMES
    7. e6aea0a9c58c nginx "/docker-entrypoint.…" 2 seconds ago Up 1 second
    8. 0.0.0.0:49155->80/tcp, :::49155->80/tcp nginx01

     查看挂载详情

    1. [root@localhost ~]# docker volume ls
    2. DRIVER VOLUME NAME
    3. local 3f55b08bbafb39845c98efe1cf18dfa7fedea21c2fad24cb14759dced3872ba2
    4. local 4de46af05120a375cb5270af59790f432c64758517352963f55e67cd3bfa56d8
    5. local 5a8a57ea0fe775429577c451d23687f76634b5ee845c95e6e30a099b888ac2c1
    6. local 5c97447224de838eab94272de4cb556cea8e79a4469b618c00cba1b826e0d37e
    7. local 6b8cfbcf9727dc2c3f404cf9d830de7971fe7d46a56c5a0a42222182917fd6ae
    8. local 6b9adae890deb91efcf5a52913f6ab61d8c22a83c2130fc21c9a3f44f32919
    9. [root@localhost ~]# docker inspect e6aea0a9c58c | grep volume
    10. "Type": "volume", "Source": "/var/lib/docker/volumes/3f55b08bbafb39845c98efe1cf18dfa7fed
    11. ea21c2fad24cb14759dced3872ba2/_data

    4.-v 具名挂载数据卷

    基本语法:

            docker run -v 卷名称:容器内目录 容器 ID

    1. docker run -d -P --name nginx01 -v juming:/etc/nginx nginx
    2. [root@localhost ~]# docker ps -a
    3. CONTAINER ID IMAGE COMMAND CREATED STATU
    4. S PORTS NAMES
    5. 57764c6d6901 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds
    6. 0.0.0.0:49156->80/tcp, :::49156->80/tcp nginx01
    7. [root@localhost ~]# docker volume ls
    8. DRIVER VOLUME NAME
    9. local 3f55b08bbafb39845c98efe1cf18dfa7fedea21c2fad24cb14759dced3872ba2
    10. local fdf7b0c7847f62ffe2326ef8a11b746efbb8b9fd212970cd31ccca5385fc9865
    11. local juming
    12. 查看挂载详情
    13. [root@localhost ~]# docker volume inspect juming
    14. [
    15. { "CreatedAt": "2021-07-28T05:09:13-04:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming/_data",
    16. Name": "juming", "Options": null, "Scope": "local"
    17. }
    18. ]

     5.Docker 运行容器传递环境变量

    语法:
            docker run -e 变量名 = 变量值 容器 ID
             printenv: 打印出环境变量
    1. #传递一个环境变量
    2. [root@localhost www]# docker run --rm --name centos8_env -e TEST_DOMAIN=www.xxx.com 5d0da3dc9764 printenv
    3. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    4. HOSTNAME=1d078b1e2069
    5. TEST_DOMAIN=www.xxx.com
    6. HOME=/root
    7. #传递多个环境变量
    8. [root@localhost www]# docker run --rm --name centos8_env -e TEST_DOMAIN=www.xxx.com -e TEST_ADDR=192.68.1.111 5d0da3dc9764 printenv
    9. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    10. HOSTNAME=1c3d6eaef5b7
    11. TEST_DOMAIN=www.xxx.com
    12. TEST_ADDR=192.68.1.111
    13. HOME=/root

    6.Docker 容器内安装软件

    Docker 容器内安装软件和linux 中安装软是一样的,Centos 系列yum安装,Ubuntu 系列apt-get 安装

    1. #进入容器
    2. [root@localhost ~]# docker exec -it centos_v /bin/bash
    3. #查看
    4. [root@095f45520b1e /]# ls
    5. bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin
    6. srv sys tmp usr var
    7. #使用ifocnfig容器ip:发现没有ifconfig包以及命令
    8. [root@095f45520b1e /]# ifconfig
    9. bash: ifconfig: command not found
    10. #安装net-tools
    11. [root@095f45520b1e /]#yum install -y net-tools
    12. #查看容器的 ip
    13. [root@095f45520b1e /]# ifconfig
    14. eth0: flags=4163 mtu 1500
    15. inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
    16. ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
    17. RX packets 7805 bytes 14138450 (13.4 MiB)
    18. RX errors 0 dropped 0 overruns 0 frame 0
    19. TX packets 4715 bytes 259714 (253.6 KiB)
    20. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
    21. lo: flags=73 mtu 65536
    22. inet 127.0.0.1 netmask 255.0.0.0
    23. loop txqueuelen 1000 (Local Loopback)
    24. RX packets 0 bytes 0 (0.0 B)
    25. RX errors 0 dropped 0 overruns 0 frame 0
    26. TX packets 0 bytes 0

    [上一节][Docker]二.Docker 镜像,仓库,容器介绍以及详解

    [下一节][Docker]四.Docker部署nodejs项目,部署Mysql,部署Redis,部署Mongodb 

  • 相关阅读:
    小程序开发的报价为什么有差别?需要多少钱?
    windows下搭建appium+android测试环境(node.js样例)
    javase集合 温故而知新
    五种方式实现 Java 单例模式
    小程序显示数据
    【JVM笔记】方法调用与返回字节码指令
    第 43 章 MDK 的编译过程及文件类型全解
    Android的handler消息收发处理——子线程与主线程(UI线程)间的通信
    C语言之数学运算强化练习题
    基于CNN-GRU-Attention的时间序列回归预测matlab仿真
  • 原文地址:https://blog.csdn.net/zhoupenghui168/article/details/134045594