操作步骤如下:
#搜索镜像
docker search nginx
#拉去镜像
docker pull nginx
#查看本地镜像
docker images
#启动nginx容器
docker run --name nginx -d -p 80:80 nginx
以上步骤可查看我的上一个博客,对其讲解比较详细
https://blog.csdn.net/qq_37641547/article/details/107356549
本节主要操作通过宿主机配置控制nginx容器
#在宿主机中创建挂载目录
mkdir -p /home/nginx/{conf,conf.d,html,log}
#将容器里的配置信息远程复制到宿主机
[root@localhost ~]# docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf/
[root@localhost ~]# docker cp nginx:/var/log/nginx/* /home/nginx/log/
[root@localhost ~]# docker cp nginx:/usr/share/nginx/html/* /home/nginx/html/
[root@localhost ~]# docker cp nginx:/etc/nginx/conf.d/default.conf /home/nginx/conf.d/
#启动nginx容器
[root@localhost conf.d]# docker run --name nginx -d -p 80:80 -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/log:/var/log/nginx -v /home/nginx/html:/usr/share/nginx/html -v /home/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf nginx
第一个-v:挂载nginx的主配置文件,以方便在宿主机上直接修改容器的配置文件
第二个-v:挂载容器内nginx的日志,容器运行起来之后,可以直接在宿主机的这个目录中查看nginx日志
第三个-v:挂载静态页面目录
第四个-v:挂载nginx子配置文件
#验证:通过修改宿主机的nginx配置文件来控制nginx容器
#复制index.html重命名为main.html,并编辑此页面区别于index.html页面内容
cd /home/nginx/html/
cp index.html main.html
#编辑main.html区别于index.html页面内容
vi main.html
#修改配置文件/conf.d/default.conf
vi default.conf
#修改后如下:
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index main.html;
}
#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容器
docker restart nginx
#在浏览器中输入http://宿主机ip + nginx端口/ ,正常打开的页面应该是main.html
http://192.168.234.3:80/
docker常用命令:
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 create + docker start
docker attach 容器名或者容器ID bash # 进入容器的命令行(退出容器后容器会停止)
docker exec -it 容器名或者容器ID bash # 进入容器的命令行
docker stop 容器名 # 停止容器
docker rm 容器名 # 删除容器
docker top 容器名 # 查看WEB应用程序容器的进程
docker inspect 容器名 # 查看Docker的底层信息
exit 退出容器
以上内容如有错误欢迎指正!!!