https://hub.docker.com/search?q=nginx&type=image
一、查看docker是否安装nginx
docker images
二、安装nginx
docker pull nginx
三、运行nginx镜像
docker run --name mynginx -p 80:80 -d nginx
四、查看运行结果
docker ps
网页访问,浏览器输入虚拟机IP:端口号地址回车,就可以看到 “Welcome to nginx!”
centos开放的端口并设置
firewall-cmd --zone=public --list-ports #查看所有开放的端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent # 开放5672端口
firewall-cmd --zone=public --remove-port=8080/tcp --permanent #关闭5672端口
firewall-cmd --reload # 配置立即生效systemctl start/stop/restart firewalld.service #打开/关闭/重启防火墙
firewall-cmd --state #查看防火墙状态
五、把nginx 配置文件映射到虚拟机(每次改配置都需要到镜像里改,太麻烦!!!)
(1)本地创建管理目录
mkdir -p /data/nginx
mkdir -p /data/nginx/www
mkdir -p /data/nginx/conf
mkdir -p /data/nginx/logs
(2)将容器中的相应文件copy到刚创建的管理目录中
docker cp 6f37f231a7c1:/etc/nginx/nginx.conf /data/nginx/
docker cp 6f37f231a7c1:/etc/nginx/conf.d /data/nginx/conf/
docker cp 6f37f231a7c1:/usr/share/nginx/html/ /data/nginx/www/
docker cp 6f37f231a7c1:/var/log/nginx/ /data/nginx/logs/
注:docker cp 6f37f231a7c1中的 "6f37f231a7c1" 为容器ID(docker ps可查看)
(3)停止并移除容器
docker stop 6f37f231a7c1 #停止容器
docker rm 6f37f231a7c1 #移除容器
(4)再次启动容器并作目录挂载
docker run --name nginx -p 80:80 -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/www/:/usr/share/nginx/html/ -v /data/nginx/logs/:/var/log/nginx/ -v /data/nginx/conf/:/etc/nginx/conf.d --privileged=true -d nginx
ps:-p 80:80 端口进行映射,将本地 80端口映射到容器内部的 80 端口。
六、我自己测试的配置文件nginx.conf配置
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;events {worker_connections 1024;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;upstream mynginx{server 192.168.100.164:1111 weight=1;server 192.168.100.164:2222 weight=1;}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {#root html;proxy_pass http://mynginx;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}include /etc/nginx/conf.d/*.conf;
}