1.查找Docker Hub 上nginx的镜像
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# docker search nginx
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/nginx Official build of Nginx. 11500 [OK]
docker.io docker.io/jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 1607 [OK]
docker.io docker.io/richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 716 [OK]
docker.io docker.io/bitnami/nginx Bitnami nginx Docker Image 67 [OK]
docker.io docker.io/linuxserver/nginx An Nginx container, brought to you by Linu... 63
docker.io docker.io/tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rt... 46 [OK]
docker.io docker.io/nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 20
docker.io docker.io/arm64v8/nginx Official build of Nginx. 16
docker.io docker.io/schmunk42/nginx-redirect A very simple container to redirect HTTP t... 16 [OK]
docker.io docker.io/nginxdemos/hello NGINX webserver that serves a simple page ... 14 [OK]
docker.io docker.io/blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK]
2.拉取官方的镜像
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# docker pull nginx
3.查看安装后的nginx镜像
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 53f3fd8007f7 4 weeks ago 109 MB
vehicle-startup latest 4ccf0194850f 2 months ago 739 MB
docker.io/tomcat 8 dd6ff929584a 3 months ago 463 MB
docker.io/tomcat latest dd6ff929584a 3 months ago 463 MB
docker.io/redis latest ce25c7293564 5 months ago 95 MB
docker.io/centos/mysql-57-centos7 latest f171d281f17c 6 months ago 453 MB
docker.io/java 8 d23bdf5b1b1b 2 years ago 643 MB
docker.io/grange74/centos6-java6-tomcat6 latest 481203b6b87d 4 years ago 347 MB
docker.io/filemon/oracle_11g latest d03c4f0a4743 4 years ago 8.62 GB
4.启动一个实例nginx容器
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# docker run --name nginx-test-web -p 8082:8080 -d docker.io/nginx
--name nginx-test-web
:容器名称,
-p 8082:8080
:端口映射,将本地端口8082映射到容器中的8080端口,
-d
:容器一直在后台运行
docker.io/nginx
:是创建的nginx镜像的REPOSITORY
5.查看names 是nginx-test-web的容器
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cef29fcf982 nginx "nginx -g 'daemon ..." 13 days ago Up 25 hours 0.0.0.0:8082->80/tcp nginx-test-web
4cef29fcf982
:是生成的实例容器ID
6.将nginx的容器内的目录挂载到本地
目的:方便项目的部署
(1)创建本地的目录用于存放项目文件
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
www
: 目录将映射为 nginx 容器配置的虚拟目录。
logs
: 目录将映射为 nginx 容器的日志目录。
conf
: 目录里的配置文件将映射为 nginx 容器的配置文件。
(2)拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# docker cp 4cef29fcf982:/etc/nginx/nginx.conf ~/nginx/conf
(3)进行部署
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# ocker run -d -p 8082:80 --name nginx-test-web -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx nginx
-p 8082:80
: 将容器的 80 端口映射到主机的 8082 端口。
--name nginx-test-web
:将容器命名为 runoob-nginx-test-web。
~/nginx/www:/usr/share/nginx/html
:将我们自己创建的 www 目录挂载到容器的 /usr/share/nginx/html。
-v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
-v ~/nginx/logs:/var/log/nginx
:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。
(4)启动以上命令后进入 ~/nginx/www 目录:
[root@izbp1jdwfzdvn1q6ccy1xbz ~]# cd nginx/www
[root@izbp1jdwfzdvn1q6ccy1xbz www]# ls