操作步骤如下:
#搜索镜像
docker search nginxdocker search nginxdocker search nginx
#拉去镜像
docker pull nginxdocker pull nginxdocker pull nginx
#查看本地镜像
docker imagesdocker imagesdocker images
#启动nginx容器
docker run --name nginx -d -p 80:80 nginxdocker run --name nginx -d -p 80:80 nginxdocker run --name nginx -d -p 80:80 nginx
以上步骤可查看我的上一个博客,对其讲解比较详细
https://blog.csdn.net/qq_37641547/article/details/107356549
本节主要操作通过宿主机配置控制nginx容器
#在宿主机中创建挂载目录
<span class="token function">mkdir</span> -p /home/nginx/<span class="token punctuation">{<!-- --></span>conf,conf.d,html,log<span class="token punctuation">}</span><span class="token function">mkdir</span> -p /home/nginx/<span class="token punctuation">{<!-- --></span>conf,conf.d,html,log<span class="token punctuation">}</span>mkdir -p /home/nginx/{conf,conf.d,html,log}
#将容器里的配置信息远程复制到宿主机
<span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span> <span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span> <span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span> <span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span><span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span> <span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span> <span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span> <span class="token punctuation">[</span>root@localhost ~<span class="token punctuation">]</span>[root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~]
#启动nginx容器
<span class="token punctuation">[</span>root@localhost conf.d<span class="token punctuation">]</span><span class="token punctuation">[</span>root@localhost conf.d<span class="token punctuation">]</span>[root@localhost conf.d]
第一个-v:挂载nginx的主配置文件,以方便在宿主机上直接修改容器的配置文件
第二个-v:挂载容器内nginx的日志,容器运行起来之后,可以直接在宿主机的这个目录中查看nginx日志
第三个-v:挂载静态页面目录
第四个-v:挂载nginx子配置文件
#验证:通过修改宿主机的nginx配置文件来控制nginx容器
#复制index.html重命名为main.html,并编辑此页面区别于index.html页面内容
<span class="token function">cd</span> /home/nginx/html/ <span class="token function">cp</span> index.html main.html<span class="token function">cd</span> /home/nginx/html/ <span class="token function">cp</span> index.html main.htmlcd /home/nginx/html/ cp index.html main.html
#编辑main.html区别于index.html页面内容
<span class="token function">vi</span> main.html<span class="token function">vi</span> main.htmlvi main.html
#修改配置文件/conf.d/default.conf
<span class="token function">vi</span> default.conf<span class="token function">vi</span> default.confvi default.conf
#修改后如下:
server <span class="token punctuation">{<!-- --></span> listen 80<span class="token punctuation">;</span> listen <span class="token punctuation">[</span>::<span class="token punctuation">]</span>:80<span class="token punctuation">;</span> server_name localhost<span class="token punctuation">;</span> location <span class="token operator">/</span> <span class="token punctuation">{<!-- --></span> root <span class="token operator">/</span>usr<span class="token operator">/</span>share<span class="token operator">/</span>nginx<span class="token operator">/</span>html<span class="token punctuation">;</span> index main<span class="token punctuation">.</span>html<span class="token punctuation">;</span> <span class="token punctuation">}</span> error_page 500 502 503 504 <span class="token operator">/</span>50x<span class="token punctuation">.</span>html<span class="token punctuation">;</span> location = <span class="token operator">/</span>50x<span class="token punctuation">.</span>html <span class="token punctuation">{<!-- --></span> root <span class="token operator">/</span>usr<span class="token operator">/</span>share<span class="token operator">/</span>nginx<span class="token operator">/</span>html<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>server <span class="token punctuation">{<!-- --></span> listen 80<span class="token punctuation">;</span> listen <span class="token punctuation">[</span>::<span class="token punctuation">]</span>:80<span class="token punctuation">;</span> server_name localhost<span class="token punctuation">;</span> location <span class="token operator">/</span> <span class="token punctuation">{<!-- --></span> root <span class="token operator">/</span>usr<span class="token operator">/</span>share<span class="token operator">/</span>nginx<span class="token operator">/</span>html<span class="token punctuation">;</span> index main<span class="token punctuation">.</span>html<span class="token punctuation">;</span> <span class="token punctuation">}</span> error_page 500 502 503 504 <span class="token operator">/</span>50x<span class="token punctuation">.</span>html<span class="token punctuation">;</span> location = <span class="token operator">/</span>50x<span class="token punctuation">.</span>html <span class="token punctuation">{<!-- --></span> root <span class="token operator">/</span>usr<span class="token operator">/</span>share<span class="token operator">/</span>nginx<span class="token operator">/</span>html<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index main.html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
#重启nginx容器
docker restart nginxdocker restart nginxdocker restart nginx
#在浏览器中输入http://宿主机ip + nginx端口/ ,正常打开的页面应该是main.html
http://192.168.234.3:80/
docker常用命令:
docker <span class="token function">ps</span> // 查看所有正在运行容器 docker stop containerId // containerId 是容器的ID docker <span class="token function">ps</span> -a // 查看所有容器 $ docker <span class="token function">ps</span> -a -q // 查看所有容器ID docker stop <span class="token variable"><span class="token variable">$(</span>docker <span class="token function">ps</span> -a -q<span class="token variable">)</span></span> // stop停止所有容器 docker <span class="token function">rm</span> <span class="token variable"><span class="token variable">$(</span>docker <span class="token function">ps</span> -a -q<span class="token variable">)</span></span> // remove删除所有容器 docker <span class="token function">ps</span> -l docker create 容器名或者容器ID docker start <span class="token punctuation">[</span>-i<span class="token punctuation">]</span> 容器名 docker run 容器名或者容器ID docker attach 容器名或者容器ID <span class="token function">bash</span> docker <span class="token function">exec</span> -it 容器名或者容器ID <span class="token function">bash</span> docker stop 容器名 docker <span class="token function">rm</span> 容器名 docker <span class="token function">top</span> 容器名 docker inspect 容器名 <span class="token keyword">exit</span> 退出容器docker <span class="token function">ps</span> // 查看所有正在运行容器 docker stop containerId // containerId 是容器的ID docker <span class="token function">ps</span> -a // 查看所有容器 $ docker <span class="token function">ps</span> -a -q // 查看所有容器ID docker stop <span class="token variable"><span class="token variable">$(</span>docker <span class="token function">ps</span> -a -q<span class="token variable">)</span></span> // stop停止所有容器 docker <span class="token function">rm</span> <span class="token variable"><span class="token variable">$(</span>docker <span class="token function">ps</span> -a -q<span class="token variable">)</span></span> // remove删除所有容器 docker <span class="token function">ps</span> -l docker create 容器名或者容器ID docker start <span class="token punctuation">[</span>-i<span class="token punctuation">]</span> 容器名 docker run 容器名或者容器ID docker attach 容器名或者容器ID <span class="token function">bash</span> docker <span class="token function">exec</span> -it 容器名或者容器ID <span class="token function">bash</span> docker stop 容器名 docker <span class="token function">rm</span> 容器名 docker <span class="token function">top</span> 容器名 docker inspect 容器名 <span class="token keyword">exit</span> 退出容器docker ps // 查看所有正在运行容器 docker stop containerId // containerId 是容器的ID docker ps -a // 查看所有容器 $ docker ps -a -q // 查看所有容器ID docker stop $(docker ps -a -q) // stop停止所有容器 docker rm $(docker ps -a -q) // remove删除所有容器 docker ps -l docker create 容器名或者容器ID docker start [-i] 容器名 docker run 容器名或者容器ID docker attach 容器名或者容器ID bash docker exec -it 容器名或者容器ID bash docker stop 容器名 docker rm 容器名 docker top 容器名 docker inspect 容器名 exit 退出容器
以上内容如有错误欢迎指正!!!
原文链接:https://blog.csdn.net/qq_37641547/article/details/107387264
© 版权声明
声明📢本站内容均来自互联网,归原创作者所有,如有侵权必删除。
本站文章皆由CC-4.0协议发布,如无来源则为原创,转载请注明出处。
THE END