拉取镜像

  1. docker pull nginx

启动Nginx

  1. docker run -d --name nginx01 -p 3344:80 nginx

参数意义:

  • -d:后台运行
  • —name:给容器取命名
  • -p:指定端口暴露映射,Nginx默认80端口,和服务器的外网3344映射,外网访问服务器的3344端口,就能访问到这个Nginx容器的80端口了

    进入容器

    1. [root@izj6cev682kqg86i4ogj8rz ~]# docker ps
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. 7b66cca418c6 nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 0.0.0.0:3344->80/tcp nginx01
    4. 08e8a0cbc58e centos "/bin/bash" 4 hours ago Up 4 hours gracious_chaum
    5. [root@izj6cev682kqg86i4ogj8rz ~]# docker exec -it nginx01 /bin/bash
    6. root@7b66cca418c6:/# whereis nginx
    7. nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
    8. root@7b66cca418c6:/# cd /etc/nginx
    9. root@7b66cca418c6:/etc/nginx# ls
    10. conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
    11. root@7b66cca418c6:/etc/nginx#
    可以看到里面存在Nginx的配置文件,可以自己改

    停止Nginx

    1. [root@izj6cev682kqg86i4ogj8rz ~]# docker ps
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. 7b66cca418c6 nginx "/docker-entrypoint.…" 11 minutes ago Up 10 minutes 0.0.0.0:3344->80/tcp nginx01
    4. 08e8a0cbc58e centos "/bin/bash" 4 hours ago Up 4 hours gracious_chaum
    5. [root@izj6cev682kqg86i4ogj8rz ~]# docker stop 7b66cca418c6
    6. 7b66cca418c6
    7. [root@izj6cev682kqg86i4ogj8rz ~]#
    Nginx被停止了以后,外网访问3344端口就访问不到了