Nginx简介

image.png

运行Nginx容器

  • 拉取:

    1. [root@centos ~]# docker pull nginx:1.17.9
    2. 1.17.9: Pulling from library/nginx
    3. 123275d6e508: Pull complete
    4. 9a5d769f04f8: Pull complete
    5. faad4f49180d: Pull complete
    6. Digest: sha256:88ea86df324b03b3205cbf4ca0d999143656d0a3394675630e55e49044d38b50
    7. Status: Downloaded newer image for nginx:1.17.9
    8. docker.io/library/nginx:1.17.9
  • 运行nginx:

    1. # nginx默认开启的端口是80,这里把80端口映射到27431上
    2. [root@centos ~]# docker run -p 27431:80 nginx:1.17.9
    3. 172.17.0.1 - - [20/Nov/2020:14:58:19 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"
  • 通过浏览器即可访问Nginx服务器:

image.png

  • 挂载目录:

    1. # 后台启动一个nginx容器,然后指定挂载目录(将docker中的目录/usr/share/nginx/html挂载到${PWD}/ngnix/html目录中)
    2. [root@centos ~]# docker run -d --name nginx -p 27431:80 -v ${PWD}/nginx/html:/usr/share/nginx/html nginx:1.17.9
    3. dd0edd37dad98651f079b371d45495ddd1dd8541fe797f8523ee78469c868205
  • 在${PWD}/nginx/html目录下添加静态页面

    1. [root@centos html]# cat index.html
    2. <h1>This is a docker run a Nginx container</h1>
  • 访问Nginx的时候就可以访问到静态页面了

image.png

其他相关查看操作

使用后台启动容器:

  1. [root@centos ~]# docker run -d --name nginx -p 27431:80 nginx:1.17.9
  2. 4b37595443abb80a39e15b429a76d02bd8ac36f5b494082ca4e2b1245b819114
  3. [root@centos ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 4b37595443ab nginx:1.17.9 "nginx -g 'daemon of…" 33 seconds ago Up 32 seconds 0.0.0.0:27431->80/tcp nginx

查看到docker中的目录文件:

  1. [root@centos html]# docker exec -it nginx ls /usr/share/nginx
  2. html

停掉或者删除容器

  1. [root@centos ~]# docker stop nginx
  2. nginx
  3. # 下面这个方式可以在容器运行的时候直接销毁,但是破坏性较大
  4. [root@centos ~]# docker rm -f nginx
  5. nginx