1:拉取nginx镜像
[root@xxxxx]# docker pull nginx
2:查看docker下的镜像
[root@xxxxx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest d1a364dc548d 8 weeks ago 133MB
3:启动运行nginx容器
[root@xxxxx]# docker run -p 80:80 -d nginx //将80端口映射为8080,或者80:80还是原先的80端口,不可以不写
4:查看运行情况:
[root@xxxxx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49412439764c nginx "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 0.0.0.0:80->80/tcp nginx
docker ps -a 查看docker所有安装的容器包括已经停止运行的容器
5:在宿主机上建立nginx对应的文件夹,用来存放nginx配置文件,nginx的日志,图片文件等存储的文件夹
创建nginx配置文件的文件夹
[root@xxxx ~]# cd /data/
[root@xxxx data]# mkdir -p nginx/conf.d
创建nginx日志文件夹
[root@xxxx data]# cd nginx/
[root@xxxx nginx]# mkdir logs
创建文件图片等文件存储文件夹
[root@xxxx data]# mkdir www
6:进入nginx容器中把配置文件给复制出来,方便后续对配置文件信息的修改更新
[root@xxxxx ~]# docker cp nginx:/etc/nginx/conf.d/default.conf /data/nginx/conf.d/
进入容器的命令:docker exec -it nginx bash
从宿主机机复制文件到容器中命令:
docker cp /data/nginx/conf.d/default.conf nginx:/etc/nginx/conf.d/
7::修改宿主机中的/data/nginx/conf.d/default.conf 信息
8:停止运行docker下的nginx
[root@xxxxx logs]# docker stop nginx
9:删除nginx容器
[root@xxxx logs]# docker rm nginx
10:重新运行nginx容器
[root@xxxx logs]# docker run -p 80:80 --name nginx -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d -v /data/www:/usr/share/nginx/html -d nginx
如需要配置图片等需修改配置文件
[root@iZ8vb8kifrnrz3w56c34iiZ conf.d]# more default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /images {
alias /usr/share/nginx/html/images; #nginx容器中的图片保存位置
autoindex on; #打开浏览功能
}
#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 /usr/share/nginx/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;
#}
}
重启nginx
[root@xxxx logs]# docker stop nginx //停止nginx
[root@xxxx logs]# docker start nginx //启动nginx
最后浏览器访问的效果: